在 java 方面使用 lambda 时出错

Error using lambda in java aspect

我们有一个自定义注释:

@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 代替初始化任务,则会抛出 Exception

@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 还是有某种 limitation/bug? 任何帮助将不胜感激。

提前致谢。

好吧,我的问题有误,使用的 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 可以毫无问题地用于方面。