Constructor、Method、Field 源码阅读
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);
}
Constructor、Method、Field 源码阅读的更多相关文章
- Spring源码阅读笔记
前言 作为一个Java开发者,工作了几年后,越发觉力有点不从心了,技术的世界实在是太过于辽阔了,接触的东西越多,越感到前所未有的恐慌. 每天捣鼓这个捣鼓那个,结果回过头来,才发现这个也不通,那个也不精 ...
- Rpc框架dubbo-client(v2.6.3) 源码阅读(二)
接上一篇 dubbo-server 之后,再来看一下 dubbo-client 是如何工作的. dubbo提供者服务示例, 其结构是这样的!dubbo://192.168.11.6:20880/com ...
- 【原】AFNetworking源码阅读(四)
[原]AFNetworking源码阅读(四) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇还遗留了很多问题,包括AFURLSessionManagerTaskDe ...
- 【原】AFNetworking源码阅读(二)
[原]AFNetworking源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中我们在iOS Example代码中提到了AFHTTPSessionMa ...
- Three.js源码阅读笔记-5
Core::Ray 该类用来表示空间中的“射线”,主要用来进行碰撞检测. THREE.Ray = function ( origin, direction ) { this.origin = ( or ...
- Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)
我们到底能走多远系列(33) 扯淡: 各位: 命运就算颠沛流离 命运就算曲折离奇 命运就算恐吓着你做人没趣味 别流泪 心酸 更不应舍弃 ... 主题: Spring源码阅读还在继 ...
- Mina源码阅读笔记(四)—Mina的连接IoConnector2
接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...
- Apollo源码阅读笔记(二)
Apollo源码阅读笔记(二) 前面 分析了apollo配置设置到Spring的environment的过程,此文继续PropertySourcesProcessor.postProcessBeanF ...
- 【Dubbo源码阅读系列】服务暴露之远程暴露
引言 什么叫 远程暴露 ?试着想象着这么一种场景:假设我们新增了一台服务器 A,专门用于发送短信提示给指定用户.那么问题来了,我们的 Message 服务上线之后,应该如何告知调用方服务器,服务器 A ...
随机推荐
- laravel5.8 IoC 容器
网上 对容器的解释有很多,这里只是记录,搬运! 1.简单理解: 2019-10-10 11:24:09 解析 lavarel 容器 IoC 容器 作用 就是 “解耦” .“依赖注入(DI) IoC 容 ...
- (8)全志A64查看寄存器
1.查看寄存器0x01c20824寄存器的值: cd /sys/class/sunxi_dump echo 0x01c20824 > dump cat dump 例如: 2.都多个寄存器 ech ...
- 观察数组 -vue
1.vue中的被包装的观察数组能够触发视图更新 2.有push(),pop(),shift(),unshift(),splice(),sort(),reverse() 3.不能检测到下面数组变化: 1 ...
- php类点滴---访问修饰符public protected private
public可以被继承,可以外部访问(也就是实例化对象可以直接访问) protected受保护的,可以被子类继承,无法外部访问 private继承,外部访问都别想 <?phpclass coac ...
- mongodb批量处理
mongodb支持批量插入. 1.使用Java mongodb api 查看源码com.mongodb.MongoCollectionImpl,有两个方法 @Override public void ...
- 题解 【NOIP2013】转圈游戏
[NOIP2013]转圈游戏 Description n个小伙伴(编号从0到n-1)围坐一圈玩游戏.按照顺时针方向给n个位置编号,从0到n-1.最初,第0号小伙伴在第0号位置,第1号小伙伴在第1号位置 ...
- [人物存档]【AI少女】【捏脸数据】日式校服
点击下载(城通网盘):AISChaF_20191031221657757.png 点击下载(城通网盘):1572457456165c77.zip
- vue cli3 + cube-ui 配置rem踩坑记录
在install cube-ui时,选择了后编译,选中使用rem的时候会发现,怎么刷新页面的html字体被设置成了37.5px 感觉太大了,于是去寻找修改的办法,第一反应是webpack的配置,于是去 ...
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 概率与期望+高斯消元
这个还挺友好的,自己相对轻松能想出来~令 $f[i]$ 表示起点到点 $i$ 的期望次数,则 $ans[i]=f[i]\times \frac{p}{q}$ #include <cmath> ...
- JavaScript 实现文件下载并重命名
第一种是HTML官网中的方法<a href="/images/liang.jpg" download="文件名称">HTML5 中 a 标签提供了一 ...