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

Custom annotation processing while coding

The purpose of the project I'm working on is to handle annotation at compile time, it is not focused on what exactly I'm developing. I took a simple subject for this and I'm writing a custom collection that will store elements and provide methods to…
0
votes
1 answer

AspectJ weaving: How to do a full code weave without static reference to the aspect?

a few weeks ago i was looking for a way to create some String constants for the fileds of a java class -> Generate constants for class attributes with maven? I got it working. I create an aspect containing the constants and weave them into the class…
martin
  • 980
  • 1
  • 13
  • 28
0
votes
0 answers

Gradle build - Illegal name error while specifying package path annotation processor filer.createSourceFile()

I have a custom annotation processor which creates few classes. This is working fine in eclipse build and it generates java files with full package structure under ".apt_generated" folder. String implName = "foo/bar/impl/" +…
vinodpthmn
  • 1,062
  • 14
  • 28
0
votes
1 answer

Have Annotation Processor Ignore files based on gradle flag

I have two gradle projects (A, B) And they use the same annotation processor the problem is that A has exactly one part of the processor it does not need. Is there a way to configure gradle so that the annotation processor performs differently for A…
dtracers
  • 1,534
  • 3
  • 17
  • 37
0
votes
0 answers

IntelliJ IDEA can't find generated configuration metadata file

In my project I'm using the spring-boot-configuration-processor to generate configuration metadata, which can be helpful at setting fields in @ConfigurationProperties marked classes from .properties files. As build system I use Gradle. The…
TwITe
  • 400
  • 2
  • 14
0
votes
1 answer

Get enclosing class name of a VariableElement in javax.annotation.processing.Processor

I am just starting to learn annotation processing in Java. I have @MyAnnotation which targets ElementType.FIELD, In my Processor I am limiting the annotation to only allow non-null unique value. Which is working as it should. While logging errors in…
Abbas
  • 3,529
  • 5
  • 36
  • 64
0
votes
1 answer

Should Micronaut Produce Compiler Warnings / Errors for Dependency Injection Errors?

I'm just getting started with Micronaut and one thing I was surprised to discover is that despite the annotation processors, there does not appear to be any warnings to indicate when there is a dependency injection problem. For Example, when I have…
0
votes
1 answer

Using Java Annotation Processor To Understand Parameterized Types Of Method Parameters

I am trying to use an annotation processor to validate annotations, and as part of that effort, I am trying to figure out how to use the API to determine if a Parameter of an ExecutableElement is a parameterized type (such as List), and if so,…
Zach
  • 11
  • 2
0
votes
1 answer

How can I add some generated code in existing method

Synthetic adds clearFindViewCache() in Activity.onDestroy() and Fragment.onDestroyView() methods. How can I add extra code to existing method with my own annotation processor? If we look to the synthetic's implementation we can see usage of classes…
0
votes
1 answer

Is there any Annotation Processing Library for Network Connection Check in Android?

My application constantly does API requests and I am checking connection status with ConnectivityManager. If it's connected then requesting for a data from the server, if not showing an error dialog that says Not connected to internet. Well above…
musooff
  • 6,412
  • 3
  • 36
  • 65
0
votes
1 answer

how to get extends interface by annotation process tools?

A want to get class's all methods, including it's extends class's methods. for example: interface A{ void a(); } interface B extends A { void B(); } i want to get method a() and b() by Element.
JingYeoh
  • 151
  • 1
  • 6
0
votes
2 answers

What is the use of AnnotationProcessor vs ConstraintValidator while creating custom annotation

I have a requirement to create a custom Annotation which when applied over a method checks and validates the input parameters (Primitive and non-primitive) against various checks. And if checks fail should return a error message directly. While…
0
votes
1 answer

Is it possible to use Mirror API to recursively check for annotations during annotation processing

Suppose I have a class structure like : @MyAnnotationOne class A { private String id; private B b; public static class B { private C c; @MyAnnoationOne public static class C { @MyAnnotationTwo …
0
votes
1 answer

Extend class functionality with annotation processor during compile time

I have the following Spring Boot class, annotated with the custom annotation Counted: @RestController @RequestMapping("/identity") public class IdentityController { @Autowired private IdentityService identityService; @PostMapping …
Ben
  • 332
  • 3
  • 16
0
votes
1 answer

Mapstruct AnnotationProcessor with IntelliJ and Gradle

I am trying to get the Mapstruct annotation processor to work in IntelliJ in a Gradle project. Ideally, I would expect for all configuration to be in the gradle-file and that anyone could just import the project into IntelliJ and get a complete…
David
  • 3,787
  • 2
  • 29
  • 43