如何查看java调用栈_如何使用堆栈跟踪或反射找到方法的调用方?

本文测试了四种不同的Java方法来获取调用方类名的速度,包括反射、当前线程堆栈、异常堆栈和安全管理者。结果显示,自定义SecurityManager方法速度最快,而sun.reflect.Reflection.getCallerClass()在Java 7及以后版本中被禁用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

的注释中可以找到另一种解决方案这一增强请求..它使用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() {

return "Reflection";

}

}

/**

* Get a stack trace from the current thread

*/

private static class ThreadStackTraceMethod extends GetCallerClassNameMethod {

public String  getCallerClassName(int callStackDepth) {

return Thread.currentThread().getStackTrace()[callStackDepth].getClassName();

}

public String getMethodName() {

return "Current Thread StackTrace";

}

}

/**

* Get a stack trace from a new Throwable

*/

private static class ThrowableStackTraceMethod extends GetCallerClassNameMethod {

public String getCallerClassName(int callStackDepth) {

return new Throwable().getStackTrace()[callStackDepth].getClassName();

}

public String getMethodName() {

return "Throwable StackTrace";

}

}

/**

* Use the SecurityManager.getClassContext()

*/

private static class SecurityManagerMethod extends GetCallerClassNameMethod {

public String  getCallerClassName(int callStackDepth) {

return mySecurityManager.getCallerClassName(callStackDepth);

}

public String getMethodName() {

return "SecurityManager";

}

/**

* A custom security manager that exposes the getClassContext() information

*/

static class MySecurityManager extends SecurityManager {

public String getCallerClassName(int callStackDepth) {

return getClassContext()[callStackDepth].getName();

}

}

private final static MySecurityManager mySecurityManager =

new MySecurityManager();

}

/**

* Test all four methods

*/

public static void main(String[] args) {

testMethod(new ReflectionMethod());

testMethod(new ThreadStackTraceMethod());

testMethod(new ThrowableStackTraceMethod());

testMethod(new SecurityManagerMethod());

}

private static void testMethod(GetCallerClassNameMethod method) {

long startTime = System.nanoTime();

String className = null;

for (int i = 0; i 

className = method.getCallerClassName(2);

}

printElapsedTime(method.getMethodName(), startTime);

}

private static void printElapsedTime(String title, long startTime) {

System.out.println(title + ": " + ((double)(System.nanoTime() - startTime))/1000000 + " ms.");

}}

运行Java 1.6.0_17的2.4GHz Intel Core 2 Duo MacBook的输出示例:Reflection: 10.195 ms.Current Thread StackTrace: 5886.964 ms.Throwable StackTrace: 4700.073 ms.SecurityManager: 1046.804 ms.

内反射法是多比其他人都快。从新创建的堆栈跟踪Throwable比从电流中得到它更快Thread..以及在查找调用方类的非内部方法中,自定义。SecurityManager似乎是最快的。

更新

如李奥米指出本评论这个sun.reflect.Reflection.getCallerClass()方法默认情况下已在Java 7、UPDATE 40中禁用,并在Java 8中完全删除。阅读更多有关这方面的资料Java bug数据库中的这个问题.

更新2

如查姆比已经发现,甲骨文被迫退出改变移除sun.reflect.Reflection.getCallerClass()..Java 8中仍然可以使用它(但不推荐它)。

更新3

3年后:更新当前JVM的时间。> java -version

java version "1.8.0"Java(TM) SE Runtime Environment (build 1.8.0-b132)Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)>

java TestGetCallerClassNameReflection: 0.194s.Current Thread StackTrace: 3.887s.Throwable StackTrace: 3.173s.SecurityManager: 0.565s.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值