简体   繁体   中英

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.

The solution is very simple.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM