I recently ran into an issue where a class I share between GWT and server-side java code would really, really benefit from being able to accept or return a Calendar object. However, GWT doesn’t come with a Calendar class, so the appeared to be impossible. Or, is it?
After some digging around in the documentation I discovered that GWT provides a directive in their module XML called “super-path”. By adding something like:
<super-path path=”gwtonly”/>
To my *.gwt.xml file, I can tell GWT to load my own emulation classes when compiling javascript; when running in hosted mode, it will use the Java implementation of these classes.
Here’s an example source tree structure for this:
- myapp/MyApp.gwt.xml
- myapp/client/*.java
- myapp/gwtonly/java/lang/Calendar.java
- myapp/gwtonly/java/lang/GregorianCalendar.java
In my Calendar and GregorianCalendar I just define exactly the methods and constants that I need for the code to *compile*. Note that I’m not planning to actually use these classes; I could, but the implementation is so minimal it would cause confusion for future programmers who wouldn’t understand why the calendar class behaved so weirdly in client-side code.
However, now I discovered that I have errors in my code when I compile. Huh? In eclipse it’s happy but in GWT it says something like “package declaration should be java.lang, but it should be myapp.gwtonly.java.lang”. Oh, I see, it’s really using that package as a source folder, not as a package. So, I fix it by changing the “package” declaration at the top to “java.lang”. Guess who is unhappy now? eclipse! The two compilers can’t see eye-to-eye any more.
There is a solution – eclipse has a feature called “exclusion filters”. I open the “Java Build Path” panel for the project, find the source folder that has all of this in it, and add an “exclude” filter for myapp/gwtonly/. Now eclipse just ignores those files; I’ve lost a lot of eclipse java features as a result, but at least it works.
All this is part of my work on simple online accounting software, if you’re a consultant, entrepreneur, virtual assistant, bookkeeper, or freelancer go check it out.