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

Android Gradle Build not working if changing dependency order

Ok, this is probably the most stupid question I've ever asked but I really need to know why this is happening. I've built my own annotation processor for android. Now, these are my dependencies on my build.gradle: dependencies { compile…
0
votes
2 answers

How to create an instance out of a TypeMirror

I have an annotation that receives a "dynamic" parameter according to this idiom, i.e. a parameter of an interface type. In short: public interface MyInterface {} public @interface MyAnnotation { Class value(); } Now, to…
Erik Hofer
  • 2,296
  • 2
  • 18
  • 39
0
votes
1 answer

Setting up Annotation Processor with Dependencies using Apache Ant

I have an annotation processor that is depended on other libraries e.g. Guava, Commons. I already set it up with Eclipse and Maven. However, I want to use it with an Apache Ant's build file. So, I created following target:
Baris
  • 471
  • 6
  • 19
0
votes
1 answer

STS/Eclipse error: Problems occurred when invoking code from plug-in: "org.eclipse.core.resources"

I've written a custom annotation and processor which implements com.sun.source.util.TaskListener. The annotation processing currently works when ran as part of a gradle build, but when I enable annotation processing in STS, I get the following…
Jaimie Whiteside
  • 1,180
  • 9
  • 21
0
votes
0 answers

How do I get the URI of a JavaFileObject in annotation testing?

I'm using XSLT to generate Java Code with the javax.annotation.processing API. It works well as long as I don't want to test it, which I of course do :) While running the processor, the JavaFileObject.toUri() gives me a "file:/"-URI, which I can use…
roplacebo
  • 2,837
  • 2
  • 17
  • 19
0
votes
1 answer

Android annotation processing - generate different code for different build flavor

I'm building a library that requires some annotation processing to generate code. I now run into an issue that the release build doesn't need to have as much code as the debug build does (since this is a library for modifying configuration variants…
0
votes
1 answer

Test apt argument for annotation processor with google compile-testing

I am writing annotation processor for my android project and I test it using google compile-testing. It all works fine except that I am able to test apt plugnin argument to my annotation processor. My annotation processor has this option that I want…
Abdullah
  • 7,143
  • 6
  • 25
  • 41
0
votes
1 answer

configure velocity as annotations' processor dependency on eclipse

I try to follow this tutorial on java code generation utilizing annotations and Velocity template engine. (I'm using eclipse ee mars.) On the annotated project, annotation processing is enabled and the processor (as an exported jar) is included in…
foivaras
  • 238
  • 4
  • 11
0
votes
1 answer

Javac stops compiling after first round of AnnotationProcessor

I'm sorry about the obscene names, but I've written this processor. I execute the following at the root of the project: javac -d compiled/ -verbose *.java Which generates this log. It stops there and no bytecode is generated. I can't figure out why…
user6046168
0
votes
1 answer

Generate code during automatic build in eclipse?

I am writing a code generator that generates additional classes for each class annotated with a certain annotation. I have other classes in my project that has to refer these generated classes. If I can somehow integrate my code generator into the…
rahulmohan
  • 1,285
  • 11
  • 19
0
votes
0 answers

Performing a final action after annotation processing

So I have an annotation that, although it can be declared multiple times, generally needs to access the same properties file. Currently I am using a static registry in my annotation processor to track if the file has been created, and to store the…
Socratic Phoenix
  • 556
  • 1
  • 7
  • 19
0
votes
1 answer

Can I use BeanValidation on annotations themselves?

I'm working on an annotation processor and became curious. Does it make any sense to annotate like this and validate within the annotation processor? @Retention(value = RUNTIME) @Target(value = {FIELD, METHOD, PARAMETER}) public @interface…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
0
votes
0 answers

Check if annotation processing or usual runtime in Java

Is there any technique in Java to check if annotation processing or usual runtime (or some other kind of processing) is taking place? For example something like this: if(Processing.isAnnotationProcessing()) { ... } Or something like…
narranoid
  • 155
  • 2
  • 12
0
votes
0 answers

Gradle compile jar with multiple source sets

I want to build from gradle jar file including sources from several sourceSets. Alongside main I have generated source set with classes from annotation processing. How can exclude in jar specific package from main sourceSet and include specific…
L3K0V
  • 1,104
  • 1
  • 9
  • 24
0
votes
1 answer

Compare JavaPoet ParameterSpec type with Java 8 AnnotatedType

I need to compare the annotated type of a field (or method parameter) with a ParameterSpec instance. The name of the parameter does not matter in this context. The context is somewhat related to the unresolved issue 136. The following tests are…
Sormuras
  • 8,491
  • 1
  • 38
  • 64