Drejc
Drejc

Reputation: 14286

GWT conditional compile

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.

Upvotes: 1

Views: 1084

Answers (3)

kebernet
kebernet

Reputation: 192

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.

Upvotes: 0

Drejc
Drejc

Reputation: 14286

The solution is very simple.

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

Upvotes: 1

David Nouls
David Nouls

Reputation: 1895

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.

Upvotes: 0

Related Questions