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
22
votes
5 answers

Accessing source code from Java Annotation Processor

I am trying to access the actual original source code of a type from within a Java Annotation Processor. Is this possible somehow? Thanks!
Eric
  • 1,343
  • 1
  • 11
  • 19
21
votes
4 answers

How can I refer to implementations of a method in annotation processing?

I am playing around with Java (javax) annotation processing. Suppose I have an annotation for methods: @Target(ElementType.METHOD) public @interface MethodAnnotation { } Now I want to process all the methods which are overridden from a type with…
hotkey
  • 140,743
  • 39
  • 371
  • 326
21
votes
1 answer

What is the default annotation processors discovery process?

The documentation of Maven Compiler plugin mentions the following: annotationProcessors: Names of annotation processors to run. Only applies to JDK 1.6+ If not set, the default annotation processors discovery process applies. What is the…
20
votes
2 answers

Annotation Processor appears to break Java generics

Background I was trying to use Annotation Processors, to generate implementations of specific Factory interfaces. Those interfaces look the following: public interface ViewFactory { > T create(S…
Thorben Kuck
  • 1,092
  • 12
  • 25
20
votes
2 answers

How to generate a kotlin file from an annotation processor?

I have a java annotation processor which generates a bunch of java files during compilation. I'd like to make the generated classes nicer to use in kotlin by adding extension methods. I've been told on the kotlin forums that something I could try…
Bradley Campbell
  • 9,298
  • 6
  • 37
  • 47
19
votes
3 answers

How to decrease startup time on React Native Android applications

I'm currently down the path of trying to figure out how to decrease Android startup time. It hasn't been an issue with iOS but for Android, I'm seeing anywhere from 6-10 seconds. The goal is to be around 3-4 seconds. Here's a summary of the…
19
votes
3 answers

Cannot load resources in Annotation Processor (Not on classpath)

I have an annotation processor which shall generate a enumeration with the keys defined by getter methods of an interface. The interface resides in MyProject/src/main/java/my.package.MyInterfaces.java. I want to validate the properties files…
CCHET
  • 199
  • 1
  • 4
19
votes
2 answers

Annotation processor output in maven

I'm using JSR 269 as a way to analyze code during compilation and to fail it if needed. I'm having troubles with displaying output of my annotation processor in maven (Ant does show the output) I'm using javax.annotation.processing.Messager to…
iGili
  • 823
  • 7
  • 18
17
votes
4 answers

How to configure for Spring Boot Configuration Annotation Processor using @ConfigurationProperties on IntelliJ?

On IntelliJ, I am getting a Spring Boot Configuration Annotation Processor not configured for having @ConfigurationProperties. Below is my class: @Configuration @ConfigurationProperties(prefix = "abc") @Data @RefreshScope class Config { String…
夢のの夢
  • 5,054
  • 7
  • 33
  • 63
17
votes
1 answer

Compilation error for annotations in Java 1.8 and IntelliJ IDEA 14

I was playing with Java annotation processing. Application build fails in IntelliJ IDEA, while a maven build ends successfully. I am sure that provider class exists, yet I get the following error: java: Bad service configuration file, or exception…
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
16
votes
1 answer

Debug Java annotation processors using Intellij and Maven

I'm trying to learn how to make a custom annotation processor and I'm stuck with trying to debug it. I have already managed to run the javac compiler in debug mode (with mvnDebug clean install) (with someone else's project with an annotation…
PaperTsar
  • 961
  • 1
  • 9
  • 22
16
votes
1 answer

Java Annotations Processor: Check if TypeMirror implements specific interface

I'm working with on a Java annotations processor. My annotation, @foo is used to mark field variables that can be read to a file or from a file during runtime. However, I would like to check if the variable type implements Serializable during…
Testare
  • 338
  • 3
  • 12
16
votes
2 answers

AnnotationProcessor using multiple source-files to create one file

I have two classes with methods and i want to combine the methods of the two classes to one class. @Service("ITestService") public interface ITest1 { @Export void method1(); } @Service("ITestService") public interface ITest2 { @Export …
Poidi
  • 161
  • 1
  • 5
15
votes
4 answers

Getting the qualified class name of generic type with Java 6 annotation processor

I am developing a small code generator using JDK 6's Annotation Processing API and am stuck trying to get the actual generic type of a field in the class. To be clearer, let's say I have a class like this: @MyAnnotation public class User { …
Tinh Truong
  • 303
  • 2
  • 11
15
votes
4 answers

Java Generics: Accessing Generic Type at runtime

I'm looking to access the generic type of a declared field during runtime. I was previously under the impression that this was not possible due to the Java type erasure. However, this must not be the case because some well known frameworks…
John Ericksen
  • 10,995
  • 4
  • 45
  • 75
1
2
3
48 49