How to use runtime annotation API?

I used several guidelines for processing annotations (APTs) (e.g. 1 and 2 ) on the Internet, and he managed to get it to work during the compilation / assembly process and even get it to work in Eclipse.

Is it possible to use APT at run time to get a list of types (classes) using my annotation.

I wrote something like:

@SupportedAnnotationTypes("com.domain.MyAnnotation")
public class MyAbstractProcessor extends AbstractProcessor {

    public static Map<Element, MyAnnotation> patches = new HashMap<Element, MyAnnotation>();

    @Override
    public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnvironment) {

        // Get all classes that has the annotation
        Set<? extends Element> classElements = roundEnvironment.getElementsAnnotatedWith(MyAnnotation.class);

        // For each class that has the annotation
        for (final Element classElement : classElements) {

            patches.put(classElement, annotation);

So, MyAbstractProcessor.patches will be populated with a list of classes using annotation. A noble idea, in addition to the flaw that this APT performs during the build, and does not run the time.

Is it possible to use APT at run time?

Or am I using the wrong framework to get what I want?

+3
2

- getAnnotations.

( ) , - - , .

- .

+4

, RetentionPolicy? , @Retention .

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnnotation {
    String[] parameters();
    String[] exceptions();
}
+1

All Articles