JavaScriptObject generate a json with a Integer field

48 views
Skip to first unread message

Sandro Munda

unread,
Mar 20, 2012, 7:49:10 AM3/20/12
to google-we...@googlegroups.com
Hello everybody !

I have a subclass of a JavaScriptObject with a generic type T.

public class Foo<T> extends JavaScriptObject {
// ...
}

In this class, I have a getValue() method that returns a T value.

private final native T getValue() /*-{
return this.value;
}-*/;

In my situation, T is an Integer. When I running the code I have the
following exception :

Caused by: java.lang.ClassCastException: Cannot cast
java.lang.Integer to com.google.gwt.core.client.JavaScriptObject

Is the value is a 'int', it works well. But not the Integer.

How can I fix the problem ?
Any workarrounds ?

Thanks !

Sandro Munda
<munda....@gmail.com>

Thomas Broyer

unread,
Mar 20, 2012, 9:28:04 AM3/20/12
to google-we...@googlegroups.com
You cannot use Integer as a generic parameter in this case; you have to code specifically for the integer or other "primitive wrapper") case; something like:

public final Integer getValue() {
    return hasValue() ? Integer.valueOf(getValueInt()) : null;
}

private final native boolean hasValue() /*-{
    return this.value == null;
}-*/;

private final native int getValueInt() /*-{
    return Number(this.value);
}-*/;

Sandro Munda

unread,
Mar 21, 2012, 5:25:05 AM3/21/12
to google-we...@googlegroups.com
Thanks ! Exactly.

Sandro Munda
<munda....@gmail.com>

> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/nrBiWtOwQqcJ.
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-web-tool...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages