0
@SupportedAnnotationTypes({"com.tg.annotation.Table", "com.tg.annotation.Test"})
public class TgDaoGenerateProcessor extends AbstractProcessor {
    private Messager messager;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        messager = processingEnv.getMessager();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        messager.printMessage(Diagnostic.Kind.NOTE, "annotations size " + annotations.size());
}

In my project have a class annotated @Table and a class annotated @Test

I clean output directory and build in Intellij, output two lines:

annotations size 2
annotations size 0

why have two lines? what have javac done?

And if I rebuild it nothing is printed. I guess I don't modify source code so javac will not generate new .class. If I modify a class annotated with @Table and rebuild, the output is: annotations size 1. APT get @Table info, can't get @Test annotated class info, because i don't modify class annotated @Test?

I want to get class info annotated @Table and @Test and use those to generate an xml file. Give a example:class A annotated @Table and class B annotated @Test and APT will get class A's and B's fields and methods , then write them into a new file. So if only modify class A , to change some fields and methods.Build and the APT Processor can't get class B that annotated @Test, missing those info I can't generate a latest xml file. Sure, I clean output directory and rebuild, it will work, but nobody willing to do that right. So change anyone class, how to make APT Processor can get all class info.

twogoods
  • 1,646
  • 2
  • 14
  • 21

1 Answers1

1

It is hard to answer when you are asking multiple questions at once (and doing so is against StackOverflow policies)… let's do away with less relevant issue first:

why have tow line ? what have javac done?

This is because of multiple processing rounds. Multiple rounds are required to process annotations on classes, generated by annotation processors. See documentation of Processor for details.

and I rebuild it nothing output

Don't rely on this. You haven't specified your build system (IntelliJ Ant? Maven? Gradle?) Different build systems handle incremental compilation in different ways, and some of them might completely disable incremental build when the project uses annotation processors. Most notably, Android Gradle plugin, and recent versions of Gradle itself have disabled incremental compilation to work around it's poor compatibility with annotation processors.

If you need easy way, have users of your annotation processor disable incremental compilation too. Alternatively, you can redesign your processor to be compatible with incremental compilation. This is a complex task, and I recommend you to ask a separate question, if you are interested in that.

I modify a class annotated @Table and rebuild, output annotations size 1

Don't use first argument of process as indication of anything. If you need to get annotated elements, call getElementsAnnotatedWith for each annotation you are interested in.

I want to get class info annotated @Table and @Test, use those to generate a xml file

You haven't described exact goals of your processor and the purpose of @Table and @Test annotations, but if one of classes references another (e.g. @Table-annotated class contains @Test-annotated types in it's method signatures), you can use getTypeElement to access that class, even if it is not returned by getElementsAnnotatedWith.

user1643723
  • 4,109
  • 1
  • 24
  • 48