CodexBloom - Programming Q&A Platform

Java 11: implementing Custom Annotation Processor Not Recognizing Annotations in Module System

👀 Views: 233 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-02
java annotation-processing java11 gradle module-system Java

I can't seem to get This might be a silly question, but I'm working with an scenario with my custom annotation processor not recognizing certain annotations when using the Java 11 module system. I've created an annotation processor to generate boilerplate code for my project, but when I compile my modules, it seems the processor isn't picking up the annotations as expected. Here's how I set up my build.gradle: ```groovy plugins { id 'java' id 'java-library' } java { modularity.inferModulePath = true } dependencies { annotationProcessor 'com.squareup:javapoet:1.13.0' } ``` I've defined my annotations like this: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface MyAnnotation { String value(); } ``` And my annotation processor looks something like this: ```java import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import java.util.Set; @SupportedAnnotationTypes("MyAnnotation") @SupportedSourceVersion(SourceVersion.RELEASE_11) public class MyAnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) { // Generate code based on the annotation } return true; } } ``` However, when I run the build, I get the following behavior: ``` behavior: MyAnnotationProcessor: processing failed. Unable to process annotations for module 'my.module.name'. ``` I've also tried adding the `--add-modules` option during compilation, but it doesn't seem to help. I've ensured that my processor is correctly listed in the `META-INF/services/javax.annotation.processing.Processor` file. Does anyone have insights on how to make the annotation processor recognize annotations in the module system, or am I missing some configuration in my Gradle setup? Any suggestions would be greatly appreciated! Is there a better approach? This is my first time working with Java stable. What's the correct way to implement this?