Thursday, May 19, 2016

Re: jsinterop: converting a jre collection to a jsArray

It would be nice to have a way to convert arrays and string to\from Js shipped by the compiler

A documented way to pass java.lang.String or byte[] to JS.

On Thursday, May 19, 2016 at 11:06:12 AM UTC+3, Vassilis Virvilis wrote:
I agree

I am just saying that java String is not the same as the js String. So String[] cannot be converted easily to a js string array.

I agree that arrays in other @JsType objects works as expected.

The main difference if I understand your point of view is that I have to create a js string array from java where in your case it is handed to you by javascript and you have to cope with that.

Thanks for the insights

On Wed, May 18, 2016 at 11:25 PM, Pablo Nussembaum <> wrote:
If you have a JsInterop object that returns an []
you can do:

Object o = jsInteropObject.method(); //this in declared as "public Object method();" in your @JsType object
JsArray<JavaScriptObject>  array = JsInteropHelper.cast(o);

On 18/05/16 11:38, Vassilis Virvilis wrote:

@Pablo, I don't see how the example you gave relates to arrays and jsinterop

I am not exactly sure about but looks like that String[] is an array of char arrays instead of native javascript Strings. For example this also works.

config.buttons = new JsString[] {Util.getString("copyHtml5")};

Where JsString and Util.getString are:

    @JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "String")
    public static interface JsString {

    public static native JsString getString(String string) /*-{
        var l = string.length;
        var s = "";
        for (var i = 0; i < l; i++) {
            s += string[i];
        return s;

Thanks in advance.

On Thu, May 12, 2016 at 2:46 PM, Pablo Nussembaum <> wrote:
Hi Vassilis,
I have been using JsInterop for managing IndexedDB stores and I have created this [1] helper class for casting back and forth js to java. The main issue is that in GWT there are two Object hiearchies one from java and js, GWT makes are really job hiding that detail, but with JsInterop the difference leaks ;-)


Let me know if want examples where I'm using this helper class.


On 12/05/16 07:43, Vassilis Virvilis wrote:
Thanks for all the answers

The Collection#toArray does not work in my case because are a bit sensitive in the input. It stumbles upon java.lang stuff later on.
buttons = new String[] {"copyHtml5"};
while buttons2 is the correct one created with your previous suggestion.
  1. buttons:
    1. 0:"copyHtml5"
    2. __elementTypeCategory$:6
    3. __elementTypeId$:2
    4. java_lang_Object__1_1_1clazz:java_lang_Class_Class__V
    5. java_lang_Object_castableTypeMap:Object
    6. java_lang_Object _typeMarker:com_google_gwt_lang_Runtime_typeMarkerFn__V()
    7. length:1
    8. __proto__:Array[0]
  2. buttons2:Array[1]
    1. 0:"copyHtml5"
    2. length:1
    3. __proto__:Array[0]
About Array vs []
So I though if that could work with GWT and I tried it.

Also thanks for the link I bookmarked it.

On Thu, May 12, 2016 at 1:20 PM, Thomas Broyer <> wrote:

On Thursday, May 12, 2016 at 11:51:11 AM UTC+2, Vassilis Virvilis wrote:
Thomas thanks,

Wow I did this and it works. But it is crazy. Cr is for crazy. Kudos to jsinterop guys though.

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "[]")

You should probably use "Array" rather than "[]"
public interface CrJsArray<T> {

    public void push(T value);

    public int getLength();

I tried your suggestion to cast and __also works__ (eclipse complains about but with (JavaScriptObject) (Object) my_native_object) it works and it is definitely better.

Also, for converting from a java.util.Collection to an array, how about simply using Collection#toArray? (and then passing the array to JS through JsInterop)
1) Is JavaScriptObject scheduled for deprecation?

Yes, it'll be replaced with JsInterop.
2) Should a native type be able to extend JavascriptObject. Because right now (2.8pre1) fails at run time. That would make casting a bit saner. Should I report it somewhere?

This is probably on-purpose.
I tried to look for the reference you gave but I didn't find it. Wrong keywords maybe...

Not exactly what I remembered, but touches similar points.

On Thu, May 12, 2016 at 12:20 PM, Thomas Broyer <> wrote:
That question came up already in the forum or in GWT-Contrib (try to search the archive), and I seem to recall that Goktug said you could cast @JsType(isNative=true) objects to/from JavaScriptObject, so you could use JsArray (and JsonUtils.safeEval). I think you can also cast to/from an array of @JsType(isNative=true), so if you don't need to alter the size of the JS array you can then use the bracket operators on a Java array (and cast from/to a JavaScriptObject).
This is only out of memory, please double check from the forum archives.

On Thursday, May 12, 2016 at 10:47:51 AM UTC+2, Vassilis Virvilis wrote:
Hi again,

I am trying to convert a jre collection or build a jsArray with a isNative=true type.

Unfortunately this is not possible because JsArray requires JavascriptObject which is invcompatible (complie time error) with native classes.

Any ideas?

I looked on deep dive jsinterop from Ray Cromwell and it says that there is not a clean way for jsinterop1 to align collections. Ok I get that - but there must be a way even if not the nicest/cleanest one.

Otherwise looks like I have to create my own @jsinteroped MyJsArray which looks wrong.

Vassilis Virvilis

You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment