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

How to process annotations with @Target(ElementType.TYPE_USE)?

I'm implementing an annotation processor to make sure that the elements marked with an annotation are instances of a class that implements a certain interface, or are uses of types that implement a certain interface: @Documented @Target(value = {…
9
votes
2 answers

Access constant field in annotation processor

Suppose a class defines a constant field: public class Foo { public static final int CONSTANT_FIELD = 3; } And suppose an annotation interface is declared like the following: public @interface Something { int value(); } Finally, suppose the…
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
9
votes
3 answers

Find type parameter of method return type in Java 6 annotation processor

I'm writing a tool that uses the annotation processor to generate source code depending on the return type of methods of an annotated class. The return type is always some subtype (interface or class) of an interface A that defines a type variable…
Thomas Jung
  • 32,428
  • 9
  • 84
  • 114
8
votes
2 answers

Maven project build fails in IntelliJ when annotation processors are used (google/auto-value)

I use google/auto-value to create immutable value classes in a maven project.
sn42
  • 2,353
  • 1
  • 15
  • 27
8
votes
1 answer

Writing an annotation processor for maven-processor-plugin

I am interested in writing an annotation processor for the maven-processor-plugin. I am relatively new to Maven. Where in the project path should the processor Java source code go (e.g.: src/main/java/...) so that it gets compiled appropriately, but…
Ralph
  • 31,584
  • 38
  • 145
  • 282
8
votes
3 answers

Why IntelliJ needs Lombok plugin?

As far as I understand, Lombok uses Java's Annotation Processors to generate additional methods. With Maven 3.5 it works perfectly without adding any additional configuration, just add dependecy to Lombok and put some annotations like @Getter,…
8
votes
4 answers

Get package name of a annotated class within AnnotationProcessor

I have a class which I process with an AnnotationProcessor. While in the process I have an instance of javax.lang.model.element.Element where I can i.e. get the name of the the annotated class by .getSimpleName(). What I know need is the packageName…
xetra11
  • 7,671
  • 14
  • 84
  • 159
8
votes
1 answer

Get package name and parametrized type from a field element - Annotation Processor

How can I get package name, generic type and Parametrized type from a type from a field element in Annotation processor? Say, if Element.asType returns java.util.List, I want to get Package name java.util Generic type List or raw type…
mallaudin
  • 4,744
  • 3
  • 36
  • 68
8
votes
2 answers

Is it possible to generate an inner class of a class to compile with an annotation processor?

I am wondering if it would be possible to generate a class, via an annotation processor, that would be an inner class of a class to be compiled. For instance, while compiling class A, generate class A$Foo. I wonder if there is a trick that could be…
Snicolas
  • 37,840
  • 15
  • 114
  • 173
8
votes
1 answer

How to obtain the right JavaFileManager in a Java annotation processor?

I've written an Java annotation processor by extending javax.annotation.processing.AbstractProcessor which is called in the Eclipse context and it works fine, except that I need more information about the source path and class path for my processor…
Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26
8
votes
3 answers

Java annotation processing API accessing import statements

I am writing an AnnotationProcessor which is supposed to generate java code. It should generate a derived interface from certain existing interfaces. For this purpose I need to find the import statements of the original input code, so that I can…
Dennis Thrysøe
  • 1,791
  • 4
  • 19
  • 31
8
votes
2 answers

Messages with level Diagnostic.Kind.NOTE (and others) on Annotation Processors

I have two Maven projects: The first defines an annotation, an annotation processor and a provider-configuration file to trigger the annotation processor through the ServiceLoader API. The other depends on the first and defines some classes and…
Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
8
votes
1 answer

How to use custom annotation processor with Maven 2?

In our enterprise application we are seeking a dynamic way to collect data from our Java classes. We created a custom annotation interface (@interface) with a name property. We would like to collect the value of this property from all annotated…
7
votes
1 answer

Android multi modules annotation processing

In a multi modules Android project annotation processing is still executed as a first task before any compilation is done and then a complete compilation is triggered. Of course this is done per module before getting into the app module. Imagine a…
AouledIssa
  • 2,528
  • 2
  • 22
  • 39
7
votes
1 answer

Kotlin Multiplatform Annotation Processing

Annotation Processing in Kotlin Multiplatform can be done with kapt when one has a jvm target. But how does one process annotations if there is no jvm target? Specifically I want to generate code when processing annotations from commonMain. But I…