run time annotation scanning when Dynamic class loading

℡╲_俬逩灬. 提交于 2019-12-07 06:31:25

This approach worked for me. hope this helps someone.

 DemoClass= (Class<Annotation>) Class.forName("com.this.class.DemoClass");
    if (method.isAnnotationPresent(DemoClass)) {
       for (Annotation annotation : method.getAnnotations()) {
       Class<? extends Annotation> annotationType = annotation.annotationType();      
       if (annotationType.getName() == "com.this.class.DemoClass") {
          for (Method annotationMethod : annotationType.getDeclaredMethods()) {
          value= annotationMethod.invoke(annotation, (Object[]) null);
          }
       }
     }

When the annotation is loaded dynamically into variable, Demo, then use that variable to get the annotation:

Class<?> Demo = Class.forName("com.this.class.DemoClass");
Demo annotation = m.getAnnotation(Demo);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!