How to invoke Java String methods from JSNI code

105 views
Skip to first unread message

alucard

unread,
Feb 15, 2013, 5:53:58 AM2/15/13
to google-we...@googlegroups.com
This does not work:

private static native int test(String str) /*-{
   return str.@java.lang.String::compareTo(Ljava/lang/String;)("test");
}-*/;

It dies with the following exception:

com.google.gwt.core.client.JavaScriptException: (Error) @test.client.Test::test(Ljava/lang/String;)([string: 'hello']): Error calling method on NPObject.
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)

Is it possible (if yes please write how) to invoke String methods from JSNI?

Thanks for the help.

Patrick Tucker

unread,
Feb 15, 2013, 8:26:43 AM2/15/13
to google-we...@googlegroups.com
Does str < "test" not get the result you want?

alucard

unread,
Feb 15, 2013, 8:48:52 AM2/15/13
to google-we...@googlegroups.com
Yes, but that was not the question. I'd like to be able to invoke methods from the Java String implementation. The code above is meant just as an example for the error I get.

Hilco Wijbenga

unread,
Feb 15, 2013, 1:17:50 PM2/15/13
to google-we...@googlegroups.com
On 15 February 2013 05:48, alucard <slice.of...@gmail.com> wrote:
> Yes, but that was not the question. I'd like to be able to invoke methods
> from the Java String implementation. The code above is meant just as an
> example for the error I get.

The format object.@Class::method(param;)(arg) you are using seems fine
(I use it in multiple places). Are you sure the error is not in the
call to your "test" method? Has it been properly exported?

Thomas Broyer

unread,
Feb 15, 2013, 5:15:40 PM2/15/13
to google-we...@googlegroups.com
String is a special beast, as it's emulated by a JS String. It wouldn't surprise me if that use case weren't supported. I grep'ed the source code of GWT and didn't find any such use.

alucard

unread,
Feb 15, 2013, 6:13:04 PM2/15/13
to google-we...@googlegroups.com
On Friday, February 15, 2013 11:15:40 PM UTC+1, Thomas Broyer wrote:
String is a special beast, as it's emulated by a JS String. It wouldn't surprise me if that use case weren't supported. I grep'ed the source code of GWT and didn't find any such use.
Yeah. I found this in the docs for the emulated string:

For efficiency we handle String in a specialized way, in fact, a
java.lang.String is actually implemented as a native JavaScript String. Then
we just load up the prototype of the JavaScript String object with the
appropriate instance methods.

So it should be possible to use the instance methods but not with the object.@java.lang.String... notation. Any ideas on what could be used instead?

Thomas Broyer

unread,
Feb 15, 2013, 6:34:05 PM2/15/13
to google-we...@googlegroups.com


On Saturday, February 16, 2013 12:13:04 AM UTC+1, alucard wrote:
On Friday, February 15, 2013 11:15:40 PM UTC+1, Thomas Broyer wrote:
String is a special beast, as it's emulated by a JS String. It wouldn't surprise me if that use case weren't supported. I grep'ed the source code of GWT and didn't find any such use.
Yeah. I found this in the docs for the emulated string:

For efficiency we handle String in a specialized way, in fact, a
java.lang.String is actually implemented as a native JavaScript String. Then
we just load up the prototype of the JavaScript String object with the
appropriate instance methods.

So it should be possible to use the instance methods but not with the object.@java.lang.String... notation. Any ideas on what could be used instead?


You can use a "bridge" method:

public static int stringCompareTo(String a, String b) {
  return a.compareTo(b);
}

private static native int test(String str) /*-{
   return @com.example.MyClass::stringCompareTo(Ljava/lang/String;Ljava/lang/String;)(str, "test");
}-*/;

alucard

unread,
Feb 16, 2013, 4:04:17 AM2/16/13
to google-we...@googlegroups.com
Thanks for the tip. I'll go with that.
Reply all
Reply to author
Forward
0 new messages