AbstractProcessor
から継承するクラスのメソッドprocess()
を使用して、私の注釈を使用する要素の名前を取得します。注釈を使用する要素を保存するにはどうすればいいですか?
どのようにその名前のリストを保存すると、私はそれらを私の例で使用することができます。 main()
方法?私はインターフェイス@CustomAnnotationここ
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation{
}
はの実装である必要があり、私はここで@CustomAnnotation
public class ClassList {
private static final List<String> classList= new LinkedList<>();
public static List<String> getClassList() {
return classList;
}
public static void addList(String name) {
classList.add(name);
}
}
アノテーションを使用するクラスの名前を格納するクラスClassList
を持って
@editアノテーションを処理するクラス
@SupportedAnnotationTypes("com.example.CustomAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class CompileTimeAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
for (Element e : elements) {
//adds each name to the list
ClassList.addList(e.getSimpleName().toString());
}
//prints the array
System.out.println(Arrays.toString(ClassList.getClassList().toArray(new String[ClassList.getClassList().size()])));
return true;
}
}
私はそのクラスにアクセスしたい
main()
方法を持っている場合
は、その後、私は今、注釈
@CustomAnnotation
public class AnnotatedClass {
}
@CustomAnnotation
public class TestClass {
}
を使用しています。
public static void main(String[] args) {
System.out.println(ClassList.getClassList().size());
}
私はきれいにし、実際にそれを構築する方法process()
に配列を出力しますが、その後、私はリストが空のメインプログラムを実行します。メインプログラムでそのリストにアクセスするには?
あなたの質問は明確ではありません。あなたの実際のコードと達成したいことの詳細を編集してください。 – Mxsky
あなたの質問を精緻化したり、コードを追加したり、もっと文脈を教えてください。 –
私はいくつかのコードで私の投稿を編集しました – Humberd