Javareflect 简明教程

java.lang.reflect - Method Class

Introduction

java.lang.reflect.Method 类提供有关类或接口上单个方法的信息并可访问该方法。反射方法可以是类方法或实例方法(包括抽象方法)。java.lang.reflect.Method 允许在将实际参数与基础方法的形式参数匹配时进行扩展转换,但如果发生缩小转换,则它会引发 IllegalArgumentException。

The java.lang.reflect.Method class provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method). A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method’s formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.

Class declaration

以下是对 java.lang.reflect.Method 类的声明:

Following is the declaration for java.lang.reflect.Method class −

public final class Method<T>
   extends AccessibleObject
      implements GenericDeclaration, Member

Class methods

Sr.No.

Method & Description

1

boolean equals(Object obj)Compares this Method against the specified object.

2

<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.

3

Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.

4

Class<T> getDeclaringClass()Returns the Class object representing the class that declares the method represented by this Method object.

5

Object getDefaultValue()Returns the default value for the annotation member represented by this Method instance.

6

Class<?>[] getExceptionTypes()Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying constructor represented by this Constructor object.

7

Type[] getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.

8

Type[] getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.

9

Type getGenericReturnType()Returns a Type object that represents the formal return type of the method represented by this Method object.

10

int getModifiers()Returns the Java language modifiers for the method represented by this Method object, as an integer.

11

String getName()Returns the name of this method, as a string.

12

Annotation[][] getParameterAnnotations()Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Method object.

13

Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Method object.

14

Class<?> getReturnType()Returns a Class object that represents the formal return type of the method represented by this Method object.

15

int hashCode()Returns a hashcode for this Constructor.

16

Object invoke(Object obj, Object…​ args)Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.

17

boolean isBridge()Returns true if this method is a bridge method; returns false otherwise.

18

boolean isSynthetic()Returns true if this method is a synthetic method; returns false otherwise.

19

boolean isVarArgs()Returns true if this method was declared to take a variable number of arguments; returns false otherwise.

20

String toGenericString()Returns a string describing this Method, including type parameters.

21

String toString()Returns a string describing this Method.

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.reflect.AccessibleObject

  2. java.lang.Object