Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';java看不到我的注释吗?_Java_Annotations - Fatal编程技术网

为什么';java看不到我的注释吗?

为什么';java看不到我的注释吗?,java,annotations,Java,Annotations,因此,我试图学习使用自定义注释的基础知识,因此我创建了一个空注释: public @interface CallMe { } 和测试课程: import java.lang.annotation.*; @CallMe public class Test { public static void main(String[] args) throws Exception { Annotation[] annotations = Test.class.getAnnotat

因此,我试图学习使用自定义注释的基础知识,因此我创建了一个空注释:

public @interface CallMe {

}
测试
课程:

import java.lang.annotation.*;

@CallMe
public class Test {
    public static void main(String[] args) throws Exception {
        Annotation[] annotations = Test.class.getAnnotations();

        if (Test.class.isAnnotationPresent(CallMe.class)) {
            System.out.println("CallMe is present.");
        }

        System.out.println("Found " + annotations.length + " annotations.");

        for (Annotation a: annotations) {
            System.out.println("Annotation: " + a);
        }
    }
}
我编译这两个类并执行
测试
,但是:

$ javac Test.java CallMe.java
$ java Test
Found 0 annotations.
如果有必要的话,我将使用OpenJDK1.6进行此测试。我已经创建了
.getAnnotations()
.getDeclaredAnnotations()
,但没有结果

为什么Java找不到注释


(如果您想知道,我最初打算对方法进行注释,这就是为什么我将其设置为
CallMe
,但我认为先做一个类级示例会更容易)。

您需要像这样注释注释类,以使注释信息在运行时可用:

@Retention(RetentionPolicy.RUNTIME) 
public @interface CallMe {

}
尝试:


+1谢谢,Target也有助于了解。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface CallMe {
...
}