gpt4 book ai didi

Java 注释 - javac 编译器错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:58:35 28 4
gpt4 key购买 nike

我遇到了与嵌套类中方法参数的注释相关的奇怪效果。对我来说看起来很像编译器问题。请参阅下面的详细信息和重现步骤。

用javac编译下面的类(我用的是javac 1.7.0_51)。请注意带注释的参数“boolean param3”。

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class TestAnnotations {

public String a;

@Retention(RetentionPolicy.CLASS)
@interface MyAnnotation {}

protected class TestInner {

public TestInner(String param1, Object param2,
@MyAnnotation boolean param3) {}

public void accessField() {
System.out.println(TestAnnotations.this.a);
}
}
}

然后使用 javap 检查嵌套类(即 javap -p -v -c TestAnnotations$TestInner.class)。它的构造函数如下所示。

 public test.TestAnnotations$TestInner(test.TestAnnotations, java.lang.String, 
java.lang.Object, boolean);
flags: ACC_PUBLIC
Code:
stack=2, locals=5, args_size=5
0: aload_0
1: aload_1
2: putfield #1 // Field this$0:Ltest/TestAnnotations;
5: aload_0
6: invokespecial #2 // Method java/lang/Object."<init>":()V
9: return
LineNumberTable:
line 16: 0
RuntimeInvisibleParameterAnnotations:
0:
1:
2:
0: #18()

注意属性 RuntimeInvisibleParameterAnnotations 中的注释数量 - 它是 3。同时我们现在观察到 4 个方法参数,因为一个额外的 test.TestAnnotations 在开头(它用于将对 TestAnnotations.this 的引用传递到内部类)。这意味着,@MyAnnotation 现在引用对象参数 2,向左移动 1。

根据虚拟机规范,注释的数量应该与方法参数的数量相同:

num_parameters

The value of the num_parameters item gives the number of parameters of the method represented by the method_info structure on which the annotation occurs. (This duplicates information that could be extracted from the method descriptor (§4.3.3).)

这里我们清楚地看到违规行为。有谁知道原因吗?它真的像它看起来的那样,只是一个编译器错误吗?

最佳答案

这是一个 javac 编译器错误,请参阅:https://bugs.openjdk.java.net/browse/JDK-8065132我刚刚提交的。

关于Java 注释 - javac 编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22065461/

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