Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 337
نمايش فراداده

Methodjava.lang.reflect

Java 1.1

This class represents a method. Instances of Method are obtained by calling the getMethod( ) and related methods of java.lang.Class. Method implements the Member interface, so you can use the methods of that interface to obtain the method name, modifiers, and declaring class. In addition, getreturnType( ) , getParameterTypes( ), and getExceptionTypes( ) also return important information about the represented method.

Perhaps most importantly, the invoke( ) method allows the method represented by the Method object to be invoked with a specified array of argument values. If any of the arguments are of primitive types, they must be converted to their corresponding wrapper object types in order to be passed to invoke( ). If the represented method is an instance method (i.e., if it is not static), the instance on which it should be invoked must also be passed to invoke( ). The return value of the represented method is returned by invoke( ). If the return value is a primitive value, it is first converted to the corresponding wrapper type. If the invoked method causes an exception, the Throwable object it throws is wrapped within the InvocationTargetException that is thrown by invoke( ).

In Java 5.0, Method implements GenericDeclaration to support reflection on the type variables defined by generic methods and AnnotatedElement to support reflection on method annotations. Additionally, getParameterAnnotations( ) supports reflection on method parameter annotations. The new methods getGenericReturnType( ) , getGenericParameterTypes( ), and getGenericExceptionTypes( ) support reflection on generic method signatures. Finally, the new isVarArgs( ) method returns true if the method was declared using Java 5.0 varargs syntax.

Figure 10-98. java.lang.reflect.Method

public final class

Method extends AccessibleObject implements GenericDeclaration, Member { // No Constructor // Public Instance Methods

5.0 public Object

getDefaultValue ( ); public Class<?>[ ]

getExceptionTypes ( );

5.0 public Type[ ]

getGenericExceptionTypes ( );

5.0 public Type[ ]

getGenericParameterTypes ( );

5.0 public Type

getGenericReturnType ( );

5.0 public java.lang.annotation.Annotation[ ][ ]

getParameterAnnotations ( ); public Class<?>[ ]

getParameterTypes ( ); public Class<?>

getReturnType ( ); public Object

invoke (Object

obj , Object...

args ) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;

5.0 public boolean

isBridge ( );

5.0 public boolean

isVarArgs ( );

5.0 public String

toGenericString ( ); // Methods Implementing GenericDeclaration

5.0 public TypeVariable<Method>[ ]

getTypeParameters ( ); // Methods Implementing Member public Class<?>

getDeclaringClass ( ); public int

getModifiers ( ); public String

getName ( );

5.0 public boolean

isSynthetic ( ); // Public Methods Overriding AccessibleObject

5.0 public <T extends java.lang.annotation.Annotation> T

getAnnotation (Class<T>

annotationClass );

5.0 public java.lang.annotation.Annotation[ ]

getDeclaredAnnotations ( ); // Public Methods Overriding Object public boolean

equals (Object

obj ); public int

hashCode ( ); public String

toString ( ); }

Passed To

java.lang.annotation.AnnotationTypeMismatchException.AnnotationTypeMismatchException( ), InvocationHandler.invoke( )

Returned By

Class.{getDeclaredMethod( ), geTDeclaredMethods( ), getEnclosingMethod( ), getMethod( ), getMethods( )}, java.lang.annotation.AnnotationTypeMismatchException.element( )