aspecj访问java 11中的sun包



我使用的是java 11中软件包sun.security.validator.ValidatorException中的ValidatorException类。但我得到了以下错误:

AJC编译器错误:类型sun.security.validator.ValidatorException不可访问

在maven编译器中,我使用编译器参数修复了这个问题——add exports java.base/sun.security.validator=ALL-UNNAMED

但我不知道如何使用AspectJ插件。

maven编译器的pom.xml快照

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-exports</arg><arg>java.base/sun.security.validator=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>

快照我的pom.xml用于aspectj插件

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<source>11</source>
<target>11</target>
<complianceLevel>11</complianceLevel>
</configuration>
<executions>
<execution>
<id>compile-with-aspectj</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<weaveDirectories>
<weaveDirectory>${project.build.outputDirectory}/aspectJ-classes</weaveDirectory>
</weaveDirectories>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>11</version>
<scope>system</scope>
<systemPath>${project.basedir}/pom.xml</systemPath>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</plugin>

参见

https://github.com/eclipse-aspectj/aspectj/pull/146#issuecomment-1079883826

https://github.com/mojohaus/aspectj-maven-plugin/issues/139

https://github.com/eclipse-aspectj/aspectj/issues/145

使用所需的其他两个不同包的示例

import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;

但你明白了:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!--fixes:
[ERROR] exporting a package from system module java.base is not allowed with &#45;&#45;releas-->
<release combine.self="override"/>
<compilerArgs>
<arg>--add-exports</arg>
<arg>java.base/sun.security.x509=ALL-UNNAMED</arg>

<arg>--add-exports</arg>
<arg>java.base/sun.security.tools.keytool=ALL-UNNAMED</arg>
</compilerArgs>
<!-- this fixes: [142,6] error: cannot find symbol [ERROR]   symbol:   class sun.security.tools.keytool.CertAndKeyGen -->
<source>${java.source.version}</source>
<target>${java.target.version}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>

<!-- AspectJ Compile Time Weaving -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
<complianceLevel>${java.target.version}</complianceLevel>
<XnoInline>true</XnoInline>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<additionalCompilerArgs>
<arg>--add-exports</arg>
<arg>java.base/sun.security.x509=ALL-UNNAMED</arg>

<!-- fix for:                       [ERROR]   (package sun.security.tools.keytool is declared in module java.base, which does not export it)-->
<arg>--add-exports</arg>
<arg>java.base/sun.security.tools.keytool=ALL-UNNAMED</arg>
</additionalCompilerArgs>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

相关内容

最新更新