java – 为什么AspectJ生成一个空的Annotation检查?

我正在使用AspectJ 1.8.8编译时编织,我有一个像这样的块

@SomeAnnotation(value="someValue")
public List doSomething(String someArg) {
    ...
}

其中@SomeAnnotation使用“Around”建议实现.

使用JD-GUI查看字节码,我看到以下生成的代码(稍微格式化):

public class SomeClass {
  private static Annotation ajc$anno$5;

  ...

  @SomeAnnotation(value="someValue")
  public List doSomething(String someArg)
  {
    String str = someArg;
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_5, this, this, str);
    Object[] arrayOfObject = new Object[3];
    arrayOfObject[0] = this;
    arrayOfObject[1] = str;
    arrayOfObject[2] = localJoinPoint;
    Annotation tmp56_53 = ajc$anno$5;
    if (tmp56_53 == null) {
      tmp56_53;
    }
    return (List)new SomeClass.AjcClosure11(arrayOfObject).linkClosureAndJoinPoint(69648).around(tmp56_53, (SomeAnnotation)(ajc$anno$5 = SomeClass.class.getDeclaredMethod("doSomething", new Class[] { String.class }).getAnnotation(SomeAnnotation.class)));
  }
}

我想知道为什么这个条件(如果(tmp56_53 …))甚至存在,因为它似乎什么都不做(并且语法上也是不正确的Java?也许是因为这是由ajc生成的?).我对此感到好奇,因为它在覆盖工具(JaCoCo)中导致“分支未命中”.

编辑1

以下是javap的原始Java机器代码:

       0: aload_1
       1: astore_2
       2: getstatic     #480                // Field ajc$tjp_10:Lorg/aspectj/lang/JoinPoint$StaticPart;
       5: aload_0
       6: aload_0
       7: aload_2
       8: invokestatic  #312                // Method org/aspectj/runtime/reflect/Factory.makeJP:(Lorg/aspectj/lang/JoinPoint$StaticPart;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Lorg/aspectj/lang/JoinPoint;
      11: astore_3
      12: invokestatic  #339                // Method com/foo/SomeAspect.aspectOf:()Lcom/foo/SomeAspect;
      15: iconst_3
      16: anewarray     #2                  // class java/lang/Object
      19: astore        4
      21: aload         4
      23: iconst_0
      24: aload_0
      25: aastore
      26: aload         4
      28: iconst_1
      29: aload_2
      30: aastore
      31: aload         4
      33: iconst_2
      34: aload_3
      35: aastore
      36: new           #484                // class com/foo/SomeClass$AjcClosure21
      39: dup
      40: aload         4
      42: invokespecial #485                // Method com/foo/SomeClass$AjcClosure21."<init>":([Ljava/lang/Object;)V
      45: ldc_w         #327                // int 69648
      48: invokevirtual #333                // Method org/aspectj/runtime/internal/AroundClosure.linkClosureAndJoinPoint:(I)Lorg/aspectj/lang/ProceedingJoinPoint;
      51: getstatic     #488                // Field ajc$anno$10:Ljava/lang/annotation/Annotation;
      54: dup
      55: ifnonnull     86
      58: pop
      59: ldc           #75                 // class com/foo/SomeClass
      61: ldc_w         #489                // String someArg
      64: iconst_1
      65: anewarray     #348                // class java/lang/Class
      68: dup
      69: iconst_0
      70: ldc           #171                // class java/lang/String
      72: aastore
      73: invokevirtual #352                // Method java/lang/Class.getDeclaredMethod:(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;
      76: ldc_w         #341                // class com/foo/SomeAnnotation
      79: invokevirtual #358                // Method java/lang/reflect/Method.getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
      82: dup
      83: putstatic     #488                // Field ajc$anno$10:Ljava/lang/annotation/Annotation;
      86: nop
      87: checkcast     #341                // class com/foo/SomeAnnotation
      90: invokevirtual #362                // Method com/foo/SomeAspect.around:(Lorg/aspectj/lang/ProceedingJoinPoint;Lcom/foo/SomeAnnotation;)Ljava/lang/Object;
      93: pop
      94: return

看起来ifnonnull可能是有条件的,但我对JVM指令并不熟悉,我仍然不知道为什么AspectJ会生成这样的逻辑.

最佳答案 tl; dr:这是一个正常的懒惰初始化,jd只是困惑.

字节16是创建新对象[3]的地方:

16: anewarray     #2                  // class java/lang/Object

你可以在19-35之后看到,它只是立即将局部变量复制到堆栈(索引的iconst,参考的aload),然后将它们写入数组(aastore).紧接的下一个字节是36,这是新的运算符(只是分配,然后紧跟invokespecial运行构造函数).

这将我们带到字节48,它调用linkClosureAndJoinPoint.你没有包含你的常量表,但在45 ldc_w#327加载常量值69648,这样就可以使我们达到.around的目的.

现在,在字节51处发生了一些有趣的事情.jd重建的单链接调用现在被中断了.字节码将静态注释字段ajc $anno $10(不是5,如jd所说)加载到堆栈上.如果该注释字段不为null(55),则执行跳转到86(无操作,用作跳转的“着陆点”),执行该转换检查((SomeAnnotation)),然后最终实际调用咨询.

跳过的代码(58-82)说明了这一点,您将从反编译中识别出来:

SomeClass.class
    .getDeclaredMethod("doSomething", new Class[] { String.class })
    .getAnnotation(SomeAnnotation.class)

字节83然后将结果存储到静态字段中,并从那里继续执行.

用Java术语来说,这正是发生的事情:

if (cachedAnnotation == null) {
    cachedAnnotation = getAnnotationOnMethodUsingReflection();
}

AspectJ的字节码在这里非常紧凑和干净(可能是手工优化的,因为这可能是非常热门的代码).由于这个或因为该逻辑中断了链式方法调用,jd变得混乱并且分裂空检查和赋值.

点赞