views:

132

answers:

3

I'm reading the docs on the UIBinder of GWT and the first code snippet made me confused:

public class HelloWorld extends UIObject { // Could extend Widget instead
  interface MyUiBinder extends UiBinder<DivElement, HelloWorld> {}
  private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

  @UiField SpanElement nameSpan;

  public HelloWorld() {
    // createAndBindUi initializes this.nameSpan
    setElement(uiBinder.createAndBindUi(this));
  }
}

On the second line an interface is created locally which extends the UiBinder interface. However, on the third line an instance of this interface is created using the GWT.create() method. How is this possible? There's nowhere a class that implements MyUiBinder, so it can't be instantiated right?

A: 

I'd assume a dynamic implementation is created. For example using javassist or CGLIB.

However, this case is a bit difficult, because this class is then translated to javascript.

Bozho
+3  A: 

GWT.create is treated specially by the GWT Java compiler at compile time. The GWT class is the place where Google puts the low-level "magic" that makes GWT work.

More details under this question.

Daniel Earwicker
A: 

Here is a rather old answer (old in SO terms ;) ) that covers the same topic.

As far as I understand: it does not work magically but with the little help of a generator that knows how to create an instance for a given interface (some MyUIBinderGenerator in your example). And that generator has to be implemented and published in some gwt.xml file.

Andreas_D