Reputation: 33297
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 and a Java Annotation Processor?
Upvotes: 0
Views: 609
Reputation: 64541
GWT generators not only generate code, they also tell GWT to use it; whereas annotation processors only generate code that then needs to be used by some other code (either referenced directly in the code, or loaded through reflection – something that's not possible in a GWT environment though).
The way generators and processors are invoked is very different too. GWT instantiates and runs a generator for each type that needs processing, for each permutation (this stems from the above difference: the generator tells GWT which class to actually instantiate for the given type passed as input), whereas an annotation processor is instantiated once per compilation and then invoked repeatedly for each processing round, where each round processes a set of types.
GWT has support for permutations, where the generated class can be different depending on the binding properties' values to achieve deferred binding (GWT generators are just one facet of deferred binding). Note that GWT generators don't actually have to generate anything, they can also just select an existing class. You can only approximate this with annotation processors by generating code that will select the given implementation at runtime.
Put differently, if you generate a specific implementation for locales en-US
and fr-FR
; with a GWT generator, when the generator is invoked, it knows which locale the permutation is about, so it can tell GWT to use that particular implementation. The net result is as if the GWT.create()
in the code was replaced with a new TheGeneratedClass()
, and that class can be different depending on the permutation (e.g. MyMessagesImpl_en_US
vs. MyMessagesImpl_fr_FR
). With an annotation processor, you'd have to generate both classes and then dynamically (at runtime) select among them, depending on the context (you could generate a factory to help doing that, but you'd still have to somehow feed the factory with the current context, e.g. the current locale).
Finally, the way you trigger them is different. GWT's deferred binding is only triggered by GWT.create()
in the code, i.e. instantiation of a yet-to-be-determined-at-the-time-of-writing class; whereas annotation processors are triggered by the mere presence of an annotation on an element (a package, a type, a field, a method, a parameter, or even in Java 8 a type-parameter or anywhere a type is used).
GWT generators and annotation processors are different things, made with different goals. In many cases you could use one or the other almost interchangeably (e.g. AutoBeans, RequestFactory, the Editor framework, PlaceHistoryMappers in GWT could be done using annotation processors), but not always.
Upvotes: 3
Reputation: 3832
In fact, they are doing the same. Both generate code and both can not change existing classes. A annotation processor is initiated by an annotation where as the GWT compiler is started by the GWT.create statement. There are some differences in the amount of informations a generator has compared to an annotation processor. And there are some things that does not work well with annotation processors, for example working with xml files, etc.
My personal opinion is, GWT will stop using generators and start using annotation processors where ever they can. Take a look at Singular. This framework created by Daniel Kurka does not use generators. It is created using annotation processors.
Using annotation processor will speed up super dev mode. There is no need to wait until all generators did there work.
At the moment I am thinking about replacing the generators of the mvp4g framework and using annotation processors instead. I think this will be a great improvement.
Also there were a thread in the GWT group:
https://groups.google.com/forum/#!topic/google-web-toolkit-contributors/RYZulixEQWg
Hope that helps.
Upvotes: 1