A Simple Example of Deferred Binding in GWT
Deferred Binding in GWT lets you swap classes at compile time and have GWT generate the result into JavaScript as if you’d coded it that way from the start. Sort of like reflection but at compile time instead of runtime. It’s a bit of nonsense to allow extension of GWT beyond what the native compiler can do.
As a side-note, GWT’s big value add is that it generates JavaScript off of Java. It also does some things during compilation which will speed up the JavaScript it produces de-virtualizing methods and so forth. We used to do this back in the day with C, unwinding loops, in-lining assembly code and so on. GWT does similar stuff to shave milliseconds off processing time. However, it’s questionable since GWT’s flagship application: Google Wave has a resource profile that weighs in at a whopping 3MB and looks like this:
The first thing you need is an interface, this has to go in your client package from what I can tell:
Human.java:
package com.crap.crapGWT.client;
public interface Human {
void WhatAmI();
}
Then you need a couple of classes that implement this interface:
Woman.java:
package com.crap.crapGWT.client;
import com.google.gwt.user.client.Window;
public class Woman implements Human {
public void WhatAmI() {
Window.alert("I'm a lady");
}
}
and
Man.java:
package com.crap.crapGWT.client;
import com.google.gwt.user.client.Window;
public class Man implements Human {
public void WhatAmI() {
Window.alert("I'm a dude");
}
}
In onModuleLoad() add the following:
Human myHuman = (Human)GWT.create(Human.class); myHuman.WhatAmI();
Then you define some properties, you can put the following in your main module XML file OR you can put it in its own file. If you do the latter you must inherit that XML file in the main module.
<define-property name="human" values="man,woman" />
<property-provider name="human">
<![CDATA[
return __gwt_getMetaProperty("human");
]]>
</property-provider>
Believe it or not the above is actually JavaScript! (wasn’t this what we wanted to avoid by adopting GWT?)
Then in your main XML module put the rules by which the classes are swapped out. E.g. below, if you find a human property set to “man” replace this with the Man class.
<replace-with class="com.crap.crapGWT.client.Man"> <when-type-is class="com.crap.crapGWT.client.Human" /> <when-property-is name="human" value="man" /> </replace-with> <replace-with class="com.crap.crapGWT.client.Woman"> <when-type-is class="com.crap.crapGWT.client.Human" /> <when-property-is name="human" value="woman" /> </replace-with>
And finally add in the actual property itself in the HTML file in your WAR.
<meta name='gwt:module' content='js/gwt=com.crap.crapGWT.client.Human'/> <meta name='gwt:property' content='human=woman'/>
If everything goes well you should get a popup saying “I’m a lady”.
If you have trouble, go back and check everything, particularly the class namespaces. If all looks well yet you still can’t get it to work, restart Eclipse (*yes* I’m not kidding).
Then finally change the property to a “man” and you’ll get a different message.
<meta name='gwt:property' content='human=man'/>
You can try this also:
if (myHuman instanceof Human) Window.alert("myHuman is a Human");
if (myHuman instanceof Man) Window.alert("myHuman is a Man");
if (myHuman instanceof Woman) Window.alert("myHuman is a Woman");
So that’s Deferred Binding.









If you need to do stuff like this don’t go for GWT. It’s great if you stick to the basics but for large sites, enterprise apps it gets out of control.
Hello
Nice tutorial. I followed the tutorial from start to finish here is the error I have:
Compiling module org.gdb.binding.Testbindings
[ERROR] Errors in ‘file: / C: / eclipse / workspace / testbindings / src / org / gdb / binding / client / Testbindings.java’
[ERROR] Line 13: rebind result ‘org.gdb.binding.client.Human’ Must Be a Class
[ERROR] Can not shall proceed due to previous errors
Écouter
Lire phonétiquement