gpt4 book ai didi

java - 在 Java 方面使用 lambda 时出错

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:35 24 4
gpt4 key购买 nike

我们有一个自定义注解:

@Target(value = {ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface AsyncWithTimeout {
long timeout();
}

还有一个自定义的 @aspect,它拦截用 @AsyncWithTimeout 注释的方法,如下所示,工作正常(注意任务定义,没有 lambda )

@Aspect
public class AsyncWithTimeoutInterceptor {

@Around(value = "@within(com.foo.AsyncWithTimeout) || " + "@annotation(com.foo.AsyncWithTimeout)")
public Object asyncWithTimeout(ProceedingJoinPoint point) {
long timeout = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(AsyncWithTimeout.class).timeout()

// Task without lambda -> OK
Callable<Object> task = new Callable<Object>() {
public Object call() {
try {
// Do some stuff with the timeout...
} catch (Throwable throwable) {
// Do some stuff...
}
}
};

// Do some stuff ...
}
}

但是,如果我们使用 lambda 代替初始化任务,则会抛出一个异常:

@Aspect
public class AsyncWithTimeoutInterceptor {

@Around(value = "@within(com.foo.AsyncWithTimeout) || " + "@annotation(com.foo.AsyncWithTimeout)")
public Object asyncWithTimeout(ProceedingJoinPoint point) {
long timeout = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(AsyncWithTimeout.class).timeout()

// Task with lambda -> Error
Callable<Object> task = () -> {
try {
// Do some stuff with the timeout...
} catch (Throwable throwable) {
// Do some stuff...
}
};

// Do some stuff ...
}
}

异常详情:

java.lang.IllegalStateException: there is no classname for invokedynamic
at org.aspectj.apache.bcel.generic.InvokeDynamic.getClassName(InvokeDynamic.java:126)
at org.aspectj.weaver.bcel.BcelAccessForInlineMunger.openAroundAdvice(BcelAccessForInlineMunger.java:141)
at org.aspectj.weaver.bcel.BcelAccessForInlineMunger.munge(BcelAccessForInlineMunger.java:80)
at org.aspectj.weaver.bcel.BcelClassWeaver.weave(BcelClassWeaver.java:441)
at org.aspectj.weaver.bcel.BcelClassWeaver.weave(BcelClassWeaver.java:101)
at org.aspectj.weaver.bcel.BcelWeaver.weave(BcelWeaver.java:1689)
at org.aspectj.weaver.bcel.BcelWeaver.weaveWithoutDump(BcelWeaver.java:1633)
at org.aspectj.weaver.bcel.BcelWeaver.weaveAndNotify(BcelWeaver.java:1398)
at org.aspectj.weaver.bcel.BcelWeaver.weave(BcelWeaver.java:1155)
at org.aspectj.weaver.tools.WeavingAdaptor.getWovenBytes(WeavingAdaptor.java:527)
at org.aspectj.weaver.tools.WeavingAdaptor.weaveClass(WeavingAdaptor.java:363)
at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:121)
at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:54)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

EDIT: Actually aspectjweaver version is 1.8.0

使用的版本:jdk 1.8.0_124,aspectjweaver 1̶.̶9̶.̶1̶1.8.0

在我的 META-INF 中文件 aop.xml:

<aspectj>
<aspects>
<aspect name="com.foo.AsyncWithTimeoutInterceptor"/>
</aspects>

<weaver>
<include within="com.foo.*"/>
</weaver>
</aspectj>

我应该能够在方面使用 lambda 还是存在某种限制/错误?任何帮助将不胜感激。

提前致谢。

最佳答案

好吧,我的问题有误,使用的 aspectjweaver 版本实际上是 1.8.0 而不是 1.9.1

EDIT: As @kriegaex points in a comment, the bug is already solved in the 1.8.13 aspectjweaver version (not necessary upgrading to 1.9.1) Thanks for pointing out.

我刚刚检查过该错误已在 1.8.13 及更高版本中解决,因此 lambda 可以毫无问题地用于方面。

关于java - 在 Java 方面使用 lambda 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50059232/

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