我正在研究一个方面问题,需要知道它从哪里调用。目前我正在使用
new Throwable().getStackTrace();
访问此信息,但每个方面都需要几百微秒才能运行。
我看过SecurityManager,但似乎只能找到我的类名。
还有其他我错过的选择吗?
更新
JMH基准测试结果在我对@ apangin答案的评论中提及:
Benchmark Mode Cnt Score Error Units
MyBenchmark.javalangaccess13i avgt 100 2025.865 ± 8.133 ns/op
MyBenchmark.javalangaccess2i avgt 100 2648.598 ± 24.369 ns/op
MyBenchmark.throwable1 avgt 100 12706.978 ± 84.651 ns/op
基准代码:
@Benchmark
public StackTraceElement[] throwable1() {
return new Throwable().getStackTrace();
}
@SuppressWarnings("restriction")
@Benchmark
public static StackTraceElement javalangaccess2i() {
Exception e = new Exception();
return sun.misc.SharedSecrets.getJavaLangAccess().getStackTraceElement(e, 2);
}
@SuppressWarnings("restriction")
@Benchmark
public static StackTraceElement javalangaccess13i() {
Exception e = new Exception();
return sun.misc.SharedSecrets.getJavaLangAccess().getStackTraceElement(e, 13);
}
在戴尔XPS13 9343(i5-5200U @ 2.2GHz)上的Windows 10,JDK 1.8.0_112下运行测试
答案 0 :(得分:5)
不幸的是,Throwable.getStackTrace()
似乎是在纯Java 8中获取调用者帧的唯一可行选项。
但是,有一个JDK特定的技巧只能访问一个选定的堆栈帧
它使用非标准sun.misc.SharedSecrets
API。
public static StackTraceElement getCaller() {
Exception e = new Exception();
return sun.misc.SharedSecrets.getJavaLangAccess().getStackTraceElement(e, 2);
}
这里2是所需帧的索引。
这在最新的JDK 8之前工作正常,但在JDK 9中无法访问私有API。一个好消息是Java 9将具有新的标准Stack-Walking API。以下是在Java 9中如何做到这一点。
public static StackWalker.StackFrame getCaller() {
return StackWalker.getInstance(Collections.emptySet(), 3)
.walk(s -> s.skip(2).findFirst())
.orElse(null);
}
适用于旧版和旧版Java的替代选项是JVMTI GetStackTrace函数。它需要链接本机代码。
答案 1 :(得分:1)
你在谈论AspectJ。因此,您不需要任何反射,但可以使用板载AspectJ方法,例如thisEnclosingJoinPointStaticPart.getSignature()
与call()
切入点结合使用:
驱动程序应用程序:
package de.scrum_master.app;
public class Application {
private static final long NUM_LOOPS = 1000 * 1000;
public static void main(String[] args) {
Application application = new Application();
long startTime = System.nanoTime();
for (long i = 0; i < NUM_LOOPS; i++)
application.doSomething();
System.out.printf(
"%-40s | %8.3f ms%n",
"AspectJ thisEnclosingJoinPointStaticPart",
(System.nanoTime() - startTime) / 1.0e6
);
startTime = System.nanoTime();
for (long i = 0; i < NUM_LOOPS; i++)
application.doSomething2();
System.out.printf(
"%-40s | %8.3f ms%n",
"Throwable.getStackTrace",
(System.nanoTime() - startTime) / 1.0e6
);
startTime = System.nanoTime();
for (long i = 0; i < NUM_LOOPS; i++)
application.doSomething3();
System.out.printf(
"%-40s | %8.3f ms%n",
"SharedSecrets.getJavaLangAccess",
(System.nanoTime() - startTime) / 1.0e6
);
}
public void doSomething() {}
public void doSomething2() {}
public void doSomething3() {}
}
<强>方面:强>
package de.scrum_master.aspect;
import de.scrum_master.app.Application;
import sun.misc.SharedSecrets;
public aspect MyAspect {
before() : call(* Application.doSomething()) {
Object o = thisEnclosingJoinPointStaticPart.getSignature();
//System.out.println(o);
}
before() : call(* Application.doSomething2()) {
Object o = new Throwable().getStackTrace()[1];
//System.out.println(o);
}
before() : call(* Application.doSomething3()) {
Object o = SharedSecrets.getJavaLangAccess().getStackTraceElement(new Throwable(), 1);
//System.out.println(o);
}
}
控制台日志:
AspectJ thisEnclosingJoinPointStaticPart | 7,246 ms
Throwable.getStackTrace | 1852,895 ms
SharedSecrets.getJavaLangAccess | 1043,050 ms
如您所见,AspectJ比下一个最好的基于反射的方法快140倍。
顺便说一句,如果您取消注释方面中的打印语句,您会看到以下三种类型的输出:
void de.scrum_master.app.Application.main(String[])
de.scrum_master.app.Application.main(Application.java:16)
de.scrum_master.app.Application.main(Application.java:21)
享受!