gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:28:50 29 4
gpt4 key购买 nike

查看 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

正在测试的类(我遇到了一些问题,因为proceed显然声明它抛出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();

}

最后再次出现错误,该错误实际上是在 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)

最佳答案

问题在于您将 native AspectJ 语法(公共(public)方面 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();
}
}

正确的 native 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();
}
}

关于AspectJ 和 Java8 - 操作数堆栈上的错误类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042972/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com