4

I am new to GWT. i have below line of code.

SomeClientServiceAsync someService = GWT.create(SomeClientService.class);

what does above line means and why cannot i use any other alternatives to instantiate it?

Please help me!

Thanks.

ColinE
  • 68,894
  • 15
  • 164
  • 232
user1016403
  • 12,151
  • 35
  • 108
  • 137

3 Answers3

7

GWT.create is used for deferred binding. This allows you to supply different implementations of the same service based on the user's browser. See the following question:

Why use GWT.create() instead of new?

If you do not need to have multiple implementations of your service, just create it via new!

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232
1

GWT.create works in different ways:

  • It tries to see if in the gwt.xml files there is no declaration of which implementation to use depending on a GWT property. This GWT property can be the well known user agent which in this case will have the effect of selecting different implementations for each browser, but it can also be used for other things, for example to disable logging (the fact that logging is enabled or not has nothing to do with in which browser it runs)

Example:

<replace-with class="com.x.app.client.ui.base.button.CustomSlicedButtonCellAppearance">
    <when-type-is class="com.x.app.client.ui.base.button.CustomButtonCellAppearance" />
    <when-property-is name="gxt.css3.enabled" value="false"/>
    <when-property-is name="gxt.theme" value="themeName" />
</replace-with>

In this case it will use CustomSlicedButtonCellAppearance for a call to GWT.create(CustomButtonCellAppearance.class) only if css3 is not supported and for the given theme. Notice that "when-property-is" is optional, and if not supplied it will always use that implementation for the given interface.

  • It also looks for generators, in which case a new class is generated during GWT compilation (or in devmode) usually based on annotation that are present in the interface passed to the create method.

Example:

<generate-with class="org.fusesource.restygwt.rebind.RestServiceGenerator">
  <when-type-assignable class="org.fusesource.restygwt.client.RestService" />
</generate-with>

In this case the RestServiceGenerator will generate code to submit the request. Another example is how UIBinder works: besides using the annotations in the interface it also generates code based on what is inside the ui.xml file.

  • If no declaration matches the class/interface passed to the GWT.create method, then it will try to do a new on that class (in case of an interface it will fail).

Declarations in gwt.xml files can be overwritten by other declarations that are processed afterwards, so if you are using a module which declares a rule you can change that rule by declaring a new rule after the inherits declaration of the module containing the original declaration.

1

GWT works by creating a service just like RMI does. Here you are creating the service SomeClientService which resides in the client package. It contains all the functions that can be called server-side.

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89