对多个匹配建议的Aspectj异常处理 [英] Aspectj exceptions handling on multiple matching advices

查看:60
本文介绍了对多个匹配建议的Aspectj异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个方面适用于相同的方法.当方法正确执行时,我没有问题,一切都正常,并且两个方面都按预期工作.问题是方法抛出异常时.在这些情况下,第一个方面正确地重新引发了异常,但是第二个方面正在生成nullpointerexception.我能够重现在单独项目中的单元测试中隔离案例的问题.这些就是这些方面(实际上,我删除了所有逻辑,目前它们什么也不做):

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

@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个自定义注释

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

最后是本单元测试

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)失败.

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

该应用程序在带有Aspectj 1.9.4和junit 5的Java 8上运行.这里是完整的pom.xml:

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>

如果我仅应用一个方面,则将CustomException按方面抛出,但是当同时应用这两个方面时,我会得到一个nullpointerexception.

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

我还尝试使用DeclarePrecedence批注声明方面的优先级,但没有用

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

这是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错误凭单#552687 .请检查那里以获取更多更新.现在,您可以降级到1.9.2并继续工作.

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.

更新:修正了错误的AspectJ 1.9.5已发布.请再试一次.我的重新测试成功了.

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

这篇关于对多个匹配建议的Aspectj异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