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

Gradle not processing annotation processor first

I'm having a problem with a Gradle build. In my project I'm using an annotation processor made by myself. This processor generates some source files on the last round of processing. In the generated files there's one class in particular (Revolver)…
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
5
votes
0 answers

Specify path for generated classes in Annotation Processor

I am creating an Android application. I using AbstractProcessor to generate my own classes from Annotated classes and Velocity templates. This is the method that generates source file. private void createSourceFile(String className, String…
thealeksandr
  • 1,686
  • 12
  • 17
5
votes
1 answer

How to generate constructor that calls superclass constructor using JavaPoet

I want to generate a class that extends other class using JavaPoet. For example I have this class: @MyAnnotation public class ClassA { public ClassA(String paramA, int paramB) { // some code } } and I want to generate new class like this…
radzio
  • 2,862
  • 4
  • 26
  • 35
5
votes
1 answer

Aggregate output from Annotation Processors of different buck modules

I have a project composed of several BUCK modules. Each module uses the same annotation processor to generate a single file for each annotated class. For the sake of simplicity, for each annotated class: package…
Lior
  • 7,845
  • 2
  • 34
  • 34
5
votes
3 answers

Generate unit test using annotation processing

I’ve been seeking information about this matter but I couldn’t find any useful resources. I need to generate unit tests using annotation processing. I have no problem generating a class which can be a unit test. The thing I do not know how to do is…
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
5
votes
2 answers

unable to extend AbstractProcessor to create java annotation processor

I am trying to begin creating a javax annotation processor, im doing it from android studio for now. I just need the gradle dependency i think for it. Right now in gradle i have the following which i have tried: provided…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
5
votes
0 answers

Dagger2 Maven Eclipse Integration

I try to migrate a bigger maven project from guice to dagger2. The annotation processing is integrated using the m2e-apt plugin. The code generation works fine if all injected dependencies can be provided. If a dependency can not be provided I just…
mkaulig
  • 51
  • 1
5
votes
2 answers

How to get type annotations & attribute values for VariableElement with Java8?

Consider the following code: public class SimpleTest { private Map<@JSON Integer,Map<@Frozen Integer,@Enumerated(value = Enumerated.Encoding.NAME, test = "123") String>> map; } With the latest JDK8 API for annotation processing, how can I…
doanduyhai
  • 8,712
  • 27
  • 26
5
votes
2 answers

How to capture an Enum from an AnnotationValue in an Annotation Processor

I am trying to read the value of an enum in an annotation using an annotation processor and annotation mirror, but I am getting back null. I think this has to do with the AnnotationValue wrapping an Enum as a VariableElement. The doc for…
J Hall
  • 531
  • 3
  • 10
5
votes
0 answers

Resolve method call in annotation processor

I want to write an Annotation Processor to check that a Method is called only in specific places. For example: interface Command { @MustOnlyBeCalledByWorker void execute(); } class Worker { void work(Command cmd) { …
LQuadrat
  • 51
  • 2
5
votes
2 answers

How to check a methods parameter type in an Annotation Processor?

With pure reflection this would be easy, but the annotation processor world seems to be different. How do I get from a TypeMirror returned by getParameterTypes() to String.class? In my Annotation Processor I want to check if the currently visited…
towi
  • 21,587
  • 28
  • 106
  • 187
5
votes
1 answer

How to Run Annotation Processor without compiling sources using javac (Java 8 can't use Apt)

How can i Run Annotation Processor without compiling sources using javac (Java 8 can't use Apt)? Is there any parameter for javac that could run only annotation processing without compiling all files? What i want to do by javac: Just find…
rapoo.coder
  • 133
  • 1
  • 2
  • 7
5
votes
2 answers

How to get field's type annotation in Java Annotation Processing?

For example, I have this code: @Retention(RetentionPolicy.SOURCE) public @interface ClassAnnotation { } @ClassAnnotation public class AnnotatedClass { } @ClassAnnotation public class AnotherAnnotatedClass { private AnnotatedClass someField; …
fikr4n
  • 3,250
  • 1
  • 25
  • 46
5
votes
1 answer

How do I get the type of the expression in a MemberSelectTree from a javac plugin?

I am trying to write an annotation processor in the JSR 269 format which uses javac's Compiler Tree API to do some source code analysis. I am interested in member select expressions, such as method calls. I can easily get the name of the method (or…
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
5
votes
1 answer

How to run JSR269 annotaion processor in Eclipse Kepler with Java 7 & Maven

I'm using eclipse Kepler and the compiler is set to use Java 7. I have a JSR269 compliant annotation processor in my classpath (in the maven container). What is the easiest way to run this annotation processor? Actually I would expect the Eclipse…
TmTron
  • 17,012
  • 10
  • 94
  • 142