注释看不见? [英] annotation invisible?
问题描述
这让我为难。我有一个自定义注解类,我似乎无法验证注解是present。我在做什么错在这里?如果我运行 MyOperationTest
(见下文),我得到这个结果:
实现Library.Operation:真
有@ Library.Marker:假的
田田!
Library.java:
包com.example.gotchas;公共类库{
私人图书馆(){} 公共@interface标记{} 公共接口操作{
公共无效的execute();
}
}
MyOperation.java:
包com.example.gotchas;@ Library.Marker
公共类MyOperation实现Library.Operation {
@覆盖公共无效的execute(){
的System.out.println(田田!);
}
}
MyOperationTest.java:
包com.example.gotchas;公共类MyOperationTest {
静态公共无效的主要(字串[] args)
{
尝试{
类<> CL =的Class.forName(com.example.gotchas.MyOperation);
布尔implementsLibraryOperation =
Library.Operation.class.isAssignableFrom(CL);
布尔hasLibraryMarker =
cl.isAnnotation present(Library.Marker.class);
的System.out.println(实现Library.Operation:
+ implementsLibraryOperation);
的System.out.println(有@ Library.Marker:+ hasLibraryMarker);
如果(implementsLibraryOperation)
{
类&LT ;?扩展Library.Operation>的opclass =
cl.asSubclass(Library.Operation.class);
Library.Operation运算= opClass.newInstance();
op.execute();
}
}
捕捉(ClassNotFoundException的E){
e.printStackTrace();
}
赶上(InstantiationException E){
e.printStackTrace();
}
赶上(IllegalAccessException E){
e.printStackTrace();
}
}
}
您必须定义<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/annotation/Retention.html\"><$c$c>@Retention$c$c>您的注释:
@Retention(RetentionPolicy.RUNTIME)
公共@interface标记{}
否则,注释在运行时没有保留。
(你还可以添加明确的 @Target(ElementType.TYPE)
如果你想限制你只能在类注解的使用)
作为旁注 - 这不是一种常见的做法来定义注解内部类
This puzzles me. I have a class with a custom annotation and I can't seem to verify that the annotation is present. What am I doing wrong here? If I run MyOperationTest
(see below), I get this as a result:
implements Library.Operation: true
has @Library.Marker: false
Tada!
Library.java:
package com.example.gotchas;
public class Library {
private Library() {}
public @interface Marker {}
public interface Operation {
public void execute();
}
}
MyOperation.java:
package com.example.gotchas;
@Library.Marker
public class MyOperation implements Library.Operation {
@Override public void execute() {
System.out.println("Tada!");
}
}
MyOperationTest.java:
package com.example.gotchas;
public class MyOperationTest {
static public void main(String[] args)
{
try {
Class<?> cl = Class.forName("com.example.gotchas.MyOperation");
boolean implementsLibraryOperation =
Library.Operation.class.isAssignableFrom(cl);
boolean hasLibraryMarker =
cl.isAnnotationPresent(Library.Marker.class);
System.out.println("implements Library.Operation: "
+implementsLibraryOperation);
System.out.println("has @Library.Marker: "+hasLibraryMarker);
if (implementsLibraryOperation)
{
Class<? extends Library.Operation> opClass =
cl.asSubclass(Library.Operation.class);
Library.Operation op = opClass.newInstance();
op.execute();
}
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
You have to define @Retention
for your annotation:
@Retention(RetentionPolicy.RUNTIME)
public @interface Marker {}
Otherwise the annotation is not retained at runtime.
(You can also add explicit @Target(ElementType.TYPE)
if you want to limit the usage of your annotation only on classes)
As a sidenote - it is not a common practice to define annotations as inner classes.
这篇关于注释看不见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!