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

How do I strip annotations from local variables in Java annotation processors?

Due to a specific reason, I would like to use Checker Framework and its subtyping checker. To make this checker work I have to use ElementType.TYPE_PARAMETER and ElementType.TYPE_USE. However, I would like to remove them from local variables before…
0
votes
3 answers

Databinding not incremental on AGP 4.0.1

I'm currently using Android Gradle Plugin 4.0.1 and it seems like Databinding is not incremental on AGP 4.0.1, as you can see on the below image: android.databinding.annotationprocessor.ProcessDataBinding I've checked the dependencies of the…
0
votes
0 answers

Finding balanced number of samples

I have a annotation dataset consisting of multiple text files in a folder. I have more than 100000 text samples. The folder structure looks like this annotation/ ....Sample0.txt ....Sample1.txt ....Sample2.txt .... .... …
Dev3344
  • 1
  • 1
0
votes
1 answer

Eclipse annotation processing not finding class on classpath

We use Eclipse Annotation processing with the Velocity code generation framework. A while ago, we updated all our code and builds to use Java 11 - and didn't notice that the code generation no longer worked. (we haven't need to change any of the…
CasaDelGato
  • 603
  • 7
  • 17
0
votes
1 answer

Configurable annotation processors in maven modules

We have multiple services that are using certain annotation processors. All these services have 2 things in common - the same parent pom & a specific pom-import-dependency in dependencyManagement. In the individual services we define processors…
Mukund Jalan
  • 1,145
  • 20
  • 39
0
votes
1 answer

How can I include kotlin-reflect in the classpath of the Bazel compiler?

I'm trying to get moshi-kotlin-codegen to run on some Kotlin code via Bazel. After a lot of trial and error, I managed to get the plugin to run, but it's failing due to not having kotlin-reflect on the classpath. This is needed by kotlinpoet, which…
Petter Måhlén
  • 225
  • 2
  • 9
0
votes
1 answer

Can AbstractProcessor detect if annotated method has compilation errors?

My AbstractProcessorimplementation gets called even though the annotated method contains code that results in compiler errors. (I.e. the processor is triggered by the presence of an annotation whose target is ElementType.METHOD). Having experimented…
Hervian
  • 1,066
  • 1
  • 12
  • 20
0
votes
1 answer

Java runtime equivalent to annotation processing with javax Processor / google @AutoService

I have worked with annotation processing on RetentionPolicy.SOURCE level, using javax.annotation.processing.Processor/AbstractProcessor and com.google.auto.service.@AutoService before, that's when I first discovered the very helpful…
Raphael Tarita
  • 770
  • 1
  • 6
  • 31
0
votes
0 answers

Gradle Java/Kotlin Force Annotation processor to always work

In my Kotlin Gradle project I have a few annotation processors and one of them is designed to look into resources directory of the module and generate some code based on resources folder contents. It is something like R.java file for Android but…
Andranik
  • 2,729
  • 1
  • 29
  • 45
0
votes
1 answer

kapt not enabling incremental annotation processing

For some reason, Kapt complains that incremental compile is not enabled on one of my modules. However, I see no reason why is shouldn't be. The warning message when running core:kaptKotlin [WARN] Incremental annotation processing requested, but…
mdsimmo
  • 559
  • 5
  • 16
0
votes
1 answer

How to create a resource with spaces in file name from Annotation Processor

When generating resource file on compile time using javax.annotation.processing.Processor, not able to create files with spaces in the file name. Simplified code to reproduce the problem: public class SampleAnnotationProcessor extends…
Andrey
  • 6,526
  • 3
  • 39
  • 58
0
votes
0 answers

Requiring an annotation field when some other field has a specific value

If I have a field annotation defined as such: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface LineField { public int at(); public int length(); public String literal() default "[unassigned]"; public…
Snedden27
  • 1,870
  • 7
  • 32
  • 60
0
votes
0 answers

Can you "hijack" code suggestions for a certain class in InteliJ?

So I want is a plugin that changes what is suggested to the Developer for accordingly annotated classes. The idea is to combine this with a code-generating annotation where I want the developers to have suggestions for the code (added methods) that…
0
votes
1 answer

Java annotation processor: javax.validation.constraints.Past annotation changed field type

Java annotation processor: javax.validation.constraints.Past annotation changed field type. E.g., public class Foo { @javax.persistence.Temporal(TemporalType.TIMESTAMP) private Date date; } for (Element element :…
eastwater
  • 4,624
  • 9
  • 49
  • 118
0
votes
1 answer

Can we self-generate a client out of a Rest controller using annotation processing ? If we can then how I can proceed?

(https://github.com/aashrai/brahma-dao), similar to this DAO generator can we do annotation processing to generate a client for a rest controller ? PS : I am using Spring Boot with gradle.