2016-05-20 10 views
1

実際のクラスにJavaオブジェクト/文字列を変換:私は経由してJavaクラスのリストを取得

List<String> cF = classFinder.findClassesInPackage("com.some.test", Info.class); 
classFinderは、サービス/クラスであり、 findClassesInPackage方法がで @Info注釈を持つパッケージ com.some.test内のすべてのクラスを見つけ

それら。 Info.classはインターフェイス以外の2番目のパラメータです。

マイリストcFは現在、以下の内容で満たされている:

Set<String> set = new HashSet<String>(cF); 

目的:今

[com.some.test.example.FollTest, com.some.test.example.SubsTest, ..] 

、私は経由して、重複するメソッドを削除するにはList cFSetにこれを変換する

私はしたいiterateがこのセットにあるので、すべてclasses one-by-oneを取得し、最終的に各クラスのすべてのInfo注釈を取得して印刷することができます。私は後でこれらの注釈を解析するので、これが必要です。

私の懸念:設定された要素は現在ストリングになっています。実際のクラスに変換するにはどうすればいいですか? - まだオブジェクト/文字列ではなく、私が欲しい実際のクラス

Object check = set.toArray()[0];:ここ(For example: out of my first element in set: com.some.test.example.FollTest, I just want FollTest that too in class form)

は、私が最初に単一の要素のために何しようとしているものです。

そして、(例えば)注釈を読み取るための

for (Annotation annotation : ClassName.class.getAnnotations()) { 
      Class<? extends Annotation> type = annotation.annotationType(); 
      System.out.println("Values of " + type.getName()); 

      for (Method method : type.getDeclaredMethods()) { 
       Object value = method.invoke(annotation, (Object[])null); 
       System.out.println(" " + method.getName() + ": " + value); 
      } 
     } 

How do I get above ClassName? (although I have them in the String/Object form as part of list/set).

更新質問:私は、その内部のすべての注釈を取得し、私はクラス名を見つけたら、また、どのように行いますクラスを作り、それらを印刷しますか?

これを参照してください:http://www.java2s.com/Code/Java/Reflection/Getannotationbyannotationclass.htm私は特定のクラスの注釈をどのように取得できますか。

+0

私は質問を更新しました。 –

答えて

0

あなたはClass.forName()

でこれを行うことができる必要があります:

String className = "com.some.test.example.FollTest"; 

Class c = Class.forName(className); 

Annotation[] annotation = c.getAnnotations(); 
+0

私はこれを得る: '[Ljava.lang.annotation.Annotation; @ 4475a0c6'上記のコードからの出力として。 –

+0

最初の部分をトリミングする必要がありますか?私がFollTestに達するまで?また、 'getAnnotations()'はどのように動作しますか? –

+0

forループを使ってアクセスすると、次のようになります。for(int i = 0; i

関連する問題