AnnotatedElement

/**
* AnnotatedElement 接口表示目前正在此 VM 中运行的应用程序的一个已注解元素【类、方法、属性】。
* 该接口允许反射性地读取注解。此接口中方法返回的所有注解都是不可变并且可序列化的。
*/
public interface AnnotatedElement {
/**
* 该注解元素上是否有指定的注解存在
*/
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return getAnnotation(annotationClass) != null;
} /**
* 获取该注解元素上的指定注解
*/
<T extends Annotation> T getAnnotation(Class<T> annotationClass); /**
* 获取该注解元素上的所有注解
*/
Annotation[] getAnnotations(); /**
* 获取该注解元素上的所有重复注解,并返回该注解的一个数组。
*
* @param <T>:注解的类型
* @param annotationClass:指定注解的 Class 对象
* @since 1.8
*/
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
T[] result = getDeclaredAnnotationsByType(annotationClass);
if ((result.length == 0) && // Neither directly nor indirectly present
(this instanceof Class) && // the element is a class
AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
Class<?> superClass = ((Class<?>) this).getSuperclass();
if (superClass != null) {
// Determine if the annotation is associated with the
// superclass
result = superClass.getAnnotationsByType(annotationClass);
}
} return result;
} /**
* 获取直接声明在指定类型上的目标注解,不会从父类查找。
*
* @param <T>:注解的类型
* @param annotationClass:注解的 Class 对象
* @since 1.8
*/
default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
// Loop over all directly-present annotations looking for a matching one
for (Annotation annotation : getDeclaredAnnotations()) {
if (annotationClass.equals(annotation.annotationType())) {
// More robust to do a dynamic cast at runtime instead
// of compile-time only.
return annotationClass.cast(annotation);
}
}
return null;
} /**
* 获取直接声明在该元素上的所有重复注解,并返回该注解的一个数组。
*
* @since 1.8
*/
default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return AnnotationSupport.getDirectlyAndIndirectlyPresent(
Arrays.stream(getDeclaredAnnotations()).collect(Collectors.toMap(Annotation::annotationType, Function.identity(), ((first, second) -> first), LinkedHashMap::new)), annotationClass);
} /**
* 返回直接声明在此元素上的所有注解,该方法将忽略继承的注解。
*/
Annotation[] getDeclaredAnnotations();
}

Constructor

  • 属性说明
/**
* Constructor 对象提供类的单个构造方法的信息【参数列表、返回值、访问权限等】
* @since 1.1
*/
public final class Constructor<T> extends Executable {
// 构造方法声明的类型
private final Class<T> clazz;
// 内存槽位置
private final int slot;
// 构造方法的参数类型列表
private final Class<?>[] parameterTypes;
// 构造方法的异常类型列表
private final Class<?>[] exceptionTypes;
// 构造方法的修饰符
private final int modifiers;
// 泛型和注解支持
private transient String signature;
// 延迟初始化的泛型信息仓库
private transient ConstructorRepository genericInfo;
// 构造方法上的注解
private final byte[] annotations;
// 构造方法的形参注解
private final byte[] parameterAnnotations;
private volatile ConstructorAccessor constructorAccessor;
  • 常用方法
    /**
* 读取此构造方法的所在类型
*/
@Override
public Class<T> getDeclaringClass() {
return clazz;
} /**
* 读取此构造方法的名称
*/
@Override
public String getName() {
return getDeclaringClass().getName();
} /**
* 读取此构造方法的访问修饰符
*/
@Override
public int getModifiers() {
return modifiers;
} /**
* 读取此构造方法的参数类型列表
*/
@Override
public Class<?>[] getParameterTypes() {
return parameterTypes.clone();
} /**
* 读取此构造方法的参数个数
*/
public int getParameterCount() {
return parameterTypes.length;
} /**
* 读取此构造方法的异常类型数组
*/
@Override
public Class<?>[] getExceptionTypes() {
return exceptionTypes.clone();
} /**
* 获取声明在此构造函数上指定类型的注解
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return super.getAnnotation(annotationClass);
} /**
* 获取声明在此构造函数上的所有注解
*/
public Annotation[] getDeclaredAnnotations() {
return super.getDeclaredAnnotations();
} /**
* 获取声明在此构造函数参数列表上的所有注解
*/
@Override
public Annotation[][] getParameterAnnotations() {
return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
} /**
* 设置此构造函数的访问权限
*/
@Override
@CallerSensitive
public void setAccessible(boolean flag) {
AccessibleObject.checkPermission();
if (flag) {
checkCanSetAccessible(Reflection.getCallerClass());
}
setAccessible0(flag);
} /**
* 基于此构造函数和具体参数创建对象实例
*/
@CallerSensitive
@ForceInline // to ensure Reflection.getCallerClass optimization
public T newInstance(Object ... initargs)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException
{
if (!override) {
final Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz, clazz, modifiers);
}
if ((clazz.getModifiers() & Modifier.ENUM) != 0) {
throw new IllegalArgumentException("Cannot reflectively create enum objects");
}
ConstructorAccessor ca = constructorAccessor; // read volatile
if (ca == null) {
ca = acquireConstructorAccessor();
}
@SuppressWarnings("unchecked")
final
T inst = (T) ca.newInstance(initargs);
return inst;
}

