0

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?

Michael
  • 32,527
  • 49
  • 210
  • 370

2 Answers2

3

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.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Thanks Thomas for this more detailed answer. I agree, not all could be done with annotation processing. Looking at mvp4g, switching to annotation processor will improve the framework. The generation process take more than 15 seconds, sometime much more. A annotation processor would speed up the generation of code, especially when there is no need to have different permutation. – El Hoss Apr 28 '15 at 09:50
  • Have you tried making your generators incremental first? It might cost less energy than rewriting them as annotation processors. – Thomas Broyer Apr 28 '15 at 09:54
  • Yes, I did. But I think, this is a code reason to do some reengineering and add some new features ... ;-) – El Hoss Apr 28 '15 at 09:56
  • Oh sure. My rule of thumb would be that if you can do it with an annotation processor, then do it that way; otherwise use a GWT generator, or possibly a combination of both: e.g. annotation processor generating a factory that uses `GWT.create()` to trigger the smallest possible generator. The problem with annotation processors though is that you need some tooling (e.g. an IDE or file-watcher) to trigger them automatically in dev mode, and Eclipse for example is notoriously bad at it. Or maybe have GWT run annotation processors itself? (and run them on generated code from generators?) – Thomas Broyer Apr 28 '15 at 10:52
  • You are right. I made really bad experience using eclipse and annotation processor. mvp4g is a MVP-Framework based on annotation. I wrote a annotation processor to validate the references over all classes. In IntelliJ and maven it works well. In Eclipse I had to fight with the fact that saving one file initiates a processor for this class. It would be great if GWT could run annotation processor. +1 for this feature. – El Hoss Apr 28 '15 at 11:16
  • @ThomasBroyer How do I create the ``dynamically (at runtime) select among them`` for annotation processors? Could you please give some sample code? – Michael Apr 29 '15 at 11:18
  • @confile Well, generate a factory with `if…else` or `switch…case` whose conditions are based on _something_ that you can know at runtime (in a GWT environment, that could be `GWT.isClient()`, `LocaleInfo.getCurrentLocale().isRTL()`, some property of `Window.Navigator`, or`GWT.create(UserAgent.class).getCompileTimeValue()`; or `System.getProperty()` starting with GWT 2.8; or anything you pass as argument to the factory) – Thomas Broyer Apr 29 '15 at 12:44
  • @ThomasBroyer Could you please add this to your answer text. It is hard to follow in the comments here. – Michael Apr 29 '15 at 12:57
  • @ThomasBroyer I accept your answer and created a new question for the runtime part here: http://stackoverflow.com/questions/29945548/how-can-i-replace-gwt-create-with-an-annotation-processor – Michael Apr 29 '15 at 13:42
1

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.

El Hoss
  • 3,767
  • 2
  • 18
  • 24
  • Could you pleas post the link to the Singular project? – Michael Apr 27 '15 at 22:48
  • It was presented by Daniel Kurka at the GWT.create 2015. As far as I know there is currently no link to the project available. Here is the link to the presentation slide: http://www.daniel-kurka.de/talks/gwtcreate15/singular.pdf and to the video: http://gwtcreate.com/videos/#singular – El Hoss Apr 28 '15 at 04:32