java基础源码 (5)--reflect包-AccessibleObject类
学习参考博客:https://blog.csdn.net/benjaminzhang666/article/details/9664585
AccessibleObject类基本作用
1。将反射的对象标记为在使用时取消默认java语言访问控制检查的能力
2。在反射对象中设置accessible(翻译:无障碍)标志允许具有足够的特权
/**
* The AccessibleObject class is the base class for Field, Method and
* Constructor objects. It provides the ability to flag a reflected
* object as suppressing default Java language access control checks
* when it is used. The access checks--for public, default (package)
* access, protected, and private members--are performed when Fields,
* Methods or Constructors are used to set or get fields, to invoke
* methods, or to create and initialize new instances of classes,
* respectively.
AccessibleObject类是Field,Method和Constructor对象的父类,它提供了将反射对象标记
为在使用它时抑制默认java语言访问控制检查的功能。当使用Fields,Methods或Constructor来
设置或获取字段,调用方法,或创建和初始化新的类实例时,执行访问检查(对于public,默认(包)访问
,受保护和私有成员)
*
* <p>Setting the {@code accessible} flag in a reflected object
* permits sophisticated applications with sufficient privilege, such
* as Java Object Serialization or other persistence mechanisms, to
* manipulate objects in a manner that would normally be prohibited.
在反射对象中设置accessible标志允许具有足够权限的复杂应用程序(如java对象序列化
或其他持久性机制)以通常被禁止的方式操纵对象
* <p>By default, a reflected object is <em>not</em> accessible.
默认情况下,反射对象不可访问
*/
public class AccessibleObject implements AnnotatedElement { /**
* The Permission object that is used to check whether a client
* has sufficient privilege to defeat Java language access
* control checks.
Permission对象,用于检查客户端是否具有足够的权限来阻止java语言访问控制检查
*/
static final private java.security.Permission ACCESS_PERMISSION =
new ReflectPermission("suppressAccessChecks"); /**
* Convenience method to set the {@code accessible} flag for an
* array of objects with a single security check (for efficiency).
使用单一安全检查来设置对象数组的可访问标志的一个方便的方法(为了效率)*/
public static void setAccessible(AccessibleObject[] array, boolean flag)
throws SecurityException {
SecurityManager sm = System.getSecurityManager();//获取系统安全的接口
if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
for (int i = 0; i < array.length; i++) {
setAccessible0(array[i], flag);
}
} /**
将对象的可访问标志设置为指示的布尔值*/
public void setAccessible(boolean flag) throws SecurityException {
SecurityManager sm = System.getSecurityManager();
if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
setAccessible0(this, flag);
} /* Check that you aren't exposing java.lang.Class.<init> or sensitive
fields in java.lang.Class. */
private static void setAccessible0(AccessibleObject obj, boolean flag)
throws SecurityException
{
if (obj instanceof Constructor && flag == true) {
Constructor<?> c = (Constructor<?>)obj;
if (c.getDeclaringClass() == Class.class) {
throw new SecurityException("Cannot make a java.lang.Class" +
" constructor accessible");
}
}
obj.override = flag;
} /**
获取此对象的accessible标志的值(布尔类型)*/
public boolean isAccessible() {
return override;
} /**
* Constructor: only used by the Java Virtual Machine.
构造函数,仅有java虚拟机使用
*/
protected AccessibleObject() {} // Indicates whether language-level access checks are overridden
// by this object. Initializes to "false". This field is used by
// Field, Method, and Constructor.
通过这个对象,指示是否覆盖语言级别访问检查权限,初始化为false,该字段用于Field,
Method和Constructor
// NOTE: for security purposes, this field must not be visible
// outside this package.
出于安全考虑,此字段不得显示出包外
boolean override; // Reflection factory used by subclasses for creating field,
// method, and constructor accessors. Note that this is called
// very early in the bootstrapping process.
子类用于创建字段,方法和构造函数的反射工厂
static final ReflectionFactory reflectionFactory =
AccessController.doPrivileged(
new sun.reflect.ReflectionFactory.GetReflectionFactoryAction()); /**
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
throw new AssertionError("All subclasses should override this method");
} /**
* {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return AnnotatedElement.super.isAnnotationPresent(annotationClass);
} /**
* @throws NullPointerException {@inheritDoc}
* @since 1.8
*/
@Override
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
throw new AssertionError("All subclasses should override this method");
} /**
* @since 1.5
*/
public Annotation[] getAnnotations() {
return getDeclaredAnnotations();
} /**
* @throws NullPointerException {@inheritDoc}
* @since 1.8
*/
@Override
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
// Only annotations on classes are inherited, for all other
// objects getDeclaredAnnotation is the same as
// getAnnotation.
return getAnnotation(annotationClass);
} /**
* @throws NullPointerException {@inheritDoc}
* @since 1.8
*/
@Override
public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
// Only annotations on classes are inherited, for all other
// objects getDeclaredAnnotationsByType is the same as
// getAnnotationsByType.
return getAnnotationsByType(annotationClass);
} /**
* @since 1.5
*/
public Annotation[] getDeclaredAnnotations() {
throw new AssertionError("All subclasses should override this method");
} // Shared access checking logic. // For non-public members or members in package-private classes,
// it is necessary to perform somewhat expensive security checks.
// If the security check succeeds for a given class, it will
// always succeed (it is not affected by the granting or revoking
// of permissions); we speed up the check in the common case by
// remembering the last Class for which the check succeeded.
//
// The simple security check for Constructor is to see if
// the caller has already been seen, verified, and cached.
// (See also Class.newInstance(), which uses a similar method.)
//
// A more complicated security check cache is needed for Method and Field
// The cache can be either null (empty cache), a 2-array of {caller,target},
// or a caller (with target implicitly equal to this.clazz).
// In the 2-array case, the target is always different from the clazz.
volatile Object securityCheckCache; void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers)
throws IllegalAccessException
{
if (caller == clazz) { // quick check
return; // ACCESS IS OK
}
Object cache = securityCheckCache; // read volatile
Class<?> targetClass = clazz;
if (obj != null
&& Modifier.isProtected(modifiers)
&& ((targetClass = obj.getClass()) != clazz)) {
// Must match a 2-list of { caller, targetClass }.
if (cache instanceof Class[]) {
Class<?>[] cache2 = (Class<?>[]) cache;
if (cache2[1] == targetClass &&
cache2[0] == caller) {
return; // ACCESS IS OK
}
// (Test cache[1] first since range check for [1]
// subsumes range check for [0].)
}
} else if (cache == caller) {
// Non-protected case (or obj.class == this.clazz).
return; // ACCESS IS OK
} // If no return, fall through to the slow path.
slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass);
} // Keep all this slow stuff out of line:
void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers,
Class<?> targetClass)
throws IllegalAccessException
{
Reflection.ensureMemberAccess(caller, clazz, obj, modifiers); // Success: Update the cache.
Object cache = ((targetClass == clazz)
? caller
: new Class<?>[] { caller, targetClass }); // Note: The two cache elements are not volatile,
// but they are effectively final. The Java memory model
// guarantees that the initializing stores for the cache
// elements will occur before the volatile write.
securityCheckCache = cache; // write volatile
}
}
java基础源码 (5)--reflect包-AccessibleObject类的更多相关文章
- java基础源码 (4)--reflect包-AnnotatedElement接口
接口:AnnotatedElement * Represents an annotated element of the program currently running in this * VM. ...
- java基础源码 (6)--ArrayListt类
原作出处:https://www.cnblogs.com/leesf456/p/5308358.html 简介: 1.ArrayList是一个数组队列,相当于动态数组.与java中的数组相比,它的容量 ...
- java基础源码 (1)--String类
这个是String类上面的注释,我用谷歌翻译翻译的,虽然有点语法上的问题,但是大概都可以翻译出来 /** * The {@code String} class represents character ...
- java基础源码 (3)--Annotation(注解)
借鉴博客地址:https://www.cnblogs.com/skywang12345/p/3344137.html /** * The common interface extended by al ...
- java基础源码 (2)--StringBuilder类
Serializable(接口): 是一个IO的序列化接口,实现了这个接口,就代表这个类可以序列化或者反序列化,该接口没有方法或者字段,仅用于标识可串行话的语义. Appendable(接口): /* ...
- 自学Java HashMap源码
自学Java HashMap源码 参考:http://zhangshixi.iteye.com/blog/672697 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提 ...
- Java集合源码分析(三)LinkedList
LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...
- Java集合源码学习(一)集合框架概览
>>集合框架 Java集合框架包含了大部分Java开发中用到的数据结构,主要包括List列表.Set集合.Map映射.迭代器(Iterator.Enumeration).工具类(Array ...
- 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码
转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...
随机推荐
- 有关vector元素的取地址
1--原则上,最好不要对vector的元素取地址,除非所有的vector元素已经填充完毕,这样vector的元素不会发生位置移动,地址才不会变,这样才能确保取得的地址的有效性.PS:即使在可以用已经分 ...
- Python学习第九课——匿名函数
匿名函数 # 匿名函数 func = lambda x: x + 1 # x表示参数 x+1表示处理逻辑 print(func(10)) # 输出结果为11 # 例:如何将name="han ...
- LATTICE 编程烧录器HW-USBN-2B使用说明
HW-USBN-2B说明文档 1. 引脚定义 编程引脚 名称 编程设备引脚类型 描述 VCC 编程电压 输入 连接VCC到目标设备,典型的ICC=10Ma.板子设计必须考虑VCC的电流供应 ...
- C# 自定义控件容器,设计时可添加控件
本分步指南介绍在将 UserControl 放在 Windows 窗体上之后,如何将 UserControl 对象用作设计时控件容器.可能会有这样的情况:您想将一个控件拖到 UserControl 中 ...
- Jmeter - Linux 下面执行jmeter-server的时候出现:An error occurred: Cannot start. localhost is a loopback address.错误
Jmeter - Linux 下面执行jmeter-server的时候出现:An error occurred: Cannot start. localhost is a loopback addre ...
- eclipse console 查看全部的输出
参考:https://blog.csdn.net/thatluck/article/details/52080736
- PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟
1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...
- 第3节 sqoop:5、实现数据的控制导入
导入表数据子集 我们可以导入表的使用Sqoop导入工具,"where"子句的一个子集.它执行在各自的数据库服务器相应的SQL查询,并将结果存储在HDFS的目标目录. where子句 ...
- Timer(阿里CTF)
下载文件之后发现是.apk类型文件,于是百度搜索了一下推荐再电脑上安装安卓模拟器,都尝试了一下逍遥安卓是真的不好用,小白觉得还是BlueStacks好用一些,下载之后去安装打开就看到了, 什么都没有发 ...
- Ubuntu 更新源 内核升级
cat /etc/apt/sources.listdeb http://mirrors.sohu.com/ubuntu/ precise main restricted universe multiv ...