views:

249

answers:

3

I'm using the same source code for a GWT 1.5 and GWT 1.7 application.

I'm wondering is there a way to conditionally compile parts of the java code for one or the other version.

I know there is a way to do it for widgets and browsers in the module XML file.

A: 

If you really want to do that I guess the approach would be to use a Generator.

With a generator you can have deferred binding (a bit a poor man's introspection).

To get access to the 1.5 or 1.7 code you then have to define the generic API in an interface and use GWT.create on it to get the concrete implementation.

David Nouls
+1  A: 

The solution is very simple.

String version = GWT.getVersion();
if (version.startsWith("1.5"))
{
  // do something the 1.5.* way
}
Drejc
A: 

if (version.startsWith("1.5")) { // do something the 1.5.* way }

Is actually kind of a bad idea. Ideally you should use absolute string values here, because the GWT compiler will evaluate equality and trim unreachable code inside an if conditional from the compiled output. Using .startsWith means that all the code will end up in the final application.

Unfortunately GWT version is not a compile time property. Perhaps the best way to do it would be to create an empty Generator implementation that just returns "ClassName15" or "ClassName17" from a call in your module for "ClassName". You can then, at compile time, call About.version() from the generator and find out what the version is at compile time.

kebernet
Thanks for pointing that out, but this is the only solution I have found. And it is only for a small code snippet. So the solution works just fine for me.
Drejc