2017-12-05 19 views
1
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); 

ような何かを行うことができます。しかし、私は動的に

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

ようDemoClass/AnnotationClassをロードしたい場合はどうすればメソッドに注釈を取得します。

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

具体的に達成しようとしていることはありますか? [最小で完全で検証可能な例](https://stackoverflow.com/help/mcve)を作成してみてください。 – fragmentedreality

+1

@fragmentedrealityが質問を更新しました。まだ情報が必要な場合は教えてください。 – vjr

+0

私はあなたの質問のタイトルを調整することをお勧めします:用語 "アノテーションプロセッサ"は、[特定の意味](https://stackoverflow.com/tags/annotation-processing/info)をJava言語の範囲に含みます。あなたの目標にもっと適しているという用語は、*ランタイムアノテーションスキャン*または単に[イントロスペクション](https://stackoverflow.com/tags/introspection/info)です。 – user1643723

答えて

1

このアプローチは、のために働いていた:私は、以下の行は、この場合の著作

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

注釈を変数に動的にロードされ、Demoは、その後、注釈を取得するには、その変数を使用していないと思います私。これが誰かを助けることを望みます

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

試しました。私はここで2つの問題に遭遇しました。 1つ目はデモを型に解決できない場合で、メソッドの型getAnnotation(Class )は引数には適用されません。 – vjr

+0

私はfragmentedrealityに同意します。最小限で完全で検証可能な例があれば、修正を推奨することができます。 –

+0

あなたが最初に言ったことは私が達成しようとしていることです。しかし、私が言ったようにアプローチは機能していません。要件は明確です。あなたが何を理解していないのか分かりません。 私は特定のアノテーションクラスのアノテーションを取得しようとしています デモアノテーション= m.getAnnotation(デモ); インポートを使用してDemoクラスをロードすると動作します。 しかし、Class.forname()を使用してDemo.classがロードされているときに同じ操作を実行する方法がわからない場合は、 – vjr

関連する問題