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

Setting an annotation processor creates a problem with META-INF/DEPENDENCIES

I was trying to deal with this error (which I never had before I update something in Android Studio): Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation…
0
votes
1 answer

Maven/Gradle set environment variable for compilation?

I developed an annotation processor that would optionally require the artifactId of the project to generate a file. I am using an environment variable (GRAPHDEP_USAGE) to get the value. I can set the environment variable from shell before launching…
gotson
  • 3,613
  • 1
  • 23
  • 40
0
votes
1 answer

How to process annotations on synthetic elements

How can I process annotations on synthetic elements? RoundEnvironment.getElementsAnnotatedWith doesn't seem to return any synthetic elements.
Grisu47
  • 530
  • 5
  • 16
0
votes
1 answer

How to generate xhtml files in webapp using javac annotation processor

I am generating some source files at compile time using annotation processors, it is a very powerful feature. But I want to generate also some facelets components. I don't know how to create non java files in the webapp folder. I know I can do this…
mnesarco
  • 2,619
  • 23
  • 31
0
votes
1 answer

Using an Android SDK class in a custom annotation processor

I am trying to implement an annotation processor to fill views with a foreground color (nothing fancy for the moment). I have implemented my processor to process every Type annotation with my custom annotation. The process is going straight so far…
AouledIssa
  • 2,528
  • 2
  • 22
  • 39
0
votes
1 answer

why companion object modifier is always private when i assess it in annotation processor?

i have a custom annotation which its Retention is AnnotationRetention.SOURCE and i want to make sure that the annotated variable is a public static but im having problem with kotlin companion objects and they seem to be private even when i…
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
0
votes
1 answer

How to print stack trace when an error occurs during annotation processing in Java

I'm working on a Java library that has an annotation processor. To trap exceptions I wrote code similar to the following code: public class KriptonProcessor extends AbstractProcessor { @Override public boolean process(final Set
xcesco
  • 4,690
  • 4
  • 34
  • 65
0
votes
1 answer

Lombok Annotation Processor with Gradle 4.9

If you get that exception/warning below, when you're using Gradle 4.9, downgrade to Gradle 4.8. warning: lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation: java.lang.ClassCastException:…
0
votes
0 answers

Usage of generated classed inside Data Binding XML file

I have a problem with project build. I wrote my own annotation processor. It is generating helper class to use with Android context based on other, contextless class. This generated class is intended to use inside XML files that uses Data Binding. I…
0
votes
3 answers

Is it possible to to do validation checking at run time other than compile time?

With the following Java code: public class Bean{ private String value; public Bean(@NonNull String value) { //Usually fail-fast validation can be added here if it is needed this.value = value; } public String getValue() {return…
Rui
  • 3,454
  • 6
  • 37
  • 70
0
votes
1 answer

Why do I get this error with Velocity "Velocity is not initialized correctly."?

I initialize velocity engine inside an annotation processor that extends AbstractProcessor as follows: public boolean process(Set annotations, RoundEnvironment roundEnv) { String fqClassName = null; String className =…
0
votes
1 answer

Gradle: programmatically specifying compiler options for compileJava task

I have a gradle script with a compileJava task, and I want it to provide two different Jar tasks, jar and jarForce. The jarForce task should compile the sources with -Awarns option added so that the annotation processor errors are treated as…
saga
  • 1,933
  • 2
  • 17
  • 44
0
votes
1 answer

Generate a field as anonymous class with JavaPoet

I'm working on an annotation processor library and I'm using JavaPoet to generate som Java code. I need to generate a class with a field declared with an anonymous class like the following code: public class Dummy { private final…
xcesco
  • 4,690
  • 4
  • 34
  • 65
0
votes
1 answer

Kotlin data class definition is not in roundEnv.rootElements (annotation processing)

Inside an annotation processor that extends AbstractProcessor I have: override fun process(annotations: MutableSet, roundEnv: RoundEnvironment): Boolean { roundEnv.getElementsAnnotatedWith(FxBean::class.java) .forEach { …
Vitaly
  • 4,358
  • 7
  • 47
  • 59
0
votes
2 answers

Breakpoints not hit in IntelliJ when debugging my Java annotation processor

I've written a Java annotation processor following Hannes Dorfmann's tutorial. My project uses Maven. When I try to debug it using mvnDebug as suggested in this article, everything works except that my breakpoints are not hit in IntelliJ. I can run…