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

Integrating javassist byte code manipulation with maven compilation

I have a maven project which compiles with javac / aspectj compiler. I want to run on classes which were compiled a javassist program which manipulate the compiled classes and add stuff to them. I thought using the "process-classes" phase to run my…
5
votes
1 answer

Resource IDs will be non final in AGP 7.0

I have an annotation processing library that generates RecyclerView adapters in compile time. I'm currently rebuilding it from the ground up with many changes and improvements, but while testing, I received a warning stating: Resource IDs will be…
Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
5
votes
1 answer

Adding annotations with Java Annotation Processor

I know that the Annotation Processor is normally used to consume annotations and react to them. I, however, have a use case where this "reaction" involves adding other annotations. Can this be done within the processor itself? If so, how?
Eric
  • 1,343
  • 1
  • 11
  • 19
5
votes
0 answers

Java 11: Compiler does not recognize annotation processors specified in module-info.java

I have a Maven project called Annot which contains an annotation "MyAnnot" and a processor for it and another plain Java project called UseAnnot that contains MyAnnot(a class called User.java is annotated with it. When I specified that UseAnnot…
user
  • 7,435
  • 3
  • 14
  • 44
5
votes
0 answers

kapt replaces generated class references with error.NonExistentClass despite kapt.correctErrorTypes being enabled

I have a custom annotation processor that does roughly this: generate an annotation type (classes using this type are deferred until later rounds) in a later round, process the classes using this type and generate some more files for them This has…
Namnodorel
  • 385
  • 5
  • 17
5
votes
4 answers

How to implement build specific annotation retention in Java

I have an annotation that I currently use only for internal build and documentation purposes. It does not offer any value at runtime, which is why I chose @Retention(SOURCE): @Retention(SOURCE) public @interface X However, in order to validate its…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
5
votes
1 answer

How to get all elements with an annotation in an IntelliJ incremental build?

I'm writing an annotation processor which needs to collect all the classes with a certain annotation in the current module and write a class referencing each of them. To simplify a bit, given these source…
Dan Berindei
  • 7,054
  • 3
  • 41
  • 48
5
votes
1 answer

Checking if kapt uses incremental annotation processing

In Kotlin 1.3.30 support for incremental annotation processing was added: https://blog.jetbrains.com/kotlin/2019/04/kotlin-1-3-30-released/ According to the doc: Note that in the current implementation, using any non-incremental annotation…
Piotr Zawadzki
  • 1,678
  • 1
  • 18
  • 24
5
votes
1 answer

How to read a Class[] values from a nested annotation in an annotation processor

I am trying to generate some code using Java annotation processing tools, I have nested annotations where the parent annotation value is an array of the child annotation, and the child annotation value is an array of classes. Annotations: public…
Ahmad Bawaneh
  • 1,014
  • 8
  • 23
5
votes
1 answer

Change KAPT class generation path

I want to instruct my Kotlin annotation processor to change the output directory of the generated classes. I want from my Gradle build script to change the kapt.kotlin.generated argument. I have tried the following to no avail. Doesn't work, path…
iFanie
  • 916
  • 8
  • 10
5
votes
1 answer

Java Annotation Processor: check if TypeMirror implements specific generic interface

I'm writing annotation processor, and I need to check if particular TypeMirror implements specific interface. The question Java Annotations Processor: Check if TypeMirror implements specific interface provides an answer for non-generic…
AreSo
  • 643
  • 4
  • 13
5
votes
0 answers

IntelliJ doesn't pick up generated .class files

I have implemented an AnnotationProcessor that picks up class annotations that take a string argument. The string argument is an expression in a domain-specific language, which the annotation processor will use to compile a class file. I create a…
5
votes
2 answers

Android Studio 2.4 + Lombok annotationProcessor configuration confusion

I using lombok on some project, and with the new Android Studio 2.4 Update now (Preview) I get this strange error: What went wrong: Execution failed for task ':core:javaPreCompileRelease'. Annotation processors must be explicitly declared now. …
5
votes
1 answer

Android Annotation Processor accessing Annotated classes from different modules

I'm having an Android Studio project with 2 modules: A and B. (I do not include here the Annotation Processor and the Annotations module) B depends on A. B is an Android Library Module, and A is simple Java library Module. I'm also having an…
5
votes
2 answers

Java annotation processor - Annotated Kotlin classes unit tests

I have an annotation processor library that I would like to get working for Kotlin, however I have hit a snag in terms of my unit testing. I would really appreciate if someone could give me some advice. My current unit testing implementation uses…