How to create a shared lib/utility class (client/server) ?

46 views
Skip to first unread message

Jens

unread,
Nov 2, 2011, 10:01:38 AM11/2/11
to google-we...@googlegroups.com
Hi,

I am trying to implement a small utility class / library that should be useable on client side and on server side but has different implementation on client and server.

For example I have a utility method that should make a http REST call. On client side I should use RequestBuilder but on server side I have to use java.net.* classes. How would I do that?

I tried the factory approach in combination with GWT.isScript() but this didn't work because the GWT compiler still sees java.net.* classes and wants to translate them:

public class SharedUtils {
  private static final SharedUtilsFactory FACTORY = GWT.isScript() ?
       new SharedUtilsFactoryJS() : new SharedUtilsFactoryVM();

  public static RequestUtils requestUtils() {
    return FACTORY.requestUtils();
  }

}

Shouldn't this somehow be possible?

Thanks for help,

-- J.

Thomas Broyer

unread,
Nov 2, 2011, 10:47:03 AM11/2/11
to google-we...@googlegroups.com
You have to use 'super-source'. See "Overriding one package implementation with another" at http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModuleXml

Jens

unread,
Nov 2, 2011, 1:03:29 PM11/2/11
to google-we...@googlegroups.com
Ah well ok that works. I have already done this in a different context (globally overwriting a GWT client class), don't know why it doesn't came into my mind :)

The only "small issue" that remains is that Eclipse always shows the VM implementation when clicking into the source code (jar library only contains VM .class + .java files and emulated GWT .java files without class files). Would be nice if Eclipse would jump to the emulated client source code but I don't think its possible. 
Although its working it just feels a bit odd if you suddenly see java.net.* classes if you use the lib only on client side.

-- J.
Reply all
Reply to author
Forward
0 new messages