Tuesday, February 25, 2014

Re: How to load a JRE-dependent/non-compile-mode-supported class in GWT Dev Mode

Thanks for the quick response.  I haven't tried it yet, but will soon.

I want to use Object.clone() because I have a situation where I need to create a (shallow) duplicate of a Java class that differs from the original only by virtue of pointing to a different JS object (with one member).  The class may be one I have no control over, such as something user writes, so I can't insist that it implement any particular API.  (I guess the ideal situation would be to have my root class A implement myClone(), which clones by just calling a constructor for that class with the right arguments, and then require that any class B that a user writes to extend A override myClone().  However, I don't think I can require that.)

On Tuesday, February 25, 2014 3:19:29 AM UTC-6, Thomas Broyer wrote:

On Tuesday, February 25, 2014 8:53:39 AM UTC+1, Benjamin Bitdiddle wrote:
For a particular GWT module, I'm able to solve a problem in compiled mode by manipulating the JS object representing the Java object.  (Specifically, I need to clone the object, so I simply create a new JS object, copy the properties from the original JS object, and assign a new hash value.)  I've tested this an it works OK.

Since that approach (that I use in compiled mode) doesn't work in Dev Mode, I want to use Object.clone(), which is available in Dev Mode, to perform the clone operation. However, I'm not sure how to accomplish this as every approach I try hits a problem.
  • I can't seem to use deferred binding (i.e. GWT.create() and "replace with" in the module gwt.xml file), for two reasons:
    1. Even in Dev Mode, when I replace a class using "replace with", the class I swap in seems not to be able to use JRE-only methods, such as reflection or Object.clone.  I get an error that the "source code for the method is not available".  I don't want this class compiled, but it seems to want to compile it to JS.
    2. If I solve #1, then I have to figure out how to get the "replace with" to be conditional based on being in dev mode.  I'm using ant to build the compiled-mode stuff and I'm using Eclipse to run dev mode.  Is there way to make this automatic so I can use the same module gwt.xml in Eclipse and for an ant build?  I guess I'd need an ant property that automatically is set by compiled but not by development mode, or vice versa?
  • I also tried simply using new Foo() (rather than GWT.create() and deferred binding), placing the JS-copying/GWT-compile-friendly version of Foo in the normal location.  This works for GWT compiled mode obviously.  Then for Dev Mode, I add a new source directory to the class path in Eclipse and stick a different class Foo there that uses the JRE-only APIs (such as Object.clone).  However, this hits the same problem as deferred binding - I still get an error that "the source code is not available" for various APIs I use in that version of Foo (e.g. reflection APIs).

Right now, I've managed to use the Eclipse expression panel to manually load the JRE-version of Foo I mentioned above (in the special class path segment).  So I know it's possible for it to coexist with other Dev Mode classes, but how do I load it programmatically as part of starting GWT DevMode class?

It seems like you're looking for super-source and @GwtScriptOnly.

In your standard source location you'll put the class you want to use in DevMode, then in the super-source (you have to declare a <super-source path="super" /> in your gwt.xml) you put the version of the class you want to use in web mode, and you annotate it with @com.google.gwt.core.client.GwtScriptOnly (note: that class only exists in super-source too, so you won't find it by auto-complete in your IDE). See http://www.gwtproject.org/doc/latest/DevGuideOrganizingProjects.html#DevGuideModuleXml
This is used in many places in GWT if you need examples: WeakMapping, StringCase, SafeHtmlHostedModeUtils, etc.

(that said, I suspect you're doing something Wrong™ if you need this for Object.clone(), and you'd better try to find an alternative)

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment