sun.reflect.CallerSensitive注释是什么意思? [英] What does the sun.reflect.CallerSensitive annotation mean?

查看:4435
本文介绍了sun.reflect.CallerSensitive注释是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@CallerSensitive 注释上面的方法是什么意思?

What is implied by @CallerSensitive annotation above methods?

例如,注释存在于getClassLoader方法

For example,the annotation is present in getClassLoader method of Class

 @CallerSensitive
    public ClassLoader getClassLoader() {
    //
    }


推荐答案

根据JEP还此处),


调用者敏感的方法根据其直接调用者的
类改变其行为。它通过调用
sun.reflect.Reflection.getCallerClass 方法来发现其调用者的类。

A caller-sensitive method varies its behavior according to the class of its immediate caller. It discovers its caller’s class by invoking the sun.reflect.Reflection.getCallerClass method.

如果你看一下 Class#forName(String)的实现

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

,您会注意到它正在使用Reflection.getCallerClass () 。如果我们看看那个方法

, you notice that it is using Reflection.getCallerClass(). If we look at that method


返回调用此方法的方法的调用者的类,忽略与 java.lang.reflect.Method.invoke()及其实现。



@CallerSensitive
public static native Class getCallerClass();

问题似乎在这个JEP之前是,如果调用者敏感方法是通过反射调用而不是直接地,必须有一个复杂的过程来识别实际的调用类是什么。这是有问题的,如果方法通过反射调用。使用 @CallerSensitive 建议(并介绍)一个更简单的过程。

The problem, it seems, before this JEP, was that if the caller sensitive method was called through reflection instead of directly, there had to be a complex process to identify what the actual calling class was. This was problematic if the method was invoked through reflection. A simpler process was proposed (and introduced) with @CallerSensitive.

基本上, @CallerSensitive 注释由JVM使用

Basically, the @CallerSensitive annotation is used by the JVM


JVM将跟踪此注释, b $ b不变, sun.reflect.Reflection.getCallerClass 方法可以
只报告方法的调用者当方法标记为
时注释。

The JVM will track this annotation and, optionally, enforce the invariant that the sun.reflect.Reflection.getCallerClass method can only report the caller of a method when that method is marked with this annotation.

这篇关于sun.reflect.CallerSensitive注释是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