views:

1481

answers:

3

I've got a method JSNI that calls a Java method that take a Hasmap as input. I've tried

[email protected]::myMethod(Ljava/util/Hashmap;)(myHashMap);
[email protected]::myMethod(Ljava/util/Hashmap<Ljava/lang/String,Ljava/lang/String>;)(myHashMap);

I'm can't seem to define the correct type signature to include the Strings or find if this usage is even allowed.

Since I'm doing this in gwt I though it might be the implementation of hashmap and the alternative approach I've though takes a String[][] array as input

I was hoping for somwthing like

[email protected]::myMethod([[Ljava/lang/String;)(myArray);

However, I hit another issue of finding the correct JNSI sntax for the 2nd dimension of the array

A single dimension array ie. [Ljava/lang/String; is fine but I need the 2nd dimension.

Any help/ideas or links to good jnsi doc appreciated.

A: 

Can you post the error that your getting, and also what kind of javascript object your trying to pass as a hashmap. I'm assuming you're getting a compile time error?

Here is a good start for JSNI documentation:

GWT JSNI doc

GWT Blog post on JSNI

rustyshelf
A: 

Ok, after looking at it...

I was unable to find any documentation or arrangement that gives the multidimensional array of strings. Managed to get this done using the HashMap, using:

[email protected]::myMethod(Ljava/util/Hashmap;)(myHashMap);

worked if I define the input without defining the types. Such as:

HashMap myHashMap = new HashMap();

This gives the JSNI a HashMap of type <Object, Object>.

I'm then handling the object on the other side by casting the contents of the hashmap into strings.

Thanks to rusty for the links:

GWT JSNI doc - This is good for getting the correct formatting of primitives

GWT Blog post on JSNI - Hadn't seen this blog before

sre
A: 

I think you're running into type erasure. Every generic object parameter is really just java.lang.Object at runtime. I don't believe generics are exposed to JNI.

I've written JNI code but never attempted to use generic types from native code so I'm not certain. Googling has turned up no specific references or examples.

See Java VM Type Signatures for a reference to the type signatures used in JNI (and JSNI)

However, you may not need to pass a Java HashMap to Javascript anyway. Instead, consider using JSONObject and passing a native JavaScript object to your javascript code. It looks like this:

  public void callFoo() {
    JSONObject obj = new JSONObject();
    obj.put("propertyName", new JSONString("properyValue"));
    JavaScriptObject jsObj = obj.getJavaScriptObject();

    nativeFoo(jsObj);
  }

  public native void nativeFoo(JavaScriptObject obj) /*-{
    $wnd.alert(obj['propertyName']);
  }-*/;

This gets compiled to roughly:

var obj = {'propertyName': 'propertyValue'};
$wnd.alert(obj['propertyName']);
Mark Renouf