Method

  • 字段说明
/**
* Method 对象提供关于类或接口上单独某个方法(以及如何访问该方法)的信息。
* Method 允许在匹配要调用的实参与底层方法的形参时进行扩展转换;但如果要进行收缩转换,则会抛出 IllegalArgumentException。
*/
public final class Method extends Executable {
// 此方法所声明类的 Class 对象
private Class<?> clazz;
// 存储的内存槽
private int slot;
// 方法的名称
private String name;
// 方法的返回值类型
private Class<?> returnType;
// 参数类型列表
private Class<?>[] parameterTypes;
// 异常类型列表
private Class<?>[] exceptionTypes;
// 访问修饰符
private int modifiers;
// 泛型方法签名
private transient String signature;
// generic info repository; lazily initialized
private transient MethodRepository genericInfo;
// 方法注解
private byte[] annotations;
// 参数注解
private byte[] parameterAnnotations;
private byte[] annotationDefault;
// 方法访问器
private volatile MethodAccessor methodAccessor;
  • 常用方法
    /**
* 获取此方法所在的类型
*/
@Override
public Class<?> getDeclaringClass() {
return clazz;
} /**
* 读取此方法的返回类型
*/
public Class<?> getReturnType() {
return returnType;
} /**
* 读取此方法的访问修饰符
*/
@Override
public int getModifiers() {
return modifiers;
} /**
* 读取方法名称
*/
@Override
public String getName() {
return name;
} /**
* 读取此方法的参数类型列表
*/
@Override
public Class<?>[] getParameterTypes() {
return parameterTypes.clone();
} /**
* 读取此方法的参数个数
* @since 1.8
*/
public int getParameterCount() {
return parameterTypes.length;
} /**
* 读取此方法的异常类型数组
*/
@Override
public Class<?>[] getExceptionTypes() {
return exceptionTypes.clone();
} /**
* 读取此方法上声明的指定类型的注解
* @since 1.5
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return super.getAnnotation(annotationClass);
} /**
* 读取此方法上声明的所有注解
* @since 1.5
*/
public Annotation[] getDeclaredAnnotations() {
return super.getDeclaredAnnotations();
} /**
* 读取此方法的形参上声明的所有注解
*/
@Override
public Annotation[][] getParameterAnnotations() {
return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
} /**
* 读取此方法返回类型上的注解信息
* @since 1.8
*/
@Override
public AnnotatedType getAnnotatedReturnType() {
return getAnnotatedReturnType0(getGenericReturnType());
} /**
* 获取泛型方法的实际返回类型
*/
public Type getGenericReturnType() {
if (getGenericSignature() != null) {
return getGenericInfo().getReturnType();
} else {
return getReturnType();
}
} /**
* 按声明顺序返回此方法的形参类型列表。
* 如果形参类型是参数化类型,则为其返回的 Type 对象必须实际反映源代码中使用的实际参数的类型。
*/
@Override
public Type[] getGenericParameterTypes() {
return super.getGenericParameterTypes();
} /**
* 设置方法的访问权限
*/
@Override
@CallerSensitive
public void setAccessible(boolean flag) {
AccessibleObject.checkPermission();
if (flag) {
checkCanSetAccessible(Reflection.getCallerClass());
}
setAccessible0(flag);
} /**
* 执行目标对象 obj 的此方法,并传入指定的参数
*/
@CallerSensitive
@ForceInline // to ensure Reflection.getCallerClass optimization
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
if (!override) {
final Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz,
Modifier.isStatic(modifiers) ? null : obj.getClass(),
modifiers);
}
MethodAccessor ma = methodAccessor; // read volatile
if (ma == null) {
ma = acquireMethodAccessor();
}
return ma.invoke(obj, args);
}

Field

  • 字段说明
