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
115
votes
9 answers

How to generate the JPA entity Metamodel?

In the spirit of type safety associated with the CriteriaQuery JPA 2.0 also has an API to support Metamodel representation of entities. Is anyone aware of a fully functional implementation of this API (to generate the Metamodel as opposed to…
Andrey
  • 8,882
  • 10
  • 58
  • 82
59
votes
4 answers

Java 6 annotation processing -- getting a class from an annotation

I have an custom annotation called @Pojo which I use for automatic wiki documentation generation: package com.example.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import…
Ralph
  • 31,584
  • 38
  • 145
  • 282
45
votes
8 answers

Is it possible to use Lombok with Kotlin?

I have a Kotlin Gradle project. I added Lombok as a dependency and also registered it with kapt compileOnly("org.projectlombok:lombok:$lombokVersion") kapt("org.projectlombok:lombok:$lombokVersion") I would like to use the @Slf4j annotation for…
ps-aux
  • 11,627
  • 25
  • 81
  • 128
45
votes
7 answers

How can I add a generated Source Folder to my Source Path in Gradle?

I use annotation processing. Therefore I use the apt plugin. It generates new java sources in build/source/apt. Here is my build.gradle: apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'apt' apply plugin: 'war' apply plugin: 'gwt' apply…
Michael
  • 32,527
  • 49
  • 210
  • 370
38
votes
3 answers

Specifying order of annotation processors

I'm trying to run Dagger 2 as well as Lombok on my Java project. Lombok has to run first, of course, but whether it actually does seems to be up to chance. At first I suspected I could specify the order by the respective position of the library jars…
Torque
  • 3,319
  • 2
  • 27
  • 39
38
votes
7 answers

Maven 3 - How to add annotation processor dependency?

I need to run an annotation processor on my project's sources. The annotation processor should not become a transitive dependency of the project since it's only needed for annotation processing and nothing else. Here is the complete (non-working)…
bernie
  • 9,820
  • 5
  • 62
  • 92
33
votes
7 answers

How to write automated unit tests for java annotation processor?

I'm experimenting with java annotation processors. I'm able to write integration tests using the "JavaCompiler" (in fact I'm using "hickory" at the moment). I can run the compile process and analyse the output. The Problem: a single test runs for…
Arne Deutsch
  • 14,629
  • 5
  • 53
  • 72
31
votes
3 answers

Gradle deprecated annotation processor warnings for lombok

After upgrading to gradle 4.7, my previously warning-free build now emits this warning: The following annotation processors were detected on the compile classpath: 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor' and…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
29
votes
9 answers

Android javax.annotation.processing Package missing

I would like to do some annotation processing based on the example in the following link: http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm. However, I would like to implement this in my Android project, and it…
srowley
  • 837
  • 1
  • 12
  • 24
29
votes
2 answers

Automatically generate .factorypath on project import when using Maven project in Eclipse IDE

The .factorypath file is a generated file, which eclipse requires for annotation-processing. The m2eclipse plugin does generate this file when using "Update Maven Project" (Alt+F5) and checking "Update project configuration from pom.xml". However, I…
Dag
  • 10,079
  • 8
  • 51
  • 74
29
votes
4 answers

Annotation Processor in IntelliJ and Gradle

tl;dr: I cannot configure IntelliJ to generate the java files in the same directory as gradle I have a small project which uses the immutables annotation processor. It works as expected in the gradle command line build, but I cannot get IntelliJ to…
TmTron
  • 17,012
  • 10
  • 94
  • 142
28
votes
2 answers

Databinding annotation processor kapt warning

In my app module's build.gradle, I have added dependencies { kapt('com.android.databinding:compiler:3.1.2') ... } but I'm still receiving the compiler warning for app: 'annotationProcessor' dependencies won't be recognized as kapt annotation…
cren90
  • 1,367
  • 2
  • 17
  • 30
26
votes
1 answer

Forward compatible Java 6 annotation processor and SupportedSourceVersion

I am trying out Java 7 for one project and getting warnings from annotation processors (Bindgen and Hibernate JPA modelgen) of this sort: warning: Supported source version 'RELEASE_6' from annotation processor…
bernie
  • 9,820
  • 5
  • 62
  • 92
25
votes
1 answer

Annotation processor-generated Errors/warnings not showing in Eclipse editor or Problems view

I have written a customer annotation processor to generate various source files, wrapped in an Eclipse plugin. As part of this process it also logs various errors and warnings using the usual call…
Overlord_Dave
  • 894
  • 10
  • 27
24
votes
3 answers

Code replacement with an annotation processor

I'm trying to write an annotation processor to insert methods and fields on a class... and the documentation is so sparse. I'm not getting far and I don't know if I'm approaching it correctly. The processing environment provides a Filer object which…
Boann
  • 48,794
  • 16
  • 117
  • 146
1
2 3
48 49