java - 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 保留策略。为什么我的注释总是假的?我犯了什么错误?提前致谢 。
解决方案
推荐阅读
- mongodb - mongodb 2级聚合查找
- android - 如何在运行时单击为整个应用程序设置自定义主题?
- python - gimp python不起作用:无法执行子进程(没有这样的文件或目录)
- firebase - Firebase 身份验证客户端线程是否安全?
- c# - 异步加载树层次结构
- c++ - 为什么地图中的名字不按排列顺序出现?
- html - 与其他元素组合时,我似乎无法让页脚位于页面底部
- django - 根据属性值从数据库中检索随机记录,直到达到限制,如果未达到限制,则与其他记录一起完成
- javascript - React Native - 在渲染下可以导航但不能在导航栏中
- excel - Excel TIME 持续时间超过 24 小时