首页 > 解决方案 > isAnnotationPresent 总是返回 false

问题描述

我试图创建自己的注释并解析它。但是,当我尝试使用反射获取具有我的注释的方法时,我总是会出错。

谷歌搜索网络中的自定义注释,但仍然无法找到确切的问题。

下面是注解类和使用它并解析它的类。

注解:

package AnnotationsImpl;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {

    int custom_int() default 11; 
    String custom_str();

}

使用注释的类:

    package AnnotationsImpl;


    public class AnnotationImpl extends AnnotationExample {

        //@CustomAnnotation(custom_str="str1")   
        int a=2;
        @SuppressWarnings("deprecation")
         @CustomAnnotation(custom_str="str1")        

        public static void main(String[] args){
            AnnotationExample ai = new AnnotationExample();
            ai.overrideMethod();

            AnnotationImpl aaa = new AnnotationImpl();
            aaa.overrideMethod();

            aaa.testingCA();


            ai.myDeprecatedMethod();


        }

        @Override
        public void overrideMethod(){

            System.out.println("In child class");
        }

        @CustomAnnotation(custom_str="str1")        
        public static void testingCA(){

            System.out.println("custom annotation");
        }

    }

解析器:

    package AnnotationsImpl;

    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;



    public class AnnotationParser {


        public static void main(String[] args)throws Exception{

            for(//AnnotationParser.class.getClassLoader().loadClass("AnnotationsImpl.AnnotationImpl")//
                    Method method : Class.forName("AnnotationsImpl.AnnotationImpl").getMethods()

                    ){

                System.out.print("method name :"+method.getName());
                System.out.print(" *** Custom annotation present :"+method.isAnnotationPresent(CustomAnnotation.class));

                System.out.println();

                if(method.isAnnotationPresent(AnnotationsImpl.CustomAnnotation.class)){

                    System.out.println("custom present");

                    for(Annotation ann : method.getDeclaredAnnotations()){



                        System.out.println("Annotation ann :"+ann +"=== method :::"+method);

                        CustomAnnotation cu = method.getAnnotation(CustomAnnotation.class);

                    }
                }



            }

        }

    }

输出 :

*run:
method name :main *** Custom annotation present :false
method name :check *** Custom annotation present :false
method name :overrideMethod *** Custom annotation present :false
method name :testingCA *** Custom annotation present :false
method name :wait *** Custom annotation present :false
method name :wait *** Custom annotation present :false
method name :wait *** Custom annotation present :false
method name :equals *** Custom annotation present :false
method name :toString *** Custom annotation present :false
method name :hashCode *** Custom annotation present :false
method name :getClass *** Custom annotation present :false
method name :notify *** Custom annotation present :false
method name :notifyAll *** Custom annotation present :false
BUILD SUCCESSFUL (total time: 0 seconds)*

尝试了 RUNTIME 保留策略。为什么我的注释总是假的?我犯了什么错误?提前致谢 。

标签: javareflectionannotations

解决方案


推荐阅读