多个匹配建议的 Aspectj 异常处理
Aspectj exceptions handling on multiple matching advices
我有 2 个方面应用于同一方法。
当方法正确执行时,我没有问题,一切正常,两个方面都按预期工作。
问题是当方法抛出异常时。在这些情况下,第一个方面会重新正确抛出异常,但第二个方面会生成空指针异常。
我能够在单独项目的单元测试中重现隔离案例的问题。
这些是方面(实际上我删除了所有逻辑,目前他们什么都不做):
@Aspect
public class LogContextConstantAspect {
@Around("execution(* *(..)) && @annotation(logContextConstant)")
public Object aroundMethod(ProceedingJoinPoint joinPoint, LogContextConstant logContextConstant) throws Throwable {
try {
Object res = joinPoint.proceed();
return res;
} catch (Throwable e) {
throw e;
}
}
}
和
@Aspect
public class LogExecutionTimeAspect {
@Around("execution(* *(..)) && @annotation(logExecutionTime)")
public Object around(ProceedingJoinPoint joinPoint, LogExecutionTime logExecutionTime) throws Throwable {
try {
Object res = joinPoint.proceed();
return res;
} catch (Throwable e) {
throw e;
}
}
}
虽然这些是我实现的 2 个自定义注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionTime {
String paramKey() default "execution_time";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface LogContextConstant {
String name();
String value();
}
然后我用下面的方法创建了一个简单的class
public class AspectSimple {
public int execute() {
System.out.println("ok");
return 1;
}
public int failSimple() throws CustomException {
throw new CustomException("ko");
}
@LogExecutionTime
public int failWithAspect1() throws CustomException {
throw new CustomException("ko");
}
@LogContextConstant(name="test", value = "test")
public int failWithAspect2() throws CustomException {
throw new CustomException("ko");
}
@LogExecutionTime
@LogContextConstant(name="test", value = "test")
public int executeWithAspect() {
return 1;
}
@LogExecutionTime
@LogContextConstant(name="test", value = "test")
public int failWithAspect3() throws CustomException {
throw new CustomException("ko");
}
}
最后是这个单元测试
public class TestSample {
static AspectSimple as = null;
@BeforeAll
public static void setup() {
as = new AspectSimple();
}
@Test
public void test1() {
int res = as.execute();
assertEquals(1, res);
}
@Test
public void test2() {
int res = as.executeWithAspect();
assertEquals(1, res);
}
@Test
public void test3() {
try {
int res = as.failSimple();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
@Test
public void test4() {
try {
int res = as.failWithAspect1();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
@Test
public void test5() {
try {
int res = as.failWithAspect2();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
@Test
public void test6() {
try {
int res = as.failWithAspect3();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
}
所有测试 运行 正确,只有最后一个 (test6) 失败。
应用程序 运行 java 8,aspectj 1.9.4 和 junit 5。
这里是完整的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.pivimarco.samples.aspects</groupId>
<artifactId>aspect-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
org.baeldung.executable.ExecutableMavenJar
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
<properties>
<excludeTags>slow</excludeTags>
</properties>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</execution>
</executions>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
<properties>
<excludeTags>slow</excludeTags>
</properties>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<configuration>
<source>1.8</source>
<target>1.8</target>
<proc>none</proc>
<complianceLevel>1.8</complianceLevel>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果我只应用一个方面,CustomException 将作为方面抛出,
但是当这两个方面都应用时,我得到了一个空指针异常。
我也尝试过使用 DeclarePrecedence 注释来声明方面的优先级,但没有成功
@DeclarePrecedence("it.pivimarco.samples.aspects.LogContextConstantAspect,it.pivimarco.samples.aspects.LogExecutionTimeAspect")
这是 NPE 的堆栈跟踪
java.lang.NullPointerException
at it.pivimarco.samples.aspects.AspectSimple.failWithAspect3_aroundBody10(AspectSimple.java:35)
at it.pivimarco.samples.aspects.AspectSimple$AjcClosure11.run(AspectSimple.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:170)
at it.pivimarco.samples.aspects.LogContextConstantAspect.aroundMethod(LogContextConstantAspect.java:18)
at it.pivimarco.samples.aspects.AspectSimple.failWithAspect3(AspectSimple.java:35)
at it.pivimarco.samples.aspects.TestSample.test6(TestSample.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
我认为您在 AspectJ 编译器(版本 1.9.3、1.9.4)中发现了一个错误,该错误在版本 1.9.2 中没有出现,正如我在评论中所说的那样。我刚刚代表您创建了 AspectJ bug ticket #552687。请在那里查看进一步的更新。现在您可以降级到 1.9.2 并继续工作。
更新: 带有错误修复的 AspectJ 1.9.5 已发布。请再试一遍。我的复试成功了。
我有 2 个方面应用于同一方法。 当方法正确执行时,我没有问题,一切正常,两个方面都按预期工作。 问题是当方法抛出异常时。在这些情况下,第一个方面会重新正确抛出异常,但第二个方面会生成空指针异常。 我能够在单独项目的单元测试中重现隔离案例的问题。 这些是方面(实际上我删除了所有逻辑,目前他们什么都不做):
@Aspect
public class LogContextConstantAspect {
@Around("execution(* *(..)) && @annotation(logContextConstant)")
public Object aroundMethod(ProceedingJoinPoint joinPoint, LogContextConstant logContextConstant) throws Throwable {
try {
Object res = joinPoint.proceed();
return res;
} catch (Throwable e) {
throw e;
}
}
}
和
@Aspect
public class LogExecutionTimeAspect {
@Around("execution(* *(..)) && @annotation(logExecutionTime)")
public Object around(ProceedingJoinPoint joinPoint, LogExecutionTime logExecutionTime) throws Throwable {
try {
Object res = joinPoint.proceed();
return res;
} catch (Throwable e) {
throw e;
}
}
}
虽然这些是我实现的 2 个自定义注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionTime {
String paramKey() default "execution_time";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface LogContextConstant {
String name();
String value();
}
然后我用下面的方法创建了一个简单的class
public class AspectSimple {
public int execute() {
System.out.println("ok");
return 1;
}
public int failSimple() throws CustomException {
throw new CustomException("ko");
}
@LogExecutionTime
public int failWithAspect1() throws CustomException {
throw new CustomException("ko");
}
@LogContextConstant(name="test", value = "test")
public int failWithAspect2() throws CustomException {
throw new CustomException("ko");
}
@LogExecutionTime
@LogContextConstant(name="test", value = "test")
public int executeWithAspect() {
return 1;
}
@LogExecutionTime
@LogContextConstant(name="test", value = "test")
public int failWithAspect3() throws CustomException {
throw new CustomException("ko");
}
}
最后是这个单元测试
public class TestSample {
static AspectSimple as = null;
@BeforeAll
public static void setup() {
as = new AspectSimple();
}
@Test
public void test1() {
int res = as.execute();
assertEquals(1, res);
}
@Test
public void test2() {
int res = as.executeWithAspect();
assertEquals(1, res);
}
@Test
public void test3() {
try {
int res = as.failSimple();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
@Test
public void test4() {
try {
int res = as.failWithAspect1();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
@Test
public void test5() {
try {
int res = as.failWithAspect2();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
@Test
public void test6() {
try {
int res = as.failWithAspect3();
} catch (CustomException e) {
assertNotNull(e);
} catch (Exception e) {
fail();
}
}
}
所有测试 运行 正确,只有最后一个 (test6) 失败。
应用程序 运行 java 8,aspectj 1.9.4 和 junit 5。 这里是完整的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.pivimarco.samples.aspects</groupId>
<artifactId>aspect-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
org.baeldung.executable.ExecutableMavenJar
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
<properties>
<excludeTags>slow</excludeTags>
</properties>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</execution>
</executions>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
<properties>
<excludeTags>slow</excludeTags>
</properties>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<configuration>
<source>1.8</source>
<target>1.8</target>
<proc>none</proc>
<complianceLevel>1.8</complianceLevel>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果我只应用一个方面,CustomException 将作为方面抛出, 但是当这两个方面都应用时,我得到了一个空指针异常。
我也尝试过使用 DeclarePrecedence 注释来声明方面的优先级,但没有成功
@DeclarePrecedence("it.pivimarco.samples.aspects.LogContextConstantAspect,it.pivimarco.samples.aspects.LogExecutionTimeAspect")
这是 NPE 的堆栈跟踪
java.lang.NullPointerException
at it.pivimarco.samples.aspects.AspectSimple.failWithAspect3_aroundBody10(AspectSimple.java:35)
at it.pivimarco.samples.aspects.AspectSimple$AjcClosure11.run(AspectSimple.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:170)
at it.pivimarco.samples.aspects.LogContextConstantAspect.aroundMethod(LogContextConstantAspect.java:18)
at it.pivimarco.samples.aspects.AspectSimple.failWithAspect3(AspectSimple.java:35)
at it.pivimarco.samples.aspects.TestSample.test6(TestSample.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
我认为您在 AspectJ 编译器(版本 1.9.3、1.9.4)中发现了一个错误,该错误在版本 1.9.2 中没有出现,正如我在评论中所说的那样。我刚刚代表您创建了 AspectJ bug ticket #552687。请在那里查看进一步的更新。现在您可以降级到 1.9.2 并继续工作。
更新: 带有错误修复的 AspectJ 1.9.5 已发布。请再试一遍。我的复试成功了。