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
11
votes
4 answers

eclipse java annotation processing

I want to do something using Annotation processing in eclipse jdt. Eclipse jdtapt help tells me I should configure things on the java/compiler/annotation processing preferences page. But on my installation (Indigo), the compiler section doesn't have…
Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
11
votes
3 answers

JUnit not working with Lombok - annotation processing doesn't seem to work for test classes

I have a problem with Lombok and JUnit. I am using IntelliJ Idea, the latest one, with Lombok plugin installed and annotation processing enabled. I have an entity class: @Data @Builder @AllArgsConstructor public class User { private String…
11
votes
1 answer

How to access TypeUse annotation via AnnotationProcessor

Question: Is it possible to access elements annotated with a @Target(ElementType.TYPE_USE) annotation via an annotation processor? Is it possible to access the annotated type bounds via an annotation processor? Links to related documentation I…
Trinova
  • 443
  • 6
  • 18
11
votes
3 answers

Java Annotation processor for remote JAR

General question I have two projects A and B; B has a dependency on A. I want to generate some code in B with an Annotation Processor, based on annotations on objects in A. When I run the compilation with the correct Processor implementation, only…
blagae
  • 2,342
  • 1
  • 27
  • 48
11
votes
2 answers

Types isAssignable and isSubtype misunderstanding

In writing an Annotation Processor using the Java 6 API, I came across a need to handle all Maps in a particular fashion, but I'm clearly misunderstanding what the API is intended to do or how to invoke it. Here's the code that's making me…
Patrick
  • 2,672
  • 3
  • 31
  • 47
10
votes
1 answer

How to authorize specific resources based on users who created those in REST, using annotations

I do not understand Java annotations with retention policy as RUNTIME that well. What I'm trying to do is create an annotation named @Authorize and use it on methods which needs user authorization in order to perform some action( the user is already…
10
votes
3 answers

How to use custom type annotations in Java

Java 8 has feature called Type annotations (JSR 308). I would like to use it for simple Object to Object mapper framework. I would like define annotation @ExpectedType like this @Target({ElementType.TYPE_PARAMETER,…
Boris Šuška
  • 1,796
  • 20
  • 32
10
votes
2 answers

Compiler options missing in Android Studio >= 0.8.2

Recently I upgraded my Android Studio to 0.8.2. Now, my android-annotations based project fails to build. It seems like annotation processing is disabled somehow. It seems the project compiler settings have been changed, the option to turn…
Rens Verhage
  • 5,688
  • 4
  • 33
  • 51
10
votes
3 answers

How to run annotation processor in eclipse on save

Currently I generate files with an annotation processor in eclipse for a project by Right click on project > Run As > Maven Clean Right click on project > Run As > Maven install This is quite time consuming. How do I set up eclipse to make it run…
AndiDev
  • 1,272
  • 14
  • 26
10
votes
3 answers

How to step-debug annotation processor during compile?

I have an annotation processor for an annotation of retention policy=SOURCE. I have not idea how to step-debug it. I have issued print statements, logger info when I run mvn install, compile or package or ant javac, and I see their sysouts in the…
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
9
votes
2 answers

processing of annotations inside a method body

I am processing java annotations using the Pluggable Annotation Processing API. Is it somehow possible to also process annotations used inside a method body? thanks for help. Peter
wrm
  • 1,898
  • 13
  • 24
9
votes
1 answer

Unable to use Maven BOM in gradle 5 with annotationProcessor configuration

I am trying to use maven BOM with gradle 5.1.1 as mentioned below ext { set('spring-boot-dependencies.version', '2.1.2.RELEASE') } apply plugin: 'java' group 'com.acme' version '1.0.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { …
Ashok Koyi
  • 5,327
  • 8
  • 41
  • 50
9
votes
1 answer

Nullable types in kotlin annotation processor

I'm working on annotation processor for Kotlin and because the processed elements are in Java I don't receive nullables as ? instead with a @Nullable annotation and that's fine, but I'm facing a problem with receiving null parameters in types and…
Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
9
votes
2 answers

Java: reflection (at runtime) versus mirroring (at annotation processing)

I understand the general differences between the concepts of reflection (done at runtime using Class, Method, Field, Annotation, ...), and mirroring (done during annotation processing using TypeMirror, TypeElement, ...). But can someone please…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
9
votes
4 answers

Can't Debug an Annotation Processor when using kapt and gradle

I'm building an annotation processor, and I recently switched from using the default annotationProcessor type to kapt, using the kotlin-kapt plugin. I was debugging my processor by using the command ./gradlew --no-daemon -Dorg.gradle.debug=true…