sun.reflect.CallerSensitive注释是什么意思?

sun.reflect.CallerSensitive注释是什么意思?,第1张

sun.reflect.CallerSensitive注释是什么意思?

根据我在评论中链接到的JEP(也在此处),

调用方敏感方法根据其直接调用方的类来改变其行为。它通过调用该

sun.reflect.Reflection.getCallerClass
方法来发现其调用者的类。

如果你看执行

Class#forName(String)

@CallerSensitivepublic static Class<?> forName(String className) throws ClassNotFoundException {    return forName0(className, true,         ClassLoader.getClassLoader(Reflection.getCallerClass()));}

,您注意到它正在使用

Reflection.getCallerClass()
。如果我们看那种方法

返回调用此方法的方法的调用者的类,而忽略与之关联的框架

java.lang.reflect.Method.invoke()
及其实现。

@CallerSensitivepublic static native Class getCallerClass();

在此JEP之前,似乎存在的问题是,如果通过反射而不是直接调用调用方敏感方法,则必须有一个复杂的过程来标识实际的调用类。如果通过反射调用该方法,则会出现问题。提出(并介绍了)一个更简单的过程

@CallerSensitive

基本上,

@CallerSensitive
注释由JVM使用

JVM将跟踪此注释,并有选择地强制执行以下不变式:

sun.reflect.Reflection.getCallerClass
仅当该方法用此注释标记时,该方法才能报告该方法的调用者。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5021127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存