Search code examples
javajavascriptgwtjsni

Passing Long array to a JSNI function


How do I pass a Long[] array or List<Long> to a JSNI function like this:

public native void updateData(Long[] data)/*-{
    waveform.update({
        data: [0.5, 1.0, 0.5, 1.0]
    });
}-*/;

Solution

  • You can't, for various reasons.

    • a Java array (whichever its type) would be an opaque object to JSNI. You have to use a JsArray (or JsArrayNumber or etc.). Same for a java.util.List.
    • java.lang.Long would be an opaque object to JSNI
    • even with long it wouldn't work, because you cannot pass a long to JSNI. This is because not all longs can be represented as JS numbers. The equivalent of a JS Number is double.

    You have to copy your Long[] into a JsArrayNumber, and cope for the possible overflows when down-casting the long value into a double value.