In un metodo statico (Annotated con @CallerSensitive
) cerco di ottenere il nome della classe chiamante:java.lang.InternalError: CallerSensitive annotazione atteso al frame 1
@CallerSensitive
public static void someMethod() {
String name = sun.reflect.Reflection.getCallerClass().getName();
...
}
ottengo l'errore:
java.lang.InternalError: CallerSensitive annotation expected at frame 1
Cosa c'è di sbagliato qui?
Riferimenti
UPDATE
Sto usando Java 8 (U25) e il metodo getCallerClass()
è non deprecato (getCallerClass(int)
è obsoleto) come visibile caso di smontaggio del bytecode:
$ /usr/lib/jvm/java-8-oracle/bin/javap -cp /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar -verbose sun.reflect.Reflection > bytecode
uscita (solo linee rilevanti sono mostrate)
Classfile jar:file:/usr/lib/jvm/jdk1.8.0_25/jre/lib/rt.jar!/sun/reflect/Reflection.class
Last modified Sep 17, 2014; size 6476 bytes
Compiled from "Reflection.java"
public class sun.reflect.Reflection
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#78 = Utf8 Lsun/reflect/CallerSensitive;
#80 = Utf8 Deprecated
#82 = Utf8 Ljava/lang/Deprecated;
{
public sun.reflect.Reflection();
descriptor:()V
flags: ACC_PUBLIC
public static native java.lang.Class<?> getCallerClass();
descriptor:()Ljava/lang/Class;
flags: ACC_PUBLIC, ACC_STATIC, ACC_NATIVE
Signature: #76 //()Ljava/lang/Class<*>;
RuntimeVisibleAnnotations:
0: #78()
public static native java.lang.Class<?> getCallerClass(int);
descriptor: (I)Ljava/lang/Class;
flags: ACC_PUBLIC, ACC_STATIC, ACC_NATIVE
Deprecated: true
Signature: #81 // (I)Ljava/lang/Class<*>;
RuntimeVisibleAnnotations:
0: #82()
quello java stai usando? Secondo i documenti che hai collegato, getCallerClass() è stato deprecato e rimosso. –
Grazie! Vedere la mia domanda aggiornata – rmuller