我无法使用自己的注释

use*_*274 2 java reflection

使用我自己的注释时出错,任何人都可以帮助我解决这个问题这是我的以下注释:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(value=ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)


public @interface CanRun {

}
Run Code Online (Sandbox Code Playgroud)

这是我的班级之一,代替注释显示错误,任何人都可以解释和解决...

import java.lang.reflect.Method;

public class AnnotationRunner {

    public void method1() {
        System.out.println("method1");
    }

    @CanRun
    public void method2() {
        System.out.println("method2");
    }

    @CanRun
    public void method3() {
        System.out.println("method3");
    }

    public void method4() {
        System.out.println("method4");
    }

    public void method5() {
        System.out.println("method5");
    }

} 
Run Code Online (Sandbox Code Playgroud)

这是使用此注释的主类

public class MyTest {

    public static void main(String[] args) {

        AnnotationRunner runner = new AnnotationRunner();
        Method[] methods = runner.getClass().getMethods();

        for (Method method : methods) {
            CanRun annos = method.getAnnotation(CanRun.class);
            if (annos != null) {
                try {
                    method.invoke(runner);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

Jag*_*ram 7

更改

@Target(value=ElementType.TYPE)
Run Code Online (Sandbox Code Playgroud)

@Target(value=ElementType.METHOD)
Run Code Online (Sandbox Code Playgroud)