Friday, October 25, 2013

Re: What happend after GWT.create()


On Friday, October 25, 2013 12:33:39 PM UTC+2, Andrey Damintsev wrote:
Hello!
I use GWT about half year.  But I cant understand what happens after GWT.create().  I read some FAQs articles and there said that occurs a deferred bindings. Ok! But how It realised from Java.

I start to read sources of ServerGwtBridge class, but cannot understand.
Can somebody advice any articles or something, that can explain this for me/

Most uses of GWT.create() are on client-side code (server-side use is really new and I don't know of anyone using it).
In client-side code, GWT.create() uses deferred binding: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsDeferred.html.

You pass a Class literal to GWT.create() (note that you cannot pass a variable of type Class<?>, it must be a SomeClass.class construct, at least for now — it might change in the future to allow more flexibility).
GWT then evaluates all <replace-with> and <generate-with> conditions in order to find a matching rule. Once a rule is found, it is executed and the resulting class is instantiated using its default constructor (so, the class must be not be abstract –or an interface or enum–, must be static, and must have a public constructor with zero arguments). In the absence of matching rule, the class is used as-is (you could think of it as the class being replaced by itself).

So, imagine you have a class Foo and no single deferred binding rule in your gwt.xml files with <when-type-is name="com.example.Foo"/> or <when-type-assignable name="com.example.Foo"/>, then "GWT.create(Foo.class)" is exactly equivalent to "new Foo()".
Now let's say you have a class "Bar extends Foo" that you want to use when the browser is IE6 or IE8 (because, as everybody, they don't work like the others, so you want a special implementation for them). You just have to add a rule to your gwt.xml file telling GWT to use Bar instead of Foo, and then the GWT.create(Foo.class) will be rewritten into either "new Foo()" if the browser is Firefox or Chrome (to name a few), or to "new Bar()" if the browser IE6 or IE8 (nota: or any IE version that's "emulating" IE6 or IE8):

 <replace-with name="com.example.Bar">
   <when-type-is name="com.example.Foo" />
   <any>
      <when-property-is name="user.agent" value="ie6" />
      <when-property-is name="user.agent" value="ie8" />
   </any>
</replace-with>

Deferred binding can also trigger code generators (<generate-with>). The most common use is to pass an interface class-literal to GWT.create() and have the generator generate an implementation of that class; the output from the generator can be different for each permutation (if you don't know what a permutation is, don't be afraid to ask, but you should be able to find the definition in the docs; if it's not clear, tell us, the docs can be enhanced/fixed). This is used for I18N, ClientBundle, UiBinder, the Editor Framework, Request Factory, PlaceHistoryMapper, etc. One special case is GWT-RPC, where the returned object does not implement the class-literal passed as argument; this is some legacy that we have to live with.
Generators are not limited to that though. They can work on classes, abstract or not, and they can even generate other files (this is used by ClientBundle and UiBinder), even without causing a "change" to the Java code.

Server-side use of GWT.create() works totally differently (it won't read gwt.xml files), but with the same effects overall: there are registered factories for each type, and the appropriate factory is looked up and called to instantiate an object. How the factory resolves which class to instantiate is left to its discretion. One goal for the future is to have I18N working on the server-side, where many language-specific classes would be generated at compile-time, and the factory would instantiate the correct one depending on the "current locale" (for the user).

--
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