Bizarre translation: what's going on here?

1 view
Skip to first unread message

DEBEDb

unread,
Sep 24, 2006, 12:43:55 AM9/24/06
to Google Web Toolkit
I have encountered a strange issue. I have spent some time trying to
reproduce it in a concise way, so please ignore the fact that this code
is really meaningless :)

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

DEBEDb

unread,
Sep 24, 2006, 2:48:50 AM9/24/06
to Google Web Toolkit
> functionfunction _$foo(_this$static){

This is a copy-paste error. Should just be "function". These are not
the droids you are looking for...

mP

unread,
Sep 24, 2006, 8:12:44 PM9/24/06
to Google Web Toolkit
Are you sure your example is right...

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.

DEBEDb

unread,
Sep 24, 2006, 8:40:32 PM9/24/06
to Google Web Toolkit
Foo is creating an array, but what is passed into doNothing is a single
widget. It is being assigned to an element of an array. This is all
correct... The types match fine. Try it.

Vivian Li

unread,
Sep 26, 2006, 2:20:09 AM9/26/06
to Google-We...@googlegroups.com

Hi DEDEDb,
  I have confirmed that there is a bug here. The problem is not that there are two different translations occuring for the foo() function, which is legitimate for the GWT compiler to do. The problem is that the _setCheck method returns void when it should return the value which is being assigned. This bug has been filed with our issue tracker.

-Vivian

DEBEDb

unread,
Sep 26, 2006, 2:52:20 AM9/26/06
to Google Web Toolkit
Hi Vivian,

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?

Scott Blum

unread,
Sep 26, 2006, 12:17:42 PM9/26/06
to Google-We...@googlegroups.com
Hi DEBEDb,

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

Reply all
Reply to author
Forward
0 new messages