赞
踩
Aspectj implements the functionality also using the asm tool.
As it provides neat aspect grammars and is supported extensively by the community, I would like to switch to it. Better than Btrace , right?
Now let us see the details (shown later).
Basically, there are two steps:
1) make aspect agent jar with the command ajc.
2) run it with aspectjweaver.jar as the javaagent, and with the aspectjrt.jar and agent jar as the classpath (also the path of the main class).
you can unzip the aspect agent jar with the command "jar xvf the aspect agent jar" and modify the aop-ajc.xml to modify options.
then use the command "jar cvfm time.jar META-INF/MANIFEST.MF META-INF/aop-ajc.xml Simple.class" to repack the aspect agent jar.
Detailed commands are attached as a screenshot.
[copy from others' blog]
Today I'm going to show a basic AspectJ load-time weaving setup and which configuration options can be used to debug a scenario that just doesn't seem to be working as expected.
Here is a trivial application:
public class Simple {
public static void main(String[]argv) {
countFast(1000);
countSlow(1000);
}
public static void countSlow(int value) {
count(value,5);
}
public static void countFast(int value) {
count(value,0);
}
private static void count(int value, int delay) {
for (int i=0;i<value;i++) {
try {Thread.sleep(delay);} catch (Exception e) {}
}
}
}
aspect WhereDoesTheTimeGo {
pointcut methodsOfInterest(): execution(* *(..)) &&
!within(WhereDoesTheTimeGo);
private int nesting = 0;
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;
}
}
ajc WhereDoesTheTimeGo.java 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
<aspectj> <aspects> <aspect name="WhereDoesTheTimeGo"/> </aspects> </aspectj>
ajc WhereDoesTheTimeGo.java -outxml -outjar timing.jar
aop-ajc.xml
(in a META-INF directory) and the class file for the compiled aspect.
jar -tvf timing.jar
55 Wed Feb 25 22:06:30 GMT 2009 META-INF/MANIFEST.MF
5149 Wed Feb 25 22:06:32 GMT 2009 WhereDoesTheTimeGo.class
85 Wed Feb 25 22:06:32 GMT 2009 META-INF/aop-ajc.xml
META-INF/aop-ajc.xml
(being in the META-INF folder is important!). That name is chosen for the generated one so it does not clash with (or overwrite) any
aop.xml
I might be working on separately that contains more advanced options. The agent is going to merge together the contents of all the xml config files it finds and use the combination to configure the weaver.
java -javaagent:<pathToAspectj>/lib/aspectjweaver.jar
-classpath "code;timing.jar;<pathToAspectj>/lib/aspectjrt.jar" Simple
execution(void Simple.count(int, int)) took 1ms
execution(void Simple.countFast(int)) took 6ms
execution(void Simple.count(int, int)) took 5114ms
execution(void Simple.countSlow(int)) took 5114ms
execution(void Simple.main(String[])) took 5121ms
aop-ajc.xml
file. Firstly I just want to know the basics about what AspectJ is up to. The xml file can have a weaver section where various options can be configured - many of which match the options that can be specified when calling the compiler on the command line. AspectJ will merge together any suitable aop xml files it discovers on the classpath in order to define the behaviour of a weaver, so I can either modify my existing
aop-ajc.xml
inside the jar or create another one just for trying out some options.
<aspectj> <aspects> <aspect name="WhereDoesTheTimeGo"/> </aspects> <weaver options="-verbose"/> </aspectj>
[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] info processing reweavable type WhereDoesTheTimeGo:
WhereDoesTheTimeGo.java
execution(void Simple.count(int, int)) took 0ms
execution(void Simple.countFast(int)) took 3ms
execution(void Simple.count(int, int)) took 5003ms
execution(void Simple.countSlow(int)) took 5003ms
execution(void Simple.main(String[])) took 5007ms
file:/C:/blog/timing.jar!/META-INF/aop-ajc.xml
) and then the messages tell me which aspects have been defined based on that configuration.
-showWeaveInfo
' exactly the same as I would on the command line.
<aspectj> <aspects> <aspect name="WhereDoesTheTimeGo"/> </aspects> <weaver options="-verbose -showWeaveInfo"/> </aspectj>
[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
<aspectj>
<aspects>
<aspect name="WhereDoesTheTimeGo"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<exclude within="Simple">
</weaver>
</aspectj>
<aspectj>
<aspects>
<aspect name="WhereDoesTheTimeGo"/>
</aspects>
<weaver options="-debug">
<exclude within="Simple">
</weaver>
</aspectj>
[AppClassLoader@9fbe93] debug not weaving 'Simple'
Simple
- and that is due to the include/exclude constraints specified. Finally I will remove my exclude section from the xml and re-run:
[AppClassLoader@9fbe93] debug weaving 'Simple'
[AppClassLoader@9fbe93] debug generating class 'Simple$AjcClosure1'
[AppClassLoader@9fbe93] debug generating class 'Simple$AjcClosure3'
[AppClassLoader@9fbe93] debug generating class 'Simple$AjcClosure5'
[AppClassLoader@9fbe93] debug generating class 'Simple$AjcClosure7'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.Factory'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.SourceLocation'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.MethodSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.MethodSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.CodeSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.MemberSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.SignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.ConstructorSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.ConstructorSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.UnlockSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.UnlockSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.LockSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.LockSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.AdviceSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.AdviceSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.InitializerSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.InitializerSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.CatchClauseSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.CatchClauseSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.FieldSignatureImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.reflect.FieldSignature'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.JoinPoint'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.SignatureImpl$Cache'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.JoinPointImpl$StaticPartImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.SourceLocationImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.JoinPointImpl'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.ProceedingJoinPoint'
[AppClassLoader@9fbe93] debug weaving 'WhereDoesTheTimeGo'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.NoAspectBoundException'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.internal.Conversions'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.StringMaker'
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.runtime.reflect.SignatureImpl$CacheImpl'
execution(void Simple.count(int, int)) took 1ms
execution(void Simple.countFast(int)) took 5ms
execution(void Simple.count(int, int)) took 5001ms
execution(void Simple.countSlow(int)) took 5002ms
execution(void Simple.main(String[])) took 5008ms
[AppClassLoader@9fbe93] debug cannot weave 'org.aspectj.lang.ProceedingJoinPoint'
[AppClassLoader@9fbe93] debug weaving 'Simple'
http://www.eclipse.org/aspectj/doc/released/devguide/ltw-configuration.html
https://bugs.eclipse.org/bugs/show_bug.cgi?id=149261#c11
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。