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

Custom Annotation Processor working in Maven but not in eclipse

I have a custom Annotation Processor which is being used in a sample project. I have added the following in the pom.xml file of the sample project maven-compiler-plugin
frigocat
  • 283
  • 2
  • 13
0
votes
0 answers

Intellij cannot recognize classes modified by annotation processor in target/classes

I'm using java annotation processor tool to modify java AST, and the modified classes is generated to target/classes. (Not generate java sources to target/generated-sources/annotations) For example, i add a getter method just like lomboks did, but…
IceMimosa
  • 21
  • 3
0
votes
2 answers

Android project won't run dependency annotation processor

I have a maven project with 2 modules. One one of the modules contain the annotation processor. When I try to import the project as a dependency for an android project , it won't run the annotation processor. The generated class files aren't found.…
Omer Ozer
  • 455
  • 1
  • 8
  • 20
0
votes
1 answer

How to get Actual Type from typeElement?

I'm writing custom annotation processor. Though I'm able to process class type without generic type but unable to get actual types for generic types. MyBase Interface interface MyBase { void method1(S, T); } Example Class1 public class…
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112
0
votes
1 answer

How to generate code that uses Android APIs

I have a java library that generates code for our Android project. We are using this to lower the amount of code we need to write in some of our RecyclerView adapters. The issue I ran into is that the annotation processor can't depend on the…
simekadam
  • 7,334
  • 11
  • 56
  • 79
0
votes
1 answer

Disable annotation processing Maven Java EE Eclipse

We have a number of war projects in eclipse. When they are imported as maven projects, the annotation processing are automatically enabled. Tried disabling the annotation processing by adding none to the maven-compiler-plugin. It…
movees
  • 183
  • 1
  • 15
0
votes
1 answer

How do I set up APT for Immutables such that Intellij Idea will recognize the generated code?

I'm looking into migrating from maven to gradle, in this case, gradle itself seems to be working fine, but Idea isn't recognizing the source code that Immutables is generating. I've read this blog post on APT, it's how I got this for. /* * This…
0
votes
0 answers

Way to write java annotation processor to require method call?

I was wondering how I might make an annotation processor that would flag uses of a class's constructor without a subsequent call to a particular method. This related to a previous question of "finding a previously annotated method", but I need to…
Charlweed
  • 1,517
  • 2
  • 14
  • 22
0
votes
1 answer

Java AnnotationProcessing - Get fields of the Annotation

I have a question according to the API AnnotationProcessing. Here's a little Example. @Retention(RetentionPolicy.SOURCE) @Target(ElementType.FIELD) public @interface MyAnnotation { String strNumberOne() default ""; String strNumberTwo()…
Fravice
  • 15
  • 6
0
votes
1 answer

Annotation processor with android library (realm)

I'm trying to create an annotation processor that works on top of realm.io Sadly to use realm you need an android project, while to create an annotation processor you need a java one (in order to import javax.annotation.processing.*) Anyone know a…
Dario Coletto
  • 145
  • 3
  • 12
0
votes
0 answers

How to find specific annotated parameters in methods and generated code inside that method block?

I'm currently writing an annotation processor in which I want it to scan the available methods in classes that their parameters are annotated as NonNull and generate some code inside that methods. During my search, I didn't find any kind of…
user4811490
0
votes
1 answer

Some questions about Java AbstractProcessor?

@SupportedAnnotationTypes({"com.tg.annotation.Table", "com.tg.annotation.Test"}) public class TgDaoGenerateProcessor extends AbstractProcessor { private Messager messager; @Override public synchronized void init(ProcessingEnvironment…
twogoods
  • 1,646
  • 2
  • 14
  • 21
0
votes
0 answers

IntelliJ: Java - Annotation processing stops working

I'm using IntelliJ 2017.1.4 (latest version) on Linux. It really often happens to me that after switching branches (GIT) the annotation processing stops working even the setting is still enabled. I have to restart the IDE than. Anyone knows how to…
messy
  • 915
  • 6
  • 26
0
votes
1 answer

How to use a class generated by library in the library itself

Well it's actually not in the library itself, it's in a different library which depends on the annoration processor. I have the roughly the following project structure. Annotation Processor -> Android Library module -> Android App module In the…
simekadam
  • 7,334
  • 11
  • 56
  • 79
0
votes
1 answer

Instantiate a Class annotated with some annotation

I want to get information about fields from a Class on which a specific annotation is present by getting the annotation in annotation processor. I can retrieve fully qualified class name from annotation but can't instantiate it, a…
mallaudin
  • 4,744
  • 3
  • 36
  • 68