isAnnotationPresent始终返回false

时间:2018-12-15 16:10:20

标签: java reflection annotations

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

在网络中使用Googled自定义注释,但仍然找不到确切的问题。

下面是注释类和使用它并对其进行解析的类。

注释:

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保留策略。 为什么我总是将注释呈现为假?我做了什么错误?预先感谢。

0 个答案:

没有答案