Cyril Lee
Cyril Lee

Reputation: 31

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.

Upvotes: 3

Views: 1277

Answers (1)

Sébastien B.
Sébastien B.

Reputation: 545

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.

Upvotes: 6

Related Questions