Java Annotation Processing doesn't generate new class
I'm trying to do an example annotation processing. It doesn't generate any new class after compilation, I've seen many videos about it and cannot fix the problem. I don't know where it is. I'm using JavaPoet and Google AutoService. My code: // ExampleProcessor.java @AutoService(Processor.class) @SupportedAnnotationTypes("com.example.ExampleAnnotation") @SupportedSourceVersion(SourceVersion.RELEASE_17) public class ExampleProcessor extends AbstractProcessor { @Override public boolean process(Set
I'm trying to do an example annotation processing. It doesn't generate any new class after compilation, I've seen many videos about it and cannot fix the problem. I don't know where it is. I'm using JavaPoet and Google AutoService.
My code:
// ExampleProcessor.java
@AutoService(Processor.class)
@SupportedAnnotationTypes("com.example.ExampleAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_17)
public class ExampleProcessor extends AbstractProcessor {
@Override
public boolean process(Set annotations, RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(ExampleAnnotation.class)) {
ExampleAnnotation annotation = element.getAnnotation(ExampleAnnotation.class);
String className = annotation.value();
MethodSpec mainMethod = MethodSpec.methodBuilder("sayHello")
.addModifiers(javax.lang.model.element.Modifier.PUBLIC, javax.lang.model.element.Modifier.STATIC)
.returns(void.class)
.addStatement("$T.out.println($S)", System.class, "Hello, " + className + "!")
.build();
TypeSpec generatedClass = TypeSpec.classBuilder(className)
.addModifiers(javax.lang.model.element.Modifier.PUBLIC)
.addMethod(mainMethod)
.build();
JavaFile javaFile = JavaFile.builder("com.example.generated", generatedClass)
.build();
try {
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
// ExampleAnnotation.java
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface ExampleAnnotation {
String value();
}
// MyClass.java
@ExampleAnnotation
public class MyClass { }
// build.gradle.kts
plugins {
id("java")
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.squareup:javapoet:1.13.0")
annotationProcessor("com.google.auto.service:auto-service:1.1.0")
implementation("com.google.auto.service:auto-service-annotations:1.1.0")
implementation("org.jetbrains:annotations:20.1.0")
}
tasks.withType {
options.annotationProcessorPath = configurations["annotationProcessor"]
}
I've been watching many YouTube videos and searched internet but nothing useful found. And yes, I have Annotation Processor enabled in my Intellij Idea.