sun.reflect.Reflection.getCallerClass alternative
I hava code
String callerClass = sun.reflect.Reflection.getCallerClass().getName()
in my project ,while I changed my jdk to 1.8 ,the code throws Exception:
Exception in thread "main" java.lang.InternalError: CallerSensitive annotation expected at frame 1
There are two ways to replace Reflection.getCallerClass()
StackTraceElement[] elements = new Throwable().getStackTrace();
String callerClass = elements[1].getClassName();
or
StackTraceElement[] elements = Thread.currentThread().getStackTrace()
String callerClass = elements[1].getClassName();
good luck
As discussed in the comments above. I came to the conclusion to use SharedSecrets.getJavaLangAccess()
as explained above in short term, but remove the dependency on sun.*
package entirely as a long term solution.
Basically I am changing my requirement itself so that it does not require getCallerClass
functionality.