AspectJ可以通过sun.net。*软件包进行编织吗?

【字号: 日期:2024-02-26浏览:50作者:雯心
如何解决AspectJ可以通过sun.net。*软件包进行编织吗??

我尚未看到用于JRE(引导)类的加载时编织的成功设置。如果您出于调试目的需要此工具,则可以使用JRE类的构建时编织方法。

这个简短的代码片段将为您编织JREjar,并将编织的类放在单个输出jar中。它需要org.aspectj/aspectjtools作为依赖项。它还会跳过extJRE子文件夹中的jar,因为这些jar将包含一些重复的类,而创建包含重复文件的jar文件将导致错误。我还将跳过jfxswt.jar较新的JRE版本,因为它会因为缺少类而失败。

String aspectFileName = 'src/main/java/pckg/AspectName.aj';String jreLibPath = 'c:/Program Files/Java/jdk1.8.0_40/jre/lib';String outputJar = 'weavedjre.jar';List<String> jars = new ArrayList<>();File dir = new File(jreLibPath);File[] files = dir.listFiles();for (File file : files) { if (file.isFile() && file.getName().endsWith('.jar') && !file.getName().endsWith('jfxswt.jar')) {jars.add(file.getAbsolutePath()); }}List<String> ajcArgs = new ArrayList<>(Arrays.asList('-showweaveInfo'));for (String jar : jars) { ajcArgs.add('-inpath'); ajcArgs.add(jar);}ajcArgs.add(aspectFileName);ajcArgs.add('-outjar');ajcArgs.add(outputJar);org.aspectj.tools.ajc.Main.main(ajcArgs.toArray(new String[] {}));

然后,使用以下VM参数运行程序以使用编织的JRE类(在启动类路径之前):

-verbose:class -Xbootclasspath/p:path_to/weavedjre.jar

或在Eclipse启动配置中:

-verbose:class -Xbootclasspath/p:${resource_loc:/project_name/weavedjre.jar}

我也添加了类加载VM参数的详细日志记录,因此您可以查看从何处加载了哪个类。

解决方法

我正在使用AspectJ来拦截java.net.Socket调用。

我创建了一个非常简单的方面

after(): call(* java.net.Socket.connect(..)) { System.out.println('Connect intercepted!');}

和aop.xml

<aspectj> <aspects><aspect name='com.iggroup.lightstreamer.nwtp.SocketExceptionLoggingAspect'/> </aspects> <weaver options='-Xlint:ignore -Xset:weaveJavaxPackages=true -Xset:weaveJavaPackages=true'> </weaver></aspectj>

当调用堆栈是这样的时,我可以看到控制台输出:

java.lang.Exception at com.iggroup.lightstreamer.nwtp.SocketExceptionLoggingAspect.ajc$after$com_iggroup_lightstreamer_nwtp_SocketExceptionLoggingAspect$2$6e16217c(SocketExceptionLoggingAspect.aj:39) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:337) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:91) at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:596) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475) at com.iggroup.lightstreamer.nwtp.users.SsoRestClientImpl.lambda$0(SsoRestClientImpl.java:68) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

但是,当调用栈是这样时,什么都不会注销:

Caused by: java.net.ConnectException: Connection refused: connectat java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_65]at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.8.0_65]at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_65]at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_65]at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_65]at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_65]at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_65]at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_65]at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668) ~[na:1.8.0_65]at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173) ~[na:1.8.0_65]at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_65]at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) ~[na:1.8.0_65]at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) ~[na:1.8.0_65]at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:781) ~[na:1.8.0_65]at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647) ~[na:1.8.0_65]at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536) ~[na:1.8.0_65]at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) ~[na:1.8.0_65]at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) ~[na:1.8.0_65]at com.lightstreamer.ls_client.HttpProvider.connectAndGetAnswer(HttpProvider.java:244) ~[lightstreamer-se-client-2.5.2-1110.jar:na]

我想知道是否是因为sun.net.*某些安全管理器限制而导致软件包没有在加载时编织。

有谁知道如何使其与sun.net.*软件包一起使用?

更新1

我确认可以拦截该ls_client.HttpProvider.connectAndGetAnswer呼叫,但不能拦截该呼叫(sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream)。

是否sun.*有可能使用AspectJ来织?

相关文章: