Aspectj exceptions handling on multiple matching advices

北慕城南 提交于 2021-02-05 10:39:14

问题


I have 2 aspects that are applied on the same method. When the method executes correctly I have no problem, everything is working fine and both aspects work as expected. The problem is when the method throw an exception. In these cases, the first aspect re-throw correctly the exception, but the second aspect is generating a nullpointerexception. I was able to reproduce the problem isolating the case on a unit test in a separated project. Those are the aspects (actually I removed all the logic, at the moment they do nothing):

@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;
        }
    }
}

and

@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;
        }
    }
}

while those are the 2 custom annotation that I implemented

@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();
}

then I created a simple class with the following methods

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");
    }   
}

and finally this unit test

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();
        }
    }       

}

All tests are running correctly, only the last one (test6) fails.

the application is running on java 8, with aspectj 1.9.4 and junit 5. Here the complete 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>

If I apply just a single aspect the CustomException is throwed as aspected, but when both aspects are applied I'm getting a nullpointerexception.

I've also tried to declare the precedence of the aspects, using DeclarePrecedence annotation, but it didn't work

@DeclarePrecedence("it.pivimarco.samples.aspects.LogContextConstantAspect,it.pivimarco.samples.aspects.LogExecutionTimeAspect")

This is the stacktrace of the 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)

回答1:


I think you uncovered a bug in the AspectJ compiler (versions 1.9.3, 1.9.4) which does not occur in version 1.9.2, as I said in my comment. I have just created AspectJ bug ticket #552687 on your behalf. Please check there for further updates. For now you can just downgrade to 1.9.2 and continue working.


Update: AspectJ 1.9.5 with the bugfix is out. Please try again. My retest was successful.



来源:https://stackoverflow.com/questions/58626598/aspectj-exceptions-handling-on-multiple-matching-advices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!