These sun.* Packages were never part of the official API and were not guaranteed to be present even in the JVM prior to Java 9. Be prepared for them to completely disappear in the future, and cannot even be restored using some options. Fortunately, there is an official API covering this functionality, eliminating the need for unofficial APIs.
Get an immediate caller class
Class<?> c = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE) .getCallerClass();
Get the nth caller on the stack (for example, the third, as in your example):
Class<?> c = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(StackWalker.StackFrame::getDeclaringClass).skip(3).findFirst().orElse(null));
source share