Class Method
- All Implemented Interfaces:
AnnotatedElement,GenericDeclaration,Member
public final class Method extends AccessibleObject implements GenericDeclaration, Member
-
Field Summary
Fields Modifier and Type Field Description static Comparator<Method>ORDER_BY_SIGNATUREOrders methods by their name, parameters and return type. -
Method Summary
Modifier and Type Method Description booleanequals(Object object)Indicates whether or not the specifiedobjectis equal to this method.protected Annotation[]getDeclaredAnnotations(boolean copy)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.protected StringgetSignatureAttribute()TypeVariable<Method>[]getTypeParameters()Returns the declared type parameters in declaration order.inthashCode()Returns an integer hash code for this method.Objectinvoke(Object receiver, Object... args)Returns the result of dynamically invoking this method.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.AccessibleObject
getAnnotation, getAnnotations, getDeclaredAnnotations, isAccessible, isAnnotationPresent, setAccessible, setAccessible
-
Field Details
-
ORDER_BY_SIGNATURE
Orders methods by their name, parameters and return type.
-
-
Method Details
-
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
-
getSignatureAttribute
-
toGenericString
Returns the string representation of the method's declaration, including the type parameters.- Returns:
- the string representation of this method
-
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
-
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
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
- Overrides:
getDeclaredAnnotationsin classAccessibleObject
-
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.- Returns:
- an array of arrays of
Annotationinstances
-
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
-
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
-
equals
Indicates whether or not the specifiedobjectis equal to this method. To be equal, the specified object must be an instance ofMethodwith the same declaring class and parameter types as this method.- Overrides:
equalsin classObject- Parameters:
object- the object to compare- Returns:
trueif the specified object is equal to this method,falseotherwise- See Also:
hashCode()
-
getDeclaringClass
Returns the class that declares this method.- Specified by:
getDeclaringClassin interfaceMember- Returns:
- the declaring 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
-
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
-
getName
Returns the name of the method represented by thisMethodinstance. -
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
Returns theClassassociated with the return type of this method.- Returns:
- the return type
-
hashCode
public int hashCode()Returns an integer hash code for this method. Objects which are equal return the same value for this method. The hash code for this Method is the hash code of the name of this method.- Overrides:
hashCodein classObject- Returns:
- hash code for this method
- See Also:
equals(java.lang.Object)
-
invoke
public Object invoke(Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetExceptionReturns 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
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
-