繁体   English   中英

"Java Type Use annotation - 如何阅读它们"

[英]Java Type Use annotation - How to read them

我正在使用自定义类型使用注释。 我无法像任何其他常规注释一样从对象中读取它们:

public class TestingAnnotations {



public static void main(final String[] args) {
     final @CustomAnnotation TypeAnnotated b = new @CustomAnnotation TypeAnnotated();
     System.out.println(b.getClass().getAnnotation(CustomAnnotation.class)); //<-- prints null :(
   }
}

@Target({ElementType.TYPE_USE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {

}

class TypeAnnotated {

}

你实际上没有注释类......一个类看起来像是注释:

@CustomAnnotation
class TypeAnnotated {

}

之后你会得到注释:

TypeAnnotated b = new TypeAnnotated();
System.out.println(b.getClass().getAnnotation(CustomAnnotation.class));

看起来您实际上想要一个本地变量注释

@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
public @interface CustomAnnotation {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM