Class Method
- java.lang.Object
-
- java.lang.reflect.AccessibleObject
-
- java.lang.reflect.AbstractMethod
-
- java.lang.reflect.Method
-
- All Implemented Interfaces:
AnnotatedElement,GenericDeclaration,Member
public final class Method extends AbstractMethod implements GenericDeclaration, Member
This class represents a method. Information about the method can be accessed, and the method can be invoked dynamically.
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<Method>ORDER_BY_SIGNATUREOrders methods by their name, parameters and return type.-
Fields inherited from class java.lang.reflect.AbstractMethod
accessFlags, artMethod, declaringClass, declaringClassOfOverriddenMethod, dexMethodIndex
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object other)Returns true ifotherhas the same declaring class, name, parameters and return type as this method.<A extends Annotation>
AgetAnnotation(Class<A> annotationType)Returns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).Annotation[]getAnnotations()Returns, for this element, an array containing all annotations (including inherited annotations).Annotation[]getDeclaredAnnotations()Returns, for this element, all annotations that are explicitly declared (not inherited).Class<?>getDeclaringClass()Returns the class that declares this method.ObjectgetDefaultValue()Returns the default value for the annotation member represented by this method.Class<?>[]getExceptionTypes()Returns the exception types as an array ofClassinstances.Type[]getGenericExceptionTypes()Returns the exception types as an array ofTypeinstances.Type[]getGenericParameterTypes()Returns the parameter types as an array ofTypeinstances, in declaration order.TypegetGenericReturnType()Returns the return type of this method as aTypeinstance.intgetModifiers()Returns the modifiers for this method.StringgetName()Returns the name of the method represented by thisMethodinstance.Annotation[][]getParameterAnnotations()Returns an array of arrays that represent the annotations of the formal parameters of this method.Class<?>[]getParameterTypes()Returns an array ofClassobjects associated with the parameter types of this method.Class<?>getReturnType()Returns theClassassociated with the return type of this method.TypeVariable<Method>[]getTypeParameters()Returns the declared type parameters in declaration order.inthashCode()Returns an integer hash code for this object.Objectinvoke(Object receiver, Object... args)Returns the result of dynamically invoking this method.booleanisAnnotationPresent(Class<? extends Annotation> annotationType)Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).booleanisBridge()Indicates whether or not this method is a bridge.booleanisSynthetic()Indicates whether or not this method is synthetic.booleanisVarArgs()Indicates whether or not this method takes a variable number argument.StringtoGenericString()Returns the string representation of the method's declaration, including the type parameters.StringtoString()Returns a string containing a concise, human-readable description of this method.-
Methods inherited from class java.lang.reflect.AbstractMethod
compareParameters, equalMethodParameters, getAccessFlags, getDexMethodIndex
-
Methods inherited from class java.lang.reflect.AccessibleObject
isAccessible, setAccessible, setAccessible
-
-
-
-
Field Detail
-
ORDER_BY_SIGNATURE
public static final Comparator<Method> ORDER_BY_SIGNATURE
Orders methods by their name, parameters and return type.
-
-
Method Detail
-
getAnnotations
public Annotation[] getAnnotations()
Description copied from interface:AnnotatedElementReturns, for this element, an array containing all annotations (including inherited annotations). If there are no annotations present, this method returns a zero length array.- Specified by:
getAnnotationsin interfaceAnnotatedElement- Overrides:
getAnnotationsin classAbstractMethod- Returns:
- an array of all annotations for this element
-
getModifiers
public int getModifiers()
Returns the modifiers for this method. TheModifierclass should be used to decode the result.- Specified by:
getModifiersin interfaceMember- Returns:
- the modifiers for this method
- See Also:
Modifier
-
isVarArgs
public boolean isVarArgs()
Indicates whether or not this method takes a variable number argument.- Returns:
trueif a vararg is declared,falseotherwise
-
isBridge
public boolean isBridge()
Indicates whether or not this method is a bridge.- Returns:
trueif this method is a bridge,falseotherwise
-
isSynthetic
public boolean isSynthetic()
Indicates whether or not this method is synthetic.- Specified by:
isSyntheticin interfaceMember- Returns:
trueif this method is synthetic,falseotherwise
-
getName
public String getName()
Returns the name of the method represented by thisMethodinstance.- Specified by:
getNamein interfaceMember- Specified by:
getNamein classAbstractMethod- Returns:
- the name of this method
-
getDeclaringClass
public Class<?> getDeclaringClass()
Returns the class that declares this method.- Specified by:
getDeclaringClassin interfaceMember- Returns:
- the declaring class
-
getExceptionTypes
public Class<?>[] getExceptionTypes()
Returns the exception types as an array ofClassinstances. If this method has no declared exceptions, an empty array is returned.- Returns:
- the declared exception classes
-
getParameterTypes
public Class<?>[] getParameterTypes()
Returns an array ofClassobjects associated with the parameter types of this method. If the method was declared with no parameters, an empty array will be returned.- Returns:
- the parameter types
-
getReturnType
public Class<?> getReturnType()
Returns theClassassociated with the return type of this method.- Returns:
- the return type
-
hashCode
public int hashCode()
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.Equivalent to
getDeclaringClass().getName().hashCode() ^ getName().hashCode().- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(Object other)
Returns true ifotherhas the same declaring class, name, parameters and return type as this method.- Overrides:
equalsin classAbstractMethod- Parameters:
other- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
toGenericString
public String toGenericString()
Returns the string representation of the method's declaration, including the type parameters.- Returns:
- the string representation of this method
-
getTypeParameters
public TypeVariable<Method>[] getTypeParameters()
Description copied from interface:GenericDeclarationReturns the declared type parameters in declaration order. If there are no type parameters, this method returns a zero length array.- Specified by:
getTypeParametersin interfaceGenericDeclaration- Returns:
- the declared type parameters in declaration order
-
getGenericParameterTypes
public Type[] getGenericParameterTypes()
Returns the parameter types as an array ofTypeinstances, in declaration order. If this method has no parameters, an empty array is returned.- Returns:
- the parameter types
- Throws:
GenericSignatureFormatError- if the generic method signature is invalidTypeNotPresentException- if any parameter type points to a missing typeMalformedParameterizedTypeException- if any parameter type points to a type that cannot be instantiated for some reason
-
isAnnotationPresent
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
Description copied from interface:AnnotatedElementIndicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- Overrides:
isAnnotationPresentin classAbstractMethod- Parameters:
annotationType- the type of the annotation to search for- Returns:
trueif the annotation exists,falseotherwise
-
getGenericExceptionTypes
public Type[] getGenericExceptionTypes()
Returns the exception types as an array ofTypeinstances. If this method has no declared exceptions, an empty array will be returned.- Returns:
- an array of generic exception types
- Throws:
GenericSignatureFormatError- if the generic method signature is invalidTypeNotPresentException- if any exception type points to a missing typeMalformedParameterizedTypeException- if any exception type points to a type that cannot be instantiated for some reason
-
getGenericReturnType
public Type getGenericReturnType()
Returns the return type of this method as aTypeinstance.- Returns:
- the return type of this method
- Throws:
GenericSignatureFormatError- if the generic method signature is invalidTypeNotPresentException- if the return type points to a missing typeMalformedParameterizedTypeException- if the return type points to a type that cannot be instantiated for some reason
-
getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
Description copied from interface:AnnotatedElementReturns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Overrides:
getDeclaredAnnotationsin classAbstractMethod- Returns:
- an array of annotations declared for this element
-
getAnnotation
public <A extends Annotation> A getAnnotation(Class<A> annotationType)
Description copied from interface:AnnotatedElementReturns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).- Specified by:
getAnnotationin interfaceAnnotatedElement- Overrides:
getAnnotationin classAbstractMethod- Parameters:
annotationType- the type of the annotation to search for- Returns:
- the annotation with the specified type or
null
-
getParameterAnnotations
public Annotation[][] getParameterAnnotations()
Returns an array of arrays that represent the annotations of the formal parameters of this method. If there are no parameters on this method, then an empty array is returned. If there are no annotations set, then and array of empty arrays is returned.- Specified by:
getParameterAnnotationsin classAbstractMethod- Returns:
- an array of arrays of
Annotationinstances
-
getDefaultValue
public Object getDefaultValue()
Returns the default value for the annotation member represented by this method.- Returns:
- the default value, or
nullif none - Throws:
TypeNotPresentException- if this annotation member is of typeClassand no definition can be found
-
invoke
public Object invoke(Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
Returns the result of dynamically invoking this method. Equivalent toreceiver.methodName(arg1, arg2, ... , argN).If the method is static, the receiver argument is ignored (and may be null).
If the method takes no arguments, you can pass
(Object[]) nullinstead of allocating an empty array.If you're calling a varargs method, you need to pass an
Object[]for the varargs parameter: that conversion is usually done injavac, not the VM, and the reflection machinery does not do this for you. (It couldn't, because it would be ambiguous.)Reflective method invocation follows the usual process for method lookup.
If an exception is thrown during the invocation it is caught and wrapped in an InvocationTargetException. This exception is then thrown.
If the invocation completes normally, the return value itself is returned. If the method is declared to return a primitive type, the return value is boxed. If the return type is void, null is returned.
- Parameters:
receiver- the object on which to call this method (or null for static methods)args- the arguments to the method- Returns:
- the result
- Throws:
NullPointerException- ifreceiver == nullfor a non-static methodIllegalAccessException- if this method is not accessible (seeAccessibleObject)IllegalArgumentException- if the number of arguments doesn't match the number of parameters, the receiver is incompatible with the declaring class, or an argument could not be unboxed or converted by a widening conversion to the corresponding parameter typeInvocationTargetException- if an exception was thrown by the invoked method
-
toString
public String toString()
Returns a string containing a concise, human-readable description of this method. The format of the string is:- modifiers (if any)
- return type or 'void'
- declaring class name
- '('
- parameter types, separated by ',' (if any)
- ')'
- 'throws' plus exception types, separated by ',' (if any)
public native Object java.lang.Method.invoke(Object,Object) throws IllegalAccessException,IllegalArgumentException ,InvocationTargetException
-
-