Class Method

All Implemented Interfaces:
AnnotatedElement, GenericDeclaration, Member

public final class Method
extends AccessibleObject
implements GenericDeclaration, Member
This class represents a method. Information about the method can be accessed, and the method can be invoked dynamically.
  • Field Details

    • ORDER_BY_SIGNATURE

      public static final Comparator<Method> ORDER_BY_SIGNATURE
      Orders methods by their name, parameters and return type.
  • Method Details

    • getTypeParameters

      public TypeVariable<Method>[] getTypeParameters()
      Description copied from interface: GenericDeclaration
      Returns the declared type parameters in declaration order. If there are no type parameters, this method returns a zero length array.
      Specified by:
      getTypeParameters in interface GenericDeclaration
      Returns:
      the declared type parameters in declaration order
    • getSignatureAttribute

      protected String getSignatureAttribute()
    • toGenericString

      public String toGenericString()
      Returns the string representation of the method's declaration, including the type parameters.
      Returns:
      the string representation of this method
    • getGenericParameterTypes

      public Type[] getGenericParameterTypes()
      Returns the parameter types as an array of Type instances, 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 invalid
      TypeNotPresentException - if any parameter type points to a missing type
      MalformedParameterizedTypeException - if any parameter type points to a type that cannot be instantiated for some reason
    • getGenericExceptionTypes

      public Type[] getGenericExceptionTypes()
      Returns the exception types as an array of Type instances. 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 invalid
      TypeNotPresentException - if any exception type points to a missing type
      MalformedParameterizedTypeException - 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 a Type instance.
      Returns:
      the return type of this method
      Throws:
      GenericSignatureFormatError - if the generic method signature is invalid
      TypeNotPresentException - if the return type points to a missing type
      MalformedParameterizedTypeException - if the return type points to a type that cannot be instantiated for some reason
    • getDeclaredAnnotations

      protected Annotation[] getDeclaredAnnotations​(boolean copy)
      Overrides:
      getDeclaredAnnotations in class AccessibleObject
    • 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.
      Returns:
      an array of arrays of Annotation instances
    • isVarArgs

      public boolean isVarArgs()
      Indicates whether or not this method takes a variable number argument.
      Returns:
      true if a vararg is declared, false otherwise
    • isBridge

      public boolean isBridge()
      Indicates whether or not this method is a bridge.
      Returns:
      true if this method is a bridge, false otherwise
    • isSynthetic

      public boolean isSynthetic()
      Indicates whether or not this method is synthetic.
      Specified by:
      isSynthetic in interface Member
      Returns:
      true if this method is synthetic, false otherwise
    • getDefaultValue

      public Object getDefaultValue()
      Returns the default value for the annotation member represented by this method.
      Returns:
      the default value, or null if none
      Throws:
      TypeNotPresentException - if this annotation member is of type Class and no definition can be found
    • equals

      public boolean equals​(Object object)
      Indicates whether or not the specified object is equal to this method. To be equal, the specified object must be an instance of Method with the same declaring class and parameter types as this method.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare
      Returns:
      true if the specified object is equal to this method, false otherwise
      See Also:
      hashCode()
    • getDeclaringClass

      public Class<?> getDeclaringClass()
      Returns the class that declares this method.
      Specified by:
      getDeclaringClass in interface Member
      Returns:
      the declaring class
    • getExceptionTypes

      public Class<?>[] getExceptionTypes()
      Returns the exception types as an array of Class instances. 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. The Modifier class should be used to decode the result.
      Specified by:
      getModifiers in interface Member
      Returns:
      the modifiers for this method
      See Also:
      Modifier
    • getName

      public String getName()
      Returns the name of the method represented by this Method instance.
      Specified by:
      getName in interface Member
      Returns:
      the name of this method
    • getParameterTypes

      public Class<?>[] getParameterTypes()
      Returns an array of Class objects 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 the Class associated 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:
      hashCode in class Object
      Returns:
      hash code for this method
      See Also:
      equals(java.lang.Object)
    • invoke

      Returns the result of dynamically invoking this method. Equivalent to receiver.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[]) null instead 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 in javac, 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 - if receiver == null for a non-static method
      IllegalAccessException - if this method is not accessible (see AccessibleObject)
      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 type
      InvocationTargetException - 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:
      1. modifiers (if any)
      2. return type or 'void'
      3. declaring class name
      4. '('
      5. parameter types, separated by ',' (if any)
      6. ')'
      7. 'throws' plus exception types, separated by ',' (if any)
      For example: public native Object java.lang.Method.invoke(Object,Object) throws IllegalAccessException,IllegalArgumentException ,InvocationTargetException
      Overrides:
      toString in class Object
      Returns:
      a printable representation for this method