Object around(): methodsOfInterest(){ nesting++; long stime=System.currentTimeMillis(); Object o = proceed(); long etime=System.currentTimeMillis(); nesting--; StringBuilder info = new StringBuilder(); for (int i=0;i<nesting;i++) { info.append(" "); } info.append(thisJoinPoint+" took "+(etime-stime)+"ms"); System.out.println(info.toString()); return o; } }
编译时织入
1 2 3 4 5 6 7 8
ajc WhereDoesTheTimeGo.aj Simple.java java Simple //输出 execution(void Simple.count(int, int)) took 2ms execution(void Simple.countFast(int)) took 4ms execution(void Simple.count(int, int)) took 5049ms execution(void Simple.countSlow(int)) took 5049ms execution(void Simple.main(String[])) took 5054ms
java -javaagent:<pathToAspectj>/lib/aspectjweaver.jar -classpath "code;timing.jar;<pathToAspectj>/lib/aspectjrt.jar" Simple //输出 [AppClassLoader@9fbe93] info AspectJ Weaver Version DEVELOPMENT built on Wednesday Feb 25, 2009 at 21:17:03 GMT [AppClassLoader@9fbe93] info register classloader sun.misc.Launcher$AppClassLoader@9fbe93 [AppClassLoader@9fbe93] info using configuration file:/C:/blog/timing.jar!/META-INF/aop-ajc.xml [AppClassLoader@9fbe93] info register aspect WhereDoesTheTimeGo [AppClassLoader@9fbe93] weaveinfo Join point 'method-execution(void Simple.main(java.lang.String[]))' in Type 'Simple' (Simple.java:3) advised by around advice from 'WhereDoesTheTimeGo' (WhereDoesTheTimeGo.java:7) [AppClassLoader@9fbe93] weaveinfo Join point 'method-execution(void Simple.countSlow(int))' in Type 'Simple' (Simple.java:8) advised by around advice from 'WhereDoesTheTimeGo' (WhereDoesTheTimeGo.java:7) [AppClassLoader@9fbe93] weaveinfo Join point 'method-execution(void Simple.countFast(int))' in Type 'Simple' (Simple.java:12) advised by around advice from 'WhereDoesTheTimeGo' (WhereDoesTheTimeGo.java:7) [AppClassLoader@9fbe93] weaveinfo Join point 'method-execution(void Simple.count(int, int))' in Type 'Simple' (Simple.java:16) advised by around advice from 'WhereDoesTheTimeGo' (WhereDoesTheTimeGo.java:7) [AppClassLoader@9fbe93] info processing reweavable type WhereDoesTheTimeGo: WhereDoesTheTimeGo.java execution(void Simple.count(int, int)) took 1ms execution(void Simple.countFast(int)) took 3ms execution(void Simple.count(int, int)) took 5005ms execution(void Simple.countSlow(int)) took 5005ms execution(void Simple.main(String[])) took 5016ms