I am writing a simple java application wherein some classes if I annotate with my custom @Log
annotation should print the message “found @Log annotation at <class_name>”. I am trying to experiment with custom annotation processors. For that I wrote a second project which will contain my custom annotation & its processor as follows:
@SupportedAnnotationTypes ("Log") @AutoService({Processor.class}) public class LogProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (TypeElement typeElement : annotations) { for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) { processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "found @Log annotation at " + element); } } return true; } }
And my custom @Log annotation code is as follows:
@Target (ElementType.TYPE) public @interface Log {}
I am using @AutoService
for registration of the annotation processor. How do I include the .jar file generated on running a gradle build? I tried the following way:
repositories { mavenCentral() flatDir { dirs 'libs' } } dependencies { annotationProcessor name: 'AnnotationSupplier-1.0-SNAPSHOT' }
or
dependencies { annotationProcessor files('/libs/AnnotationSupplier-1.0-SNAPSHOT.jar') }
But that is not working, and I am not sure how else to add a annotation processor jar. My expectation is that upon running the project with my AnnotationSupplier-1.0-SNAPSHOT added, I should be able to see “found @Log annotation at <class_name>” in the console, which I am not.
Answer
For me the problem was solved by adding the annotation processor module under the parent project (by creating a new module) and adding the following in the parent build.gradle
:
dependencies { annotationProcessor project(':AnnotationProcessor') compile project(':AnnotationProcessor') }
Note 1: AnnotationProcessor is the name of my annotation processor module which I created under the parent project.
Note 2: I am doing this in IntelliJ IDE.
Note 3: Make sure to activate annotation processing for the project