+6

Generators vs APT

Seamus McMorrow 10 months ago • updated by Colin Alworth 10 months ago 6

The GWT team are always saying don't use gwt generators anymore and try remove as much as possible the GWT.create calls through the code.


Are there any libraries out there, that make use of APT for localisation, templating, css resources, restygwt alternative, etc?

A group of us started a localization tool in APT as a group back at the last GWT.create, but no one (myself included) has updated it since then. https://github.com/niloc132/i18n-redux. I'd love for some of us to put our heads together this year at GwtCon and keep this sort of thing going.


Frank Hossfeld started an Editor Framework processor, and I assisted in getting it going - it doesn't report errors very nicely yet, and requires a small change to how you build your drivers, but seems to work otherwise so far - suggest using this branch, we havent done a release yet https://github.com/niloc132/gwt-editor-apt/tree/model-first.


Elemento is a Elemental2-based templating tool https://github.com/hal/elemento, though I also made a SafeHtmlTemplates processor (this was before I knew much about processors - I would do it slightly differently now, and should take some time to update it) - https://github.com/niloc132/gwt-safehtml-templates.


Thomas Broyer built a GWT Places processor https://github.com/tbroyer/gwt-places.


And of course, Gin/Guice can be replaced by Dagger2.

+1

To implement annotation processors, I highly recommend to take a look at the Google Auto project. Especially AutoService and Auto Common Utilities are extremely helpful. To test your annotation processors I recommend to use Google Compile Testing. It helps you verifying the generated code. 

I used to write lots of generators, but i was able to move every thing to apt without too much effort, and tools Harald Pehl mentioned ^^^ are really very very helpful.


and in the GWT i have seen many projects started to use Processors instead of generators, for GWT itself , i dont think the remove GWT.create and the generators right now from current version 2.8.1 is a good decision, there is still lots if projects that depends on them and this should version should not broke that.

+1

The current plan and expectation is that GWT 2.x will not remove GWT.create(), nor even legacy Dev Mode - for teams still using these things, the maintenance is low and the benefit is high.


With that said, neither are getting updates to bring them into the future - no jsinterop for legacy dev mode, and no java 8 (or soon, java 9) language features for generators.

I have currently no experience with APT.

I am not sure how you can replace the "JClassType[] com.google.gwt.core.ext.typeinfo.TypeOracle.getTypes()", which we currently use extensively in our code generators.

Is it possible to have something similar?

Do you know of any implementation patterns, which could replace the need to know all classes?

+1

GWT Generator's TypeOracle has the privilege of understanding the entire world, and knowing about all classes that exist - this is an aspect of what makes GWT so expensive to run, since it have already collected all of that.


In contrast, the processor environment tells you only what classes are currently being compiled so you can handle them - at least in theory, if someone doesn't edit a class that is important to your codegen tools, you will not be told about it (since nothing happened). For something like UiBinder, Constants, ClientBundle, Places, SafeHtmlTemplates this makes good sense. For other tools where you need to understand the whole world, we need to learn to think in APT a little better. Specifics for that depend on your exact use case.


...But if all the code you ever will need from getTypes() is in one project and _every_ build is a clean build, and you don't mind your processor being required to run on everything (like Generators did) which may slow things down, you can request "*" as the annotations you want to handle, and then "roundEnv.getRootElements()" will give you all types. Again though, this makes a few important assumptions about how your project works - either IntelliJ or Eclipse will try to rebuild your project without doing a full clean build.


One power that APT has that generators do _not_  is the ability to run multiple times on the same type, and run until everything settles out completely.


EDIT: I'm working up a list of many calls that are made on the gwt JType hierarchy that don't apparently exist in APT, and will publish it once I have confirmed the correctness of a few more of them. For now, you might consider looking at the existing GWT generators and their corresponding ported APT versions.