public final class ReflectiveCallerClassUtility extends Object
Classof the caller of a particular method. When it is not available, a
SecurityManageris the second-best option. When this is also not possible, the
Thread.getStackTrace()must be used, and its
Stringclass name converted to a
Classusing the slow
getCallerClass(int)method has been removed from Oracle/OpenJDK and is no longer usable. A back-port of the feature that resulted in this change was made in Java 7u25, but the
getCallerClass(int)was left around for that version and deprecated, with the intention of being removed in 7u40. By coincidence, the change actually broke
getCallerClass(int)(the return value was inadvertently offset by 1 stack frame). This was actually a good thing, because it made the hundreds of libraries and frameworks relying on this method aware of what the JDK developers were up to.
getCallerClass(int)and keep its existing behavior for the rest of Java 7. However, the method will still not be available in Java 8, and so backup options must be used. This class:
getCallerClass(int)the traditional way when possible.
getCallerClass(int)with an adjusted offset in Oracle/OpenJDK 7u25.
getCallerClass(int)develops in Java 8, this class is very likely to change or even go away.
|Modifier and Type||Method and Description|
public static boolean isSupported()
getCallerClass(int)can be used on this JVM.
trueif it can be used. If
getCaller(int)should not be called. Use a backup mechanism instead.
getCallerClass(int), compensating for the additional frame on the stack, and compensating for the Java 7u25 bug if necessary. You should check with
isSupported()before using this method.
depth- The depth of the caller to retrieve.
getCallerClass(int)is not supported.
Copyright © 1999-2014 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.