我们有一个自定义注释:
@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,aspectjweaver1̶.̶9̶.̶1̶1.8.0
在我的META-INF中文件aop.xml:
<aspectj>
<aspects>
<aspect name="com.foo.AsyncWithTimeoutInterceptor"/>
</aspects>
<weaver>
<include within="com.foo.*"/>
</weaver>
</aspectj>
我应该能够在方面使用lambdas还是存在某种限制/错误?
任何帮助将是欣赏.
提前致谢.
解决方法:
好吧,我在我的问题中犯了一个错误,使用的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及更高版本中解决的,所以lambdas可以在没有问题的情况下使用.