简体   繁体   中英

How to exclude java classes from being compiled with annotation processing?

I have two annotations:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Ann {
    public String description() default "AAA";
    public String template() default "BBB";
}

and

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface AnotherAnn {
    public String text() default "Text";
}

simple class with annotated method:

public class App {
    @Ann(description = "Desc", template = "Templ")
    public void someMethod(){}
}

I want to create new copy of App class and replace @Ann annotaion in the copy class (before someMethod method) to @AnotherAnn .
Eq Copy of App class after annotation processing:

public class AppCopy {
    @AnotherAnn(text = "Text")
    public void someMethod(){}
}

And then I should exclude App class from being compiled. Processor:

@SupportedAnnotationTypes("alexiuscrow.annotation.Ann")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Proc extends AbstractProcessor {

    public Proc() {
        super();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        /* TODO CREATE COPY & EXCLUDE ORIGINAL */
        return true;
    }
}

How can I do it?

Here are two general approaches for excluding certain Java classes from annotation processing:

  1. run the compiler twice. The first time, compile the entire project without running an annotation processor. The second time, the command line should include the annotation processor and should list only the files for which you want to run the annotation processor. Only files that the compiler compiles are subject to annotation processing. That includes files passed on the command line, plus any that those depend on whose .class files are not up to date.

  2. write logic in the annotation processor to skip processing of certain files.

Approach #1 is the one I use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM