的注释中可以找到另一种解决方案这一增强请求..它使用getClassContext()习惯的方法SecurityManager似乎比堆栈跟踪方法更快。
下面的程序测试不同建议方法的速度(最有趣的部分在内部类中)SecurityManagerMethod):/**
* Test the speed of various methods for getting the caller class name
*/public class TestGetCallerClassName {
/**
* Abstract class for testing different methods of getting the caller class name
*/
private static abstract class GetCallerClassNameMethod {
public abstract String getCallerClassName(int callStackDepth);
public abstract String getMethodName();
}
/**
* Uses the internal Reflection class
*/
private static class ReflectionMethod extends GetCallerClassNameMethod {
public String getCallerClassName(int callStackDepth) {
return sun.reflect.Reflection.getCallerClass(callStackDepth).getName();
}
public String getMethodName() {