Questions tagged [annotation-processing]

An annotation processor is a plug-in for the Java compiler. An annotation processor can do such things as analyze declarations, cause compilation errors and generate new compilation units.

An annotation processor is a plug-in for the Java compiler.

An annotation processor is an instance of javax.annotation.processing.Processor whose process method is invoked during compilation.

Once invoked, the processor is then able to do such things as:

  • Analyze parts of the abstract syntax tree, primarily declarations via javax.lang.model.element and types via javax.lang.model.type.
  • Cause compilation errors and warnings (similar to how the @Override annotation works) via the Messager.
  • Generate new compilation units (i.e. source code files) via the Filer which are subsequently compiled.

Notes on the current limitations of annotation processors

  • Annotation processors may only generate new compilation units, not modify existing compilation units. Some annotation processors, such as Project Lombok, achieve the latter via undocumented internal Javac API.
  • The existing API in Java Platform SE is limited to only analyzing declarations, such as class declarations and method declarations. The Compiler Tree API exists, which allows one to analyze the full abstract syntax tree, but it's largely undocumented and not yet a part of SE.

Resources

723 questions
15
votes
4 answers

Maven annotation processing processor not found

I'm new to annotation processing and I'm trying to automating it with Maven. I've put this in my pom.xml: org.apache.maven.plugins maven-compiler-plugin
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
15
votes
3 answers

Debugging Annotation processors in eclipse

I am writing a simple annotation processor and trying to debug it using eclipse. I created a new project for annotation processor and configured javax.annotation.processing.Processor under META-INF as needed and it processes annotations fine. Then,…
14
votes
3 answers

Dagger can not find classes generated by other annotation processor

I have written a simple Annotation Processor (just for fun) that will generate some boilerplate code that I have been writing in my previous project. It actually generates a module like following by collecting annotations on Activity…
14
votes
2 answers

javax.lang.model: How do I get the type of a field?

In java.lang.reflect, one would do: Field someField = ...; Class fieldType = someField.getType(); But what do I do with javax.lang.model's VariableElement (which may or may not represent a field)? A corresponding return value would be (I guess)…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
14
votes
1 answer

Gradle + Annotations + Flavors = won't run annotations processor

I have a Gradle build script that is using an annotations processor (Android Annotations) to generate code. Building was fine until I added a new Pro Flavor. I can build the Free flavor but when I build the Pro flavor the annotations processor…
14
votes
3 answers

Annotation Processor - How to get the Class it is processing

I am trying to write a custom Anntoation processor. The annotation processor will process each class file at compile time to check annotations, But how am i able to get the class that it is currently processing? I am only able to get the class name…
user1004413
  • 2,509
  • 6
  • 23
  • 33
13
votes
1 answer

How to make my own annotation processor incremental?

I've created an annotation processor called EasyPrefs and when I try to use it on my projects, it shows the following warning. Incremental annotation processing requested, but support is disabled because the following processors are not…
Amin
  • 3,056
  • 4
  • 23
  • 34
13
votes
1 answer

Annotation processing, RoundEnvironment.processingOver()

While reading the code of a custom annotation processor in Java, I noticed this piece of code in the processor's process method: @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { if…
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
13
votes
2 answers

Java annotation processing: how do I know if a round is the last one?

When extending AbstractProcessor, there is a possibility to override init(...), but there is no "opposite" method, which would be called after all rounds were processed. This is a problem: when you have to append information collected during each…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
13
votes
5 answers

How to read file from src/main/resources with annotation processor?

I have a simple annotation processor that needs to read a configuration file from the same project as the annotated classes. Example structure: - myproject - src - main - java - my.package.SourceFile - resources -…
Jorn
  • 20,612
  • 18
  • 79
  • 126
13
votes
2 answers

Multi-module annotation processing in Android Studio

I have a project with multiple modules in Android Studio. A module may have a dependency on another module, for example: Module PhoneApp -> Module FeatureOne -> Module Services I've included my annotation processing in the root module but the…
fakataha
  • 785
  • 6
  • 31
13
votes
1 answer

Dagger 2 on Android, missing error messages

I'm using Dagger 2 in my Android project and I'm having trouble debugging it. I know that the compilation fails because of an error in my dagger 2 setup (had it before) but it's almost impossible to track it down because I don't get a proper error…
13
votes
3 answers

How to check if the package exists from inside the annotation processor (in compile-time)?

I'm building an Annotation Processor for Android (for the sake of future explanations, let's call it TestProcessor). The plan is for the processor to operate in two modes: Mode 1: Generation of the code A Mode 2: Generation of the code A and…
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
12
votes
3 answers

JPA 2.0 metamodel in Netbeans?

I've read that since version 6.9, Netbeans includes annotation processing support, a feature needed, for instance, to generate JPA 2.0 entities' metamodels. However, I couldn't find any examples or documentation that shows exactly how to do it. Have…
dariopy
  • 568
  • 1
  • 7
  • 18
12
votes
2 answers

How can I examine the whole source tree with an annotation processor?

I have a lot of handler classes that handle specific message types. To register all these handlers, I need to know which ones exist. Currently, they're all annotated with a specific annotation, and I use a Java 6 annotation processor to get all of…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
1 2
3
48 49