- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想在我的应用程序中使用注释。为此,我为注释创建了“hello world”:
如下示例:
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();
}
我现在的问题是 main 中的 getAnnotations() 是空的。我的代码有什么问题?
最佳答案
将以下内容添加到您的注释中:
@Retention(RetentionPolicy.RUNTIME)
来自 @Retention 的 javadoc :
the retention policy defaults to
RetentionPolicy.CLASS
来自 RetentionPolicy 的 javadoc :
类
运行时间
来源
关于java - getAnnotations() 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12642266/
像这样从 gwt 类 SecureDispatchService: @RemoteServiceRelativePath("dispatch") public interface SecureDisp
我有以下代码: import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME
我期待这段代码: import java.util.Arrays; import java.lang.annotation.Retention; import java.lang.annotation
我想在我的应用程序中使用注释。为此,我为注释创建了“hello world”: 如下示例: public class HelloAnnotation { @Foo(bar = "Hello W
我正在尝试将 Java 库 (JOhm) 与 Scala 一起使用,并注意到当库尝试使用 field.isAnnotationPresent(Id.class) 之类的内容读取我的 Scala 类字段
我的注释看起来像这样: @Documented @Target(ElementType.FIELD) public @interface IsCrossSellingRevelant { bo
我有一个可重复的注解 @Repeatable(Examples.class) public @interface Example { int value(); } 带有容器注解 @Retent
我想运行一个简单的规则,如果它有特定的注释,它将尝试重新运行测试。具体来说,它将尝试根据用户的需要多次重新运行测试,方法是从 cusotm 读取 attempts int注释。 这是我创建的 cuso
我们可以在 Annotation 的接口(interface)上使用 getAnnotations() 但不能在 getAnnotation 上使用?为什么当我在以下程序中将接口(interface)
我正在尝试处理注释,我的处理器类扩展了 AbstractProcessor 并且 process 方法如下所示: KeyAnnotationProcessor.java @Override
我在 Spring 使用 AOP: 我写了一个注释 @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation { } 我
我必须按照代码检查我的 model 中的实体是否在字段上有 nullable=false 或类似的注释。 import javax.persistence.Column; import ..... p
Searchable.java @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Sea
我使用的是 Java 1.6.0_25。 我定义了一个注释: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public
本文整理了Java中org.eclipse.xsd.XSDAttributeGroupDefinition.getAnnotation()方法的一些代码示例,展示了XSDAttributeGroupD
本文整理了Java中org.eclipse.xsd.XSDModelGroupDefinition.getAnnotation()方法的一些代码示例,展示了XSDModelGroupDefinitio
本文整理了Java中org.eclipse.xsd.XSDEnumerationFacet.getAnnotation()方法的一些代码示例,展示了XSDEnumerationFacet.getAnn
本文整理了Java中org.eclipse.xsd.XSDFacet.getAnnotation()方法的一些代码示例,展示了XSDFacet.getAnnotation()的具体用法。这些代码示例主
我注意到以下情况之间有一个有趣的区别,但无法解释: //this works Class myClass = Class.class; MyAnnotation myAnnotation = myCl
我在 Maven 模块“A”中创建了以下注释 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RU
我是一名优秀的程序员,十分优秀!