AspectJ和Java8 - 操作数堆栈上的错误类型

时间:2014-07-30 17:13:31

标签: aspectj java-8 load-time-weaving

看看This Eclipse Bug,似乎Java Verifier(自1.6起)与ApsectJ存在问题。

该错误说AspectJ 1.8.1将解决问题。但是在Java8u11中使用它仍然会得到验证错误。

我在STS 3.6.0(Eclipse 4.4)下运行JUnit4。我相信这种配置是所有软件包中最新的配置。

使用请求的示例完全替换了剩余的文本。这似乎仅限于@Around建议。 @Before正常工作。

JUnit的:

package com.test.aspectjdemo.junit;

import static org.junit.Assert.*;
import org.junit.Test;
import com.test.aspectjdemo.domain.AspectTarget;

public class AspectTargetTest {

    @Test
    public void testFirstMethod() throws Throwable {
        AspectTarget aspectTarget = new AspectTarget();
        aspectTarget.firstMethod();
    }
}

Vmarg:-javaagent:C:.... m2 \ repository \ org \ aspectj \ aspectjweaver \ 1.8.1 \ aspectjweaver-1.8.1.jar

正在测试的类(我遇到了一些问题,因为显然声明它会抛出Throwable,这很有意义,但是这个简单的测试没有抛出任何东西。所以我添加了一个虚假异常使其编译:

package com.test.aspectjdemo.domain;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class AspectTarget {

    final Logger logger = LogManager.getLogger();

    int x = 1;

    public void firstMethod() throws Throwable {
        logger.info("Start First Method");
        x = secondMethod(x);
        logger.info("Exit X is {}", x);
    }

    private int secondMethod(int x) throws Throwable {
        logger.info("input is {}", x++);
        if (x==100)
            throw new RuntimeException();
        return new Integer(x);
    }
}

方面:

package com.test.aspectjdemo.aspects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

public aspect LoggingAspect {

    static final Logger logger = LogManager.getLogger();


    /**
     * Exclude JUnit methods
     */
    @Pointcut("!within(com.test.aspectjdemo.junit..*Test)")
    public void noJunit() {}


    @Pointcut("execution(* com.test.aspectjdemo.domain.*.*(..)) && noJunit()")
    public void allMethods() { }


    @Around("allMethods()")
    public Object allmethods(ProceedingJoinPoint joinPoint) throws Throwable   {

        return joinPoint.proceed();

    }

最后再次出现Error,当JUnit尝试实例化AspectTarget(testFirstMethod方法的第一行)时,它实际上被抛出。

java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/test/aspectjdemo/domain/AspectTarget.secondMethod(I)I @23: invokestatic
  Reason:
    Type 'org/aspectj/lang/JoinPoint' (current frame, stack[2]) is not assignable to integer
  Current Frame:
    bci: @23
    flags: { }
    locals: { 'com/test/aspectjdemo/domain/AspectTarget', integer, integer, 'org/aspectj/lang/JoinPoint' }
    stack: { 'com/test/aspectjdemo/domain/AspectTarget', integer, 'org/aspectj/lang/JoinPoint', 'com/test/aspectjdemo/aspects/LoggingAspect', null, 'org/aspectj/lang/JoinPoint' }
  Bytecode:
    0000000: 1b3d b200 4b2a 2a1c b800 51b8 0057 4e2a
    0000010: 1c2d b800 6601 2db8 006a b800 6dac     

    at com.test.aspectjdemo.junit.AspectTargetTest.testFirstMethod(AspectTargetTest.java:13)
    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:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

1 个答案:

答案 0 :(得分:6)

问题在于您将原生AspectJ语法(public aspect LoggingAspect)与注释样式的@AspectJ语法混合在一起。我可以通过这种方式重现问题。

更正@AspectJ语法:

package com.test.aspectjdemo.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggingAspect {
    @Pointcut("!within(com.test.aspectjdemo.junit..*Test)")
    public void excludeJUnit() {}

    @Pointcut("execution(* com.test.aspectjdemo.domain.*.*(..)) && excludeJUnit()")
    public void allMethods() {}

    @Around("allMethods()")
    public Object allmethods(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println(thisJoinPoint);
        return thisJoinPoint.proceed();
    }
}

更正原生AspectJ语法:

package com.test.aspectjdemo.aspects;

public aspect LoggingAspect {
    pointcut excludeJUnit() :
        !within(com.test.aspectjdemo.junit..*Test);

    pointcut allMethods() :
        execution(* com.test.aspectjdemo.domain.*.*(..)) && excludeJUnit();

    Object around() : allMethods() {
        System.out.println(thisJoinPoint);
        return proceed();
    }
}