简体   繁体   中英

Java runtime equivalent to annotation processing with javax Processor / google @AutoService

I have worked with annotation processing on RetentionPolicy.SOURCE level, using javax.annotation.processing.Processor/AbstractProcessor and com.google.auto.service.@AutoService before, that's when I first discovered the very helpful RoundEnvironment.getElementsAnnotatedWith() .

Now, in a completely unrelated project, I need the same functionality, but at runtime. In other words, I have the Annotation

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Mapper {
    Class<?> forUID() default Object.class;
}

and I want to obtain every single element (of course it can only be constructors or methods) anntotated with @Mapper . Can I use RoundEnvironment at runtime? If yes, how do I obtain an instance of it? If no, is there a runtime equivalent to it? Thanks in advance.

Using:

  • JDK 11 (openjdk11)
  • Maven 3.6.3

Annotation processing is a compile time only tool, the retention on the annotation is just saying if the annotation will be available at runtime or just in source, for run time you can use reflection, but also you still can use annotation processor at compile time to generate a class that list all the types annotated with a specific annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM