Method类及相关类解读
一、简介
接前面Field类解读,这里介绍Method类及相关类。
二、Method结构
2.1 Method解释
method的方法提供了对于在类或接口中单个方法的信息和访问,反射方法可能是一个类方法或实体方法(包含抽象方法)。
2.2 Method定义
public final class Method extends Executable {
}
关联的Executable定义(这里仅列出独有方法)如下:
public abstract class Executable extends AccessibleObject
implements Member, GenericDeclaration {
/***其他方法省略,在其他博客中已介绍,使用类似 *****/
//返回方法的参数数组
public Parameter[] getParameters() {
}
//返回方法的参数声明AnnotatedType数组
public AnnotatedType[] getAnnotatedParameterTypes() {
}
//返回方法的参数声明异常数组
public AnnotatedType[] getAnnotatedExceptionTypes() {
}
}
使用示例接下来介绍。
三、Method方法
Method的源码解读如下:
package java.lang.reflect;
import sun.reflect.CallerSensitive;
import sun.reflect.MethodAccessor;
import sun.reflect.Reflection;
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;
//method的方法提供了对于在类或接口中单个方法的信息和访问,反射方法可能是一个类方法或实体方法(包含抽象方法)
public final class Method extends Executable {
//返回声明方法的类实例,
public Class<?> getDeclaringClass() {
}
//返回方法的名称
public String getName() {
}
//返回方法修饰符的int表示
public int getModifiers() {
}
//返回方法按声明顺序的类型变量数组
public TypeVariable<Method>[] getTypeParameters() {
}
//返回方法返回类型
public Class<?> getReturnType() {
}
//返回方法返回的Type类型,这里为T
public Type getGenericReturnType() {
}
//返回方法按声明顺序的参数Class数组,没有则返回长度为0的数组
public Class<?>[] getParameterTypes() {
}
//返回方法的参数个数
public int getParameterCount</