AspectJ. , , AspectJ, thisEnclosingJoinPointStaticPart.getSignature()
pointcut 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();
}
before() : call(* Application.doSomething2()) {
Object o = new Throwable().getStackTrace()[1];
}
before() : call(* Application.doSomething3()) {
Object o = SharedSecrets.getJavaLangAccess().getStackTraceElement(new Throwable(), 1);
}
}
:
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)
!