问题
I'm trying to set pointcut on private method java.net.AbstractSocketImpl.connectToAddress(..) and I want to use load-time weaving. This is my test code:
public class Main {
public static void main(String args[]) throws Throwable {
new java.net.Socket("localhost", 10111);
}
}
and this is my privileged aspect:
privileged aspect PrivAspect {
before() : call(* java.net.AbstractPlainSocketImpl.connectToAddress(..)) {
System.out.println("It works");
}
}
and this is META-INF/aop.xml
<aspectj>
<aspects>
<aspect name="PrivAspect"/>
</aspects>
<weaver options="-verbose -Xset:weaveJavaPackages=true"/>
</aspectj>
This is how I compile code:
$ javac Main.java
$ java -jar ../lib/aspectjtools-1.6.11.jar -source 6 -cp .:$CLASSPATH PrivAspect.aj
[warning] this affected type is not exposed to the weaver: java.net.AbstractPlainSocketImpl (needed for privileged access) [Xlint:typeNotExposedToWeaver]
/home/batto/work/ajtest/test/PrivAspect.aj:2 [warning] advice defined in PrivAspect has not been applied [Xlint:adviceDidNotMatch]
2 warnings
$ java -cp .:$CLASSPATH -javaagent:../lib/aspectjweaver-1.6.11.jar Main
The last line throws expected "connection refused" exception (port is closed) but advice is not invoked.
What's the problem?
Thank you.
EDIT: I know that java.net.AbstractPlainSocketImpl.connectToAddress(..) is called (I debugged Main in Eclipse).
回答1:
This seems like post compile-time weaving with reference to a class not specified on the inpath (java.net.AbstractPlainSocketImpl). It is necessary to specify explicitly which .class/.jar files are to be "woven" by AspectJ. See more details here: http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html
来源:https://stackoverflow.com/questions/6269097/aspectj-load-time-weaving-privileged-aspect-and-pointcut-on-private-method-of