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

How to trigger the minimal task on Gradle to run apt plugin

I'm creating a compile-time annotation processor to generate some code on Android. To trigger the annotation processor I'm using the android-apt plugin from hvisser https://bitbucket.org/hvisser/android-apt/overview At the moment, on every change I…
Budius
  • 39,391
  • 16
  • 102
  • 144
0
votes
0 answers

Getting an annotation's field in Java

This is in annotation processing. I have a TypeElement about which I know that it is annotated with @Bind. I get the TypeElement with: ProcessingEnvironment#getElementsAnnotatedWith(Bind.class) @Bind looks like this: public @interface Bind { …
PaperTsar
  • 961
  • 1
  • 9
  • 22
0
votes
0 answers

Android annotation processor gradle error

I am writing an Android library that I plan to use with different projects. There is also an annotation processor library that I am writing that would be used to generate code based on annotation defined in the previous library. The project…
0
votes
0 answers

Error during in-memory compilation and loading of classes with annotation processing

I am compiling and loading classes in memory with JavaCompiler. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler.CompilationTask compile = compiler.getTask(null, inMemoryFileManager, diagnostics, null, annotationClasses,…
eclipse
  • 2,831
  • 3
  • 26
  • 34
0
votes
1 answer

Getting raw annotation data during annotation processing

I'm having something like this @Value(name="values1", values = { R.string.first, R.string.second, R.string.third }) and it's variation @Value(name="values2", values = { R.integer.first, R.integer.second, R.integer.third }) R.integer.* and…
Ivan
  • 1,446
  • 2
  • 11
  • 25
0
votes
1 answer

getAllMembers from Elements does not return Parent's member annotations

getAllMembers from java.lang.Model.Util.Elements does not return Parent's member annotations Example: public class Foo { @QueryParam("firstname") private String name; public String getName() { return name; } public void setName(String name) { …
Kunal
  • 2,929
  • 6
  • 21
  • 23
0
votes
2 answers

Difference between a GWT Generator and a Java Annotation Processor?

GWT uses Generator to create code before everything is translated into JavaScript. Java on the other hand has an annotation processor which generates code before everything is translated into byte code. What is the difference between a GWT Generator…
Michael
  • 32,527
  • 49
  • 210
  • 370
0
votes
1 answer

Get Element where TypeElement is defined in?

I have the following class: public class Start extends PlacePresenter { @NameToken("startPage") public interface MyProxy extends ProxyPlace { } } In an annotation processor I got the Element annotated with @NameToken which is…
Michael
  • 32,527
  • 49
  • 210
  • 370
0
votes
2 answers

Custom compiler error isnt removed (after fixing) until file is saved

Every annotation processor I've made seems to have this problem. For example, a @Constant annotation: package annotations; @Retention(RetentionPolicy.SOURCE) @Target(ElementType.FIELD) public @interface Constant { } The processor: package…
Vince
  • 14,470
  • 7
  • 39
  • 84
0
votes
1 answer

How to add generated implementation to Dagger Dependency Graph?

I have an interface public interface SomeInterface { void test(); } and an annotation processor which generates an implementation of SomeInterface called SomeInterfaceImpl. To make this type available with Dagger dependency injection I would…
Michael
  • 32,527
  • 49
  • 210
  • 370
0
votes
1 answer

JSR269 annotation processing getElementsAnnotatedWith() return all the annotated elements each loop and cannot distinguish it belongs to which type

Recently, I have been intersted with the JSR-269 annotation processing and I want to write a lib to eliminate some boilerplate code with it, such as json processing. I really generate the code, then, however, I encounter a fatal problem and spent a…
longkai
  • 3,598
  • 3
  • 22
  • 24
0
votes
1 answer

Change annotation processor dynamically

Following sample is just to explain the my implementation, please have a look at and let me know if i can get any answers for this I have created a annotation Dispenser @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface…
Maddy
  • 85
  • 11
0
votes
1 answer

Filer always throws FilerException

Edit: I just found out that not a IOExcpetion but a FilerException is thrown. Therefore I changed that in the description and the title. I'm working with Annotation Processing to generate some files for my java project. Now I always get an…
mvieghofer
  • 2,846
  • 4
  • 22
  • 51
0
votes
1 answer

Force javac to run processors

AFAIK when some errors were reported from within one processor, other will not be run. Is there any way to force processing independently?
Dany
  • 1,490
  • 1
  • 12
  • 22
0
votes
1 answer

How to use dagger in Android Studio Beta 0.8.1

Older version of Android Studio can use this config But the beta version do not have this config. Where can I enable code generation in the beta version?
Bruce
  • 107
  • 5