问题描述
我正在尝试在私有方法 java.net.AbstractSocketImpl.connectToAddress(..) 上设置切入点,并且我想使用加载时编织.这是我的测试代码:
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);
}
}
这是我的特权方面:
privileged aspect PrivAspect {
before() : call(* java.net.AbstractPlainSocketImpl.connectToAddress(..)) {
System.out.println("It works");
}
}
这是 META-INF/aop.xml
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.
有什么问题吗?
谢谢.
我知道调用了 java.net.AbstractPlainSocketImpl.connectToAddress(..)(我在 Eclipse 中调试了 Main).
I know that java.net.AbstractPlainSocketImpl.connectToAddress(..) is called (I debugged Main in Eclipse).
推荐答案
这看起来像是编译后编织,参考了 inpath (java.net.AbstractPlainSocketImpl) 上未指定的类.有必要明确指定 AspectJ 将编织"哪些 .class/.jar 文件.在此处查看更多详细信息:http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html
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
这篇关于AspectJ - JRE 类私有方法的加载时编织、特权方面和切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!