Java反射类 - Method类源码解析
一、Method简介与定义
1.1 Method简介
Method提供了对于在类或接口中单个方法的信息和访问,反射方法可能是一个类方法或实体方法(包含抽象方法)。
1.2 Method定义
public final class Method extends Executable {}
1.3 Method父类 Executable类
/* * 方法和构造函数的公共功能的共享超类。 */ public abstract sealed class Executable extends AccessibleObject implements Member, GenericDeclaration permits Constructor, Method { /* *{@code Parameter}对象的数组,表示由该对象表示的底层可执行文件的所有参数。如果可执行文件没有参数,则返回长度为0的数组 */ public Parameter[] getParameters() {} /* * 返回一个由注解数组组成的数组,这些注解以声明顺序表示由此对象表示的可执行文件的形式参数上的注解。 * 合成参数和强制参数(参见下面的解释),例如内部类构造函数的外部“this”参数将在返回的数组中表示。 * 如果可执行文件没有参数(即没有正式参数、没有合成参数和没有强制参数),将返回一个零长度的数组。 * 如果可执行文件有一个或多个参数,则为每个参数返回一个长度为零的嵌套数组,不带注释。 * 返回数组中包含的注释对象是可序列化的。此方法的调用者可以自由地修改返回的数组; * 它对返回给其他调用者的数组没有影响。 * 编译器可以向方法的参数列表中添加在source中隐式声明的额外参数(“强制”), * 以及在source中既没有隐式声明也没有显式声明的参数(“合成”)。有关更多信息,请参见参数。 * 注意,此方法返回的任何注释都是声明注释。 */ public abstract Annotation[][] getParameterAnnotations(); }
1.4 Method类中主要方法与属性
package java.lang.reflect; import jdk.internal.access.SharedSecrets; import jdk.internal.reflect.CallerSensitive; import jdk.internal.reflect.MethodAccessor; import jdk.internal.reflect.Reflection; import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.IntrinsicCandidate; import jdk.internal.vm.annotation.Stable; import sun.reflect.annotation.ExceptionProxy; import sun.reflect.annotation.TypeNotPresentExceptionProxy; import sun.reflect.generics.repository.MethodRepository; import sun.reflect.generics.factory.CoreReflectionFactory; import sun.reflect.generics.factory.GenericsFactory; import sun.reflect.generics.scope.MethodScope; import sun.reflect.annotation.AnnotationType; import sun.reflect.annotation.AnnotationParser; import java.lang.annotation.Annotation; import java.lang.annotation.AnnotationFormatError; import java.nio.ByteBuffer; import java.util.StringJoiner; public final class Method extends Executable { /* * 返回表示声明由此对象表示的方法的类或接口的Class对象。 */ public Class<?> getDeclaringClass() {return clazz;} /* * 以字符串形式返回此method对象所表示的方法的名称。 */ public String getName() {} /* * @返回Java语言{@linkplain Modifier modifiers}用于这个对象所表示的可执行文件 */ public int getModifiers() {} /* * 返回一个TypeVariable对象数组,这些对象按照声明顺序表示由这个GenericDeclaration * 对象表示的泛型声明声明的类型变量。 * 如果底层泛型声明没有声明类型变量,则返回长度为0的数组。 */ public TypeVariable<Method>[] getTypeParameters() {} /* * 返回一个Class对象,该对象表示由此method对象表示的方法的正式返回类型。 */ public Type getGenericReturnType() {} //返回方法按声明顺序的参数Class数组,没有则返回长度为0的数组 public Class<?>[] getParameterTypes() {} //返回方法的参数个数 public int getParameterCount() {} //返回方法按声明顺序的泛型参数类型数组 public Type[] getGenericParameterTypes() {} //返回方法抛出的异常类对象数组 public Class<?>[] getExceptionTypes() {} //返回方法抛出的异常Type数组 public Type[] getGenericExceptionTypes() {} //比较两个Method是否一样,当两个方法由相同的类声明、相同的名称、相同的参数类型和相同的返回类型,则相等 public boolean equals(Object obj) {} //返回方法的hashCode,由声明方法的类名和方法名称的异或计算而得 public int hashCode() {} //返回方法的字符串表示 public String toString() {} //返回方法的字符串表示(包含类型变量) public String toGenericString() {} //方法调用执行 public Object invoke(Object obj, Object... args)throws IllegalAccessException, IllegalArgumentException,InvocationTargetException{} //是否是桥接方法, 泛型擦除后jvm生的方法 public boolean isBridge() {} //方法是否有可变数量的参数 public boolean isVarArgs() {} //方法是否是合成的 public boolean isSynthetic() {} //方法是否是接口中默认实现方法 public boolean isDefault() {} //返回当前方法实例代表的注解成员的默认值 public Object getDefaultValue() {} //返回当前方法上指定类型的注解 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {} //返回当前方法上声明的所有注解 public Annotation[] getDeclaredAnnotations() {} //返回方法参数上的注解二维数组(多个参数的注解,每个参数可以有多个注解) public Annotation[][] getParameterAnnotations() {} //获取方法返回的AnnotatedType public AnnotatedType getAnnotatedReturnType() {} }