/**
* Field 对象提供类或接口上单个属性的信息,以及对它的动态访问权限,属性可以是类(静态)属性和实例(非静态)属性。
* Field 允许在执行 get 或 set 访问操作期间进行扩展转换;但如果要进行收缩转换,则会抛出 IllegalArgumentException。
* @since 1.1
*/
public final class Field extends AccessibleObject implements Member {
// 此属性所在类的 Class 对象
private Class<?> clazz;
// 属性存储的内存槽
private int slot;
// 属性的名称
private String name;
// 属性的类型
private Class<?> type;
// 属性的访问修饰符
private int modifiers;
// 用于支持泛型和注解的签名
private transient String signature;
// generic info repository; lazily initialized
private transient FieldRepository genericInfo;
// 属性上的注解
private byte[] annotations;
// 缓存的属性访问器
private FieldAccessor fieldAccessor;
// 被重载的缓存的属性访问器
private FieldAccessor overrideFieldAccessor;
  • 常用方法
    /**
* 此字段所在的 Class 类型
*/
@Override
public Class<?> getDeclaringClass() {
return clazz;
} /**
* 此字段的访问修饰符
*/
public int getModifiers() {
return modifiers;
} /**
* 此字段的名称
*/
public String getName() {
return name;
} /**
* 读取此属性上指定类型的运行时注解
* @since 1.5
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return annotationClass.cast(declaredAnnotations().get(annotationClass));
} /**
* 读取此属性上声明的所有注解,忽略继承的注解
*/
public Annotation[] getDeclaredAnnotations() {
return AnnotationParser.toArray(declaredAnnotations());
} /**
* 读取此属性上指定类型的运行时注解,此注解可重复,包括直接的和继承的
* @since 1.8
*/
@Override
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
} /**
* 设置属性的访问权限
*/
@Override
@CallerSensitive
public void setAccessible(boolean flag) {
AccessibleObject.checkPermission();
if (flag) {
checkCanSetAccessible(Reflection.getCallerClass());
}
setAccessible0(flag);
} /**
* 返回此属性的声明类型。
* 如果 Type 是一个参数化类型,则返回的 Type 对象必须准确地反映源代码中使用的实际类型参数。
* @since 1.5
*/
public Type getGenericType() {
if (getGenericSignature() != null) {
return getGenericInfo().getGenericType();
}
else {
return getType();
}
} /**
* 读取目标对象 obj 的此属性值
* @throws IllegalArgumentException 目标对象未声明该属性
* @throws IllegalAccessException 该属性受访问限制
*/
@CallerSensitive
@ForceInline // to ensure Reflection.getCallerClass optimization
public Object get(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
if (!override) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, obj);
}
return getFieldAccessor(obj).get(obj);
} /**
* 设置目标对象 obj 的属性值为 value
*/
@CallerSensitive
@ForceInline // to ensure Reflection.getCallerClass optimization
public void set(Object obj, Object value)
throws IllegalArgumentException, IllegalAccessException
{
if (!override) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, obj);
}
getFieldAccessor(obj).set(obj, value);
}

最新文章

  1. 老版mapreduce跑streaming作业多路输出的方法
  2. android listview 异步加载图片并防止错位
  3. iOS启动过程
  4. 【BZOJ-2299】向量 裴蜀定理 + 最大公约数
  5. truncate
  6. IE, FireFox, Opera 浏览器支持CSS实现Alpha半透明的方法
  7. 谈谈JPA-04-JPA的常用API
  8. 《Code Complete》ch.26 代码调整技术
  9. DB2缓冲池、表空间
  10. Difference Between Vector and Deque in C++
  11. Session,Cookie,jsessionid,Url重写
  12. android 根据网络来获取外网ip地址及国家,地区的接口
  13. zoj 1134 - Strategic Game
  14. HashMap 源码详细分析(JDK1.8)
  15. iOS 模拟器“安装”app
  16. Tukey‘s test方法 异常值
  17. 如何更改linux文件的拥有者及用户组(chown和chgrp)
  18. 转转转!!java基础一些静态代码块等知识点
  19. C++模拟OC的多重自动释放池
  20. (转)openstack 资源查询常用 sql

热门文章

  1. kbmMWClientQuery判断一个字段是否修改?
  2. Web开发的分层结构与MVC模式
  3. netfilter/iptables全攻略
  4. Winfrom UI 美化 MetroModernUI库应用实例
  5. LOAD DATA INFILE &amp; mysqlimport
  6. JS转换/Date(-28800000)/格式
  7. C#静态变量 总结
  8. 字节转b kb mb gb 等
  9. 实用——pojo (实体类) 常用函数
  10. poj 2976 Dropping tests (最大化平均值:二分查找)