AspectJ pointcut not working with external classes and LTW
I am trying to use AspectJ (which until yesterday I didn’t know) with LTW in order to understand how an existing framework works. In brief, I am interested at how framework's input XML files get parsed.
I have written the following aspect:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.CodeSignature;
public aspect UnitContextTrace {
static final void println(String s){ System.out.println(s); }
pointcut unitContextMethodsExec(): call(public * org.ivalidator.framework.test.UnitContext.* (..)) ||
call(public void org.ivalidator.repository..*.set* (..));
Object around(): unitContextMethodsExec() {
println("Intercepted message: " +
thisJoinPointStaticPart.getSignature().getName());
println("in class: " +
thisJoinPointStaticPart.getSignature().getDeclaringType().getName());
printParameters(thisJoinPoint);
println("Running original method: \n" );
Object result = proceed();
println(" result: " + result );
return result;
}
static private void printParameters(JoinPoint jp) {
println("Arguments: " );
Object[] args = jp.getArgs();
String[] names = ((CodeSignature)jp.getSignature()).getParameterNames();
Class[] types = ((CodeSignature)jp.getSignature()).getParameterTypes();
for (int i = 0; i < args.length; i++) {
println(" " + i + ". " + names[i] +
" : " + types[i].getName() +
" = " + args[i]);
}
}
}
This should debug both calls to methods of UnitContext
interface’s implementations and those to setXXX()
methods of any class belonging to org.ivalidator.repository.*
package.
I packaged my Aspect in its own jar using
ajc UnitContextTrace.aj -outxml -outjar aspectTrace.jar -extdirs "C:\aspectj1.6\lib\"
and I started the program (I use an ant script) passing -javaagent:${aspectj.home}/lib/aspectjweaver.jar
to the JVM.
The first part of the aspect (UnitContext
methods) works and I can see for example
Intercepted message: getAdapter…
But unfortunately there are no logs for calls to setXXX()
methods. The package org.ivalidator.repository.*
is part of ivalidator.jar
, which of course uses 3rd parties libraries stored in a lib folder. The structure is something like this:
Ivalidator.jar
/lib
Castor-0.9.7.jar
xercesImpl.jar
Using the debugger, I noticed that setXXX()
methods are invoked from external classes (belonging to castor-0.9.7.jar
, which in turn are invoked by xerces’ classes). To be more precise, the stack trace I can see in the debugger is this:
**ParameterXml.setName(String) line: 60 (I want to intercept this)**
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available
Method.invoke(Object, Object...) line: not available
FieldHandlerImpl.setValue(Object, Object) line: 501
UnmarshalHandler.processAttribute(String, String, String, XMLFieldDescriptor, XMLClassDescriptor, Object) line: 3028
UnmarshalHandler.processAttributes(AttributeSet, XMLClassDescriptor) line: 2702
UnmarshalHandler.startElement(String, String, AttributeSet) line: 2325
UnmarshalHandler.startElement(String, String, String, Attributes) line: 1388
SAXParser(AbstractSAXParser).startElement(QName, XMLAttributes, Augmentations) line: not available
SAXParser(AbstractXMLDocumentParser).emptyElement(QName, XMLAttributes, Augmentations) line: not available
XMLNSDocumentScannerImpl.scanStartElement() line: not available
XMLNSDocumentScannerImpl$NSContentDispatcher(XMLDocumentFragmentScannerImpl$FragmentContentDis开发者_开发技巧patcher).dispatch(boolean) line: not available
XMLNSDocumentScannerImpl(XMLDocumentFragmentScannerImpl).scanDocument(boolean) line: not available
XML11Configuration.parse(boolean) line: not available
XML11Configuration.parse(XMLInputSource) line: not available
SAXParser(XMLParser).parse(XMLInputSource) line: not available
SAXParser(AbstractSAXParser).parse(InputSource) line: not available
**DescriptorRepositoryXml(XmlObject).fromXml(InputSource) line: 341 (last call within ivalidator.jar)**
I wonder whether calls to external classes (external jars but also i.e. org.xml.sax.xmlreader
) may cause the problem?
I guess you do not want to intercept call()
, but execution()
. In that case it does not matter whether your third party classes are woven or not.
精彩评论