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
6
votes
1 answer

Checking for absence of super class in annotation processor

When obtaining a TypeElement in an annotation processor, you can ask for its super class (or more specifically, the TypeMirror of it) using method getSuperClass(). According to the JavaDoc, a type that doesn't explicity extend anything (in other…
G_H
  • 11,739
  • 3
  • 38
  • 82
6
votes
0 answers

AnnotationProcessing - Generating Files at Each Round vs at Last Round

I was playing around with annotation processing and was unable to use generated files directly via an import in my code. Instead I had to prepend the generated class with its complete package. I posted a SO question error: package generated.schema…
Abbas
  • 3,529
  • 5
  • 36
  • 64
6
votes
1 answer

Get default value of kotlin fun parameter in annotation processing phase

Background: I have an annotation processor that builds retrofit interfaces by scanning spring annotations on controllers. I have it set up to work in either kotlin or java based spring applications, and it can generate either kotlin or java…
StormeHawke
  • 5,987
  • 5
  • 45
  • 73
6
votes
0 answers

Trying to run ButterKnife in AOSP, returning NullPointerException

Trying to test the application with butterknife 8.4.0 and some sample text and I keep getting a NullPointerException The actual code itself is fine but I believe Android.mk might be the issue. So here are the portions of that makefile that are under…
6
votes
2 answers

Annotation processor @autoservice

Need help with annotation processor . I have created a simple annotation processor which uses @autoservice annotation that checks whether the field which is annotated is final. But it is not showing any compile time errors. This is my…
amt14779
  • 125
  • 1
  • 1
  • 5
6
votes
1 answer

Is it possible to get all TypeElements in a package in an annotation-processing environment?

I have a couple of packages in a library that contain annotations and base classes. I'm extending javax.annotation.processing.AbstractProcessor in the context of this question. Is there a way to get a List/Array/Set of java.lang.TypeElement for all…
Preston Garno
  • 1,175
  • 10
  • 33
6
votes
0 answers

How to get the compile-time classpath from an AnnotationProcessor?

I am trying to read the compile time classpath inside an AnnotationProcessor, but I cannot find out how. My annotation processor needs to save the classpath of a build to file, so it can be used when analyzing this build by a third party library…
jactor-rises
  • 2,395
  • 2
  • 22
  • 44
6
votes
2 answers

Disable incremental build for kapt

Since android gradle plugin has enabled incremental build by default annotation processing breaks, because only those classes who has been changed since last incremental build will be taken into account from annotation processors. So for java source…
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
6
votes
2 answers

Identify stability of types in Java annotation processor

I would like to write an annotation processor that generates source code based on the set of JavaBeans properties of processed types. This works in general, but I am struggling with doing so correctly if other annotation processors are around.…
Gunnar
  • 18,095
  • 1
  • 53
  • 73
6
votes
1 answer

How can I use Dagger2 in IntelliJ on java projects

I want to use Dagger in IntelliJ but I can't use it. Dagger uses an annotation processor and I guess IntelliJ doesn't know about the annotation processor. You can see the generated java file, it's generated by the Dagger2 compiler, but my java…
Kh Jung
  • 313
  • 1
  • 9
6
votes
1 answer

Get TypeElement from Generic TypeParameterElement for Java Annotation Processor?

Using Java Annotation Processors I have the following type: @NameToken(value={"startPage"}) public interface MyProxy extends Proxy { } and: public interface Proxy { } I have the TypeElement of Proxyas: TypeElement…
Michael
  • 32,527
  • 49
  • 210
  • 370
6
votes
3 answers

How do I use custom Java Annotation Processor in Gradle?

I've been working on a simple java annotation processor that extends AbstractProcessor. I've been able to successfully test this using javac -Processor MyProcessor mySource.java The problem is integrating this into a simple Hello World android…
James
  • 2,445
  • 2
  • 25
  • 35
6
votes
2 answers

Annotation processor in Gradle outputs source files to build/classes making javadoc fail. How to fix it?

I have an annotation processor that is automatically picked up by the Java compiler at build time (using SPI). During a gradle build, the generated java sources of this annotation processor are put in build/classes as Gradle tells the annotation…
Zubzub
  • 782
  • 7
  • 18
6
votes
2 answers

How to get methods in source order

I have a custom annotation that I want to use at runtime to display object properties. I would like them to appear in source code order but reflection does not guarantee any particular order for Class.getMethods(). Is there a way, either via…
Rangi Keen
  • 935
  • 9
  • 29
6
votes
7 answers

Setting the generated source directory for annotation processors in Maven

I'm trying to move a build which generates sources using an annotation processor to Maven. I've tried configuring the maven-compiler-plugin as follows: org.apache.maven.plugins
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278