java Java注释不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/728605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Annotations not working
提问by Rexsung
I'm trying to use Java annotations, but can't seem to get my code to recognize that one exists. What am I doing wrong?
我正在尝试使用 Java 注释,但似乎无法让我的代码识别出存在的注释。我究竟做错了什么?
import java.lang.reflect.*;
import java.lang.annotation.*;
@interface MyAnnotation{}
public class FooTest
{
@MyAnnotation
public void doFoo()
{
}
public static void main(String[] args) throws Exception
{
Method method = FooTest.class.getMethod( "doFoo" );
Annotation[] annotations = method.getAnnotations();
for( Annotation annotation : method.getAnnotations() )
System.out.println( "Annotation: " + annotation );
}
}
回答by James Davies
You need to specify the annotation as being a Runtime annotation using the @Retention annotation on the annotation interface.
您需要使用注释界面上的@Retention 注释将注释指定为运行时注释。
i.e.
IE
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{}
回答by Brandon Yarbrough
Short answer: you need to add @Retention(RetentionPolicy.RUNTIME) to your annotation definition.
简短回答:您需要将 @Retention(RetentionPolicy.RUNTIME) 添加到您的注释定义中。
Explanation:
解释:
Annotations are by default notkept by the compiler. They simply don't exist at runtime. This may sound silly at first, but there are lots of annotations that are only used by the compiler (@Override) or various source code analyzers (@Documentation, etc).
默认情况下,编译器不保留注释。它们在运行时根本不存在。乍一看这听起来很傻,但是有很多注释仅由编译器(@Override)或各种源代码分析器(@Documentation 等)使用。
If you want to actually USE the annotation via reflection like in your example, you'll need to let Java know that you want it to make a note of that annotation in the class file itself. That note looks like this:
如果您想像示例中那样通过反射实际使用注释,则需要让 Java 知道您希望它在类文件本身中记录该注释。该注释如下所示:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{}
For more information, check out the official docs1and especially note the bit about RetentionPolicy.
有关更多信息,请查看官方文档1并特别注意有关 RetentionPolicy 的部分。
回答by Sudipta Deb
Use @Retention(RetentionPolicy.RUNTIME)Check the below code. It is working for me:
使用@Retention(RetentionPolicy.RUNTIME)检查下面的代码。它对我有用:
import java.lang.reflect.*;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1{}
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{}
public class FooTest {
@MyAnnotation1
public void doFoo() {
}
@MyAnnotation2
public void doFooo() {
}
public static void main(String[] args) throws Exception {
Method method = FooTest.class.getMethod( "doFoo" );
for( Annotation annotation : method.getAnnotations() )
System.out.println( "Annotation: " + annotation );
method = FooTest.class.getMethod( "doFooo" );
for( Annotation annotation : method.getAnnotations() )
System.out.println( "Annotation: " + annotation );
}
}

