且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

注释看不见?

更新时间:2022-12-30 18:40:38

您必须定义@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.