getAnnotations()是空的 [英] getAnnotations() is empty
本文介绍了getAnnotations()是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的应用程序使用的标注。出于这个原因,我创建的Hello World的注解:
I would like to use annotations in my application. For this reason I create "hello world" for annotations:
如下的例子:
public class HelloAnnotation
{
@Foo(bar = "Hello World !")
public String str;
public static void main(final String[] args) throws Exception
{
System.out.println(HelloAnnotation.class.getField("str").getAnnotations().length);
}
}
这是注释:
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
public @interface Foo
{
public String doTestTarget();
}
确定。我现在的问题是,getAnnotations()主是空的。有什么不对我的code?
Ok. My problem is now that getAnnotations() in main is empty. What is wrong with my code?
THX很多
Walery
thx a lot Walery
推荐答案
以下内容添加到您的注释:
Add the following to your annotation:
@Retention(RetentionPolicy.RUNTIME)
从的Javadoc <一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/RetentionPolicy.html\">@Retention:
保留策略默认为 RetentionPolicy.CLASS
从的Javadoc <一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/RetentionPolicy.html\">RetentionPolicy:
From the javadoc for RetentionPolicy:
-
类
- 注释是由编译器,但被记录在类文件中的需要在运行时不被VM保留
CLASS
- Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
- 注释将被记录在由编译器的类文件,并在运行时由VM保留,所以他们可以读沉思
- Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.
- 注解是由编译器被丢弃。
这篇关于getAnnotations()是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文