Consider the following source (made with -style pretty):
package x.y.z;
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.ui.*;
public class WastedWeekend extends Label implements EntryPoint {
public void onModuleLoad() {
foo();
}
void foo() {
Button[] stuff = new Button[1];
doNothing(stuff[0] = new Button());
}
void doNothing(Widget w) {
RootPanel.get().add(w);
}
}
The foo() function translates into the following:
function _$foo(_this$static){
var _stuff;
_stuff = _initDims('[Lcom.google.gwt.user.client.ui.Button;', [0],
[5], [1], null);
_$doNothing(_this$static, _stuff[0] = _$Button(new _Button()));
}
This is all well and good. But now let me change, in foo() the type of
"stuff" to Label
(that is, the superclass of WastedWeekend). That is, as follows:
void foo() {
Label[] stuff = new Label[1];
doNothing(stuff[0] = new Label());
}
Now the translation is:
functionfunction _$foo(_this$static){
var _stuff;
_stuff = _initDims('[Lcom.google.gwt.user.client.ui.Label;', [0],
[7], [1], null);
_$doNothing(_this$static, _setCheck(_stuff, 0, _$Label(new
_Label())));
}
Notice the difference? What's going on?
This is reproducible whenever this sample WastedWeekend class is the
same subclass of the same Widget as the "stuff" variable. (I did not go
into more details as far as inheritance tree, so I don't know what
happens if they are siblings or something)...
This is a copy-paste error. Should just be "function". These are not
the droids you are looking for...
foo() is crateing an array of widgets(well in this case Buttons) but
your doNothing() accepts only a single Widget. The types dont match up
correctly - the compiler should have spotted this error.
Thanks; but out of curiousity, why is it legitimate to have a different
translation only in this case, when the class extends from the same
Widget that is being used in the example?
In Java, storing into an array causes a run-time type check to ensure
that you're storing an object whose run-time type is legal to put into
the array. See the documentation for ArrayStoreException.
We implement this behavior throught the setCheck() call. The reason
you're seeing simpler code in one case is that the compiler has
statically determined that the object you're storing into the array is
definitely the right type, so it doesn't generate the check.
Scott