java 反射 安全_java学习-反射-安全性检查

对于"java.lang.reflect.AccessibleObject",其子类包含 field / executable(java8,其子类包含 method/constructor);

/**

* Set the {@code accessible} flag for this object to

* the indicated boolean value. A value of {@code true} indicates that

* the reflected object should suppress Java language access

* checking when it is used. A value of {@code false} indicates

* that the reflected object should enforce Java language access checks.

*

*

First, if there is a security manager, its

* {@code checkPermission} method is called with a

* {@code ReflectPermission("suppressAccessChecks")} permission.

*

*

A {@code SecurityException} is raised if {@code flag} is

* {@code true} but accessibility of this object may not be changed

* (for example, if this element object is a {@link Constructor} object for

* the class {@link java.lang.Class}).

*

*

A {@code SecurityException} is raised if this object is a {@link

* java.lang.reflect.Constructor} object for the class

* {@code java.lang.Class}, and {@code flag} is true.

*

* @param flag the new value for the {@code accessible} flag

* @throws SecurityException if the request is denied.

* @see SecurityManager#checkPermission

* @see java.lang.RuntimePermission

*/

public void setAccessible(boolean flag) throws SecurityException {

SecurityManager sm = System.getSecurityManager();

if (sm != null) sm.checkPermission(ACCESS_PERMISSION);

setAccessible0(this, flag);

}

对于反射中的 AccessibleObject#setAccessible,当值为true时,作用实际是有两点

取消安全性检查,提高整体执行效率

避免了"illegalAccess"异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值