about the problem of Java, we usually get the results by using the stack as a keyword to the search engine. :)
the problem encountered this time is a bit interesting, and the procedure is very simple, so just ask a question.
what"s wrong with the following code? What does CallerSensitive annotation
mean?
import sun.reflect.Reflection;
public class CalleeApp {
public void call() {
Class<?> clazz = Reflection.getCallerClass();
System.out.println("Hello " + clazz);
}
}
public class CallerApp {
public static void main(String[] args) {
CalleeApp app = new CalleeApp();
Caller1 c1 = new Caller1();
c1.run(app);
}
static class Caller1 {
void run(CalleeApp calleeApp) {
if (calleeApp == null) {
throw new IllegalArgumentException("callee can not be null");
}
calleeApp.call();
}
}
}
wrong stack
Exception in thread "main" java.lang.InternalError: CallerSensitive annotation expected at frame 1
at sun.reflect.Reflection.getCallerClass(Native Method)
at com.honey.caller.CalleeApp.call(CalleeApp.java:8)
at com.honey.caller.CallerApp$Caller1.run(CallerApp.java:18)
at com.honey.caller.CallerApp.main(CallerApp.java:8)
Java version:
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-3-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)
< hr >
UPDATE 22:07:12, April 11, 2018
I think this article is a good answer to this question. Why not recommend calling the sun package and how to determine the caller through other methods