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

Can I generate the spring boot configuration metadata as a .properties file

If I include spring-boot-configuration-processor as a dependency, my build will produce a json file like this: { "groups": [ { "name": "attachments", "type": "com.example.config.AttachmentsSettings", "sourceType":…
0
votes
1 answer

How to get java type annotation of functional by reflection

According to JSR 308 (Java Type Annotations) it is possible to annotate any type using ElementType.TYPE_USE: import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; import…
0
votes
0 answers

Unable to use AbstractProcessor in IDEs

Motivation: In our code we have a few places where some methods are run by their name. There are some big if-else-if blocks with each function name and call of the corresponding method (I use the term function to describe just names, for example…
Miku
  • 567
  • 6
  • 15
0
votes
0 answers

Find annotation from type parameter during annotation processing

How can I get the annotation declared inside a type parameter during the annotation processing? interface HelloWorld { void fooBar(List<@NonNull String> params); } (Assume @NonNull is a custom annotation declared with…
0
votes
1 answer

what is aptMode of kapt used for?

In the doc there are three values for the aptMode. Is there any detail information about these values ? What is the meaning of "stubs" ?
Liu Tom
  • 397
  • 2
  • 9
0
votes
1 answer

Check inheritance of Element in source level AnnotationProcessors

I'm looking for a way to detect if a class is inherited from another class/interface in annotation processor. Since the annotation processor runs on the source code not runtime, there's no way to use reflation API, the only method I found is: public…
user1079877
  • 9,008
  • 4
  • 43
  • 54
0
votes
1 answer

Failure using Kotlin Kapt: error: scoping construct cannot be annotated with type-use annotation: @org.jetbrains.annotations.NotNull

I'm using the Vertx Service Gen annotation processor with Kotlin kapt. Before the annotation processor kick in, my kapt failed with following exception message all over the place: error: scoping construct cannot be annotated with type-use…
wusatosi
  • 617
  • 5
  • 13
0
votes
0 answers

How to modify the body of a method using AbstractProcessor in Java?

Longtime reader, first time poster here. I have been working on a project which requires me to use an annotation processor. I know that what I am doing is covered by AspectJ but unfortunately AspectJ requires using the AJC compiler and this causes…
0
votes
1 answer

Annotation processing exception: failed to analyze: java.lang.reflect.InvocationTargetException

I'm writing my own annotation processor and I want to be able to annotate the return type of a function. Here's the code for the annotation: @Retention(AnnotationRetention.SOURCE) @Target(AnnotationTarget.FUNCTION) annotation class ReturnType(val…
SoapyCro
  • 175
  • 2
  • 9
0
votes
1 answer

Pluggable Type Checkers and Checkers framework, is really needed all this setup?

I am trying to understand how Checker Framework implements Pluggable Type Checkers. By reading the documentation, Checker Framework (Maven) I see a lot of setup involved, and looks to me either outdated or not quite mantained. As far as I read,…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
0
votes
1 answer

Java Annotation processor check for full rebuild

I am creating an Annotation Processor in Java and I want to be able to check if the user triggers a full rebuild. I want to be able distingush between a full rebuild and just building a few files. Is this possible? Or are there any workarounds for…
derteufelqwe
  • 113
  • 2
  • 9
0
votes
1 answer

why getSimpleName() is twice in com.sun.tools.javac.tree.JCTree$JCClassDecl

I had a weird bug in an application code, which is an annotation processor and I could find that the root cause of the bug was that the class com.sun.tools.javac.tree.JCTree$JCClassDecl contains the method getSimpleName() twice when I query the…
0
votes
1 answer

java.lang.IllegalArgumentException: @kotlin.Metadata does not define an element error when converting code from Java to Kotlin

I'm trying to convert one class file in one of our modules in our application from java to Kotlin but I get this error message when trying to build. The line that causes this error is using dagger @Inject removing @Inject will make the error go…
0
votes
0 answers

Can i know if an object is a custom object?

I am writing an annotation processor and i need to scan all classes with certain annotation to get all fields and create json object with same structure of the class. For example: @ClassToJson public class Person { private String name; …
Luke
  • 516
  • 2
  • 10
0
votes
1 answer

Ways to make annotation processors read src/main/resources files on Maven Update

I am trying to make annotation processor read a file from src/main/resources package. The code I am using to read the file is: resource = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", fileName); The annotation processor is…
J. S.
  • 89
  • 1
  • 3
  • 9