【问题标题】:run time annotation scanning when Dynamic class loading动态类加载时的运行时注释扫描
【发布时间】:2017-12-05 21:30:47
【问题描述】:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
   String str();
   int val();
}

public class PackageDemo {

   // set values for the annotation
   @Demo(str = "Demo Annotation", val = 100)
   // a method to call in the main
   public static void example() {
      PackageDemo ob = new PackageDemo();

      try {
         Class c = ob.getClass();

         // get the method example
         Method m = c.getMethod("example");

         // get the annotation for class Demo
         Demo annotation = m.getAnnotation(Demo.class);

         // print the annotation
         System.out.println(annotation.str() + " " + annotation.val());
      } catch (NoSuchMethodException exc) {
         exc.printStackTrace();
      }
   }
   public static void main(String args[]) {
      example();
   }
}

我的目标是检查几个方法上的注释,如果它存在于注释上,我需要获取注释。

Demo annotation = m.getAnnotation(Demo.class);

在上面的例子中,注解是在同一个文件中声明的。如果注释在不同的包中,我可以做类似

import com.this.class.DemoClass
try {
         Class c = ob.getClass();

         // get the method example
         Method m = c.getMethod("example");

         // get the annotation for class Demo
         Demo annotation = m.getAnnotation(Demo.class);

但是如果我想像动态加载 DemoClass/AnnotationClass 一样

Class<?> Demo = Class.forName("com.this.class.DemoClass")

如何获取方法上的注释。我认为下面的行在这种情况下不起作用

Demo annotation = m.getAnnotation(Demo.class);

【问题讨论】:

  • 您能更具体地说明您要完成的工作吗?尝试创建一个minimal, complete and verifiable example
  • @fragmentedreality 更新了问题,如果您还需要一些信息,请告诉我。
  • 我建议您调整问题的标题:术语“注释处理器”在 Java 语言范围内具有 specific meaning。更适合您的目标的术语是 运行时注释扫描 或简称为 introspection
  • @user1643723 更新了标题。
  • 我没有调查您的问题的具体细节,但您是否尝试过使用现有库之一来完成您的任务? fast-classpath-scanner 或其中的一种替代方案会让您的工作更轻松。

标签: java class methods reflection annotation-processing


【解决方案1】:

这种方法对我有用。希望这可以帮助某人。

 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);
          }
       }
     }

【讨论】:

    【解决方案2】:

    当注解被动态加载到变量Demo中,然后使用该变量获取注解:

    Class<?> Demo = Class.forName("com.this.class.DemoClass");
    Demo annotation = m.getAnnotation(Demo);
    

    【讨论】:

    • 试过了。我在这里遇到了2个问题。第一个是 Demo 无法解析为类型,然后类型 Method 中的方法 getAnnotation(Class) 不适用于参数。
    • 我同意碎片化现实。通过一个最小的、完整的和可验证的示例,我们可以推荐修复。
    • 你首先说的是我想要完成的。但这种方法并没有像我说的那样奏效。要求很明确。不知道你在其中不理解什么。我正在尝试获取特定注释类的注释,例如 Demo annotation = m.getAnnotation(Demo);它在我使用 import 加载 Demo 类时起作用。但不确定如何在使用 Class.forname() 加载 Demo.class 时执行相同的操作
    猜你喜欢
    • 2010-09-20
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多