Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically remove the file generated by Java 7 Annotation Processor when delete the annotation in source file

I am writing a java annotation processor for java 7 source code. And surely, I can use javax.annotation.processing.filer to help me generate the file under project directory automatically.

ex: annotation is @becare

public interface test {
   @becare
   int compare(int a, int b);
}

my annotation processor's job is when it detects the given annotation @becare, it will generate the file for me.

My Question is that if I remove the annotation from the previous code snippet, can I let annotation processor to be aware that and delete the file it just created?

Or is there any workaround to help me achieve this ?

Thanks in advance.

like image 369
Cyril Lee Avatar asked Mar 20 '26 06:03

Cyril Lee


1 Answers

When you create the generated file you declare that it's linked to your 'test' interface like this:

Elements eltUtils = processingEnv.getElementUtils(); filer.createSourceFile("testGenerated", eltUtils.getTypeElement("test"));

When the source is deleted, the processor will remove generated file like javadoc says:

If there are no originating elements, none need to be passed. This information may be used in an incremental environment to determine the need to rerun processors or remove generated files. Non-incremental environments may ignore the originating element information.

like image 151
Sébastien B. Avatar answered Mar 21 '26 19:03

Sébastien B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!