I am using java annotation processing API with JavaPoet to generate a file and maven to compile my project.
It works as expected, resulting file is generated under /target/generated-sources/annotations/sk/typek/generated/TestGeneratedClass.java
.
However, any subsequent mvn compile
fails with Caused by: javax.annotation.processing.FilerException: Attempt to recreate a file for type sk.typek.generated.TestGeneratedClass
.
I know i can just call mvn clean
before every recompiling (then it works again), but i dont want to do that. Other annotation processing libraries i used before didnt have this problem so they handled it somehow.
Is there any way to "force overwrite" if generated file already exists? Or is it possible to delete old generated file (if it exists) as a part of my annotation processing code?
I searched for solution but couldnt find anything useful.
My annotation processor looks like this:
package sk.typek.annotation;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
@SupportedAnnotationTypes("sk.typek.annotation.TestAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class TestAnnotationProcessor extends AbstractProcessor {
private Filer filer;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.filer = processingEnv.getFiler();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
if (annotations.isEmpty()) {
return false;
}
System.out.println("Processing annotations: " + annotations);
try {
TypeSpec.Builder classBuilder = TypeSpec.classBuilder("TestGeneratedClass")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
JavaFile javaFile = JavaFile.builder("sk.typek.generated", classBuilder.build())
.build();
System.out.println("Writing file TestAnnotation to package " + javaFile.packageName);
// tried this doesnt work
if (javaFile.toJavaFileObject().delete()) {
System.out.println("Deleted previously generated file");
}
javaFile.writeTo(filer);
} catch (IOException e) {
throw new DiffBuilderAnnotationProcessorException(e);
}
return true;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton(TestAnnotation.class.getCanonicalName());
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}