JSNI syntax question - calling Java methods from JSNI

24 views
Skip to first unread message

Bryan

unread,
Nov 19, 2009, 7:07:08 PM11/19/09
to Google Web Toolkit
I'm following the example seen here for 'calling Java methods from
JSNI' and it works great.

http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-gwt-part-1-jsni.html

I'm trying to do something slightly different though. Here's what it
is.

package com.foo.bar;

public class MyClass {

private void javaMethod(String s) {
Window.alert("called the java method with String " + s);
}

private native String nativeMethod() /*-{

var myFunc = function() {
// trying to call the 'javaMethod' method from here and the
notation
// at the link above does not work
//
// notation that is not working for me
// this.@com.foo.bar.MyClass::javaMethod(Ljava/lang/String;)
('coming from nativeMethod.myFunc');
}

}-*/;

}

It doesn't surprise me that what I'm trying doesn't work, I'm assuming
the 'this' I'm trying to use is in the wrong scope. Anyone have any
suggestions for how I can get to that java method from within a native
method that defines a function?

Bryan

unread,
Nov 19, 2009, 7:38:11 PM11/19/09
to Google Web Toolkit
I came up with something that works by making 'javaMethod' static. I
can call it like so that way.

@com.foo.bar.MyClass::javaMethod

This feels a bit like I'm jumping through a hackish hoop simply
because I don't know the syntax that is likely available to me for
doing the job. Any help would be greatly appreciated.

On Nov 19, 5:07 pm, Bryan <bwn...@gmail.com> wrote:
> I'm following the example seen here for 'calling Java methods from
> JSNI' and it works great.
>
> http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-g...
>
> I'm trying to do something slightly different though.  Here's what it
> is.
>
> package com.foo.bar;
>
> public class MyClass {
>
>         private void javaMethod(String s) {
>                 Window.alert("called the java method with String " + s);
>         }
>
>         private native String nativeMethod() /*-{
>
>                 var myFunc = function() {
>                         // trying to call the 'javaMethod' method from here and the
> notation
>                         // at the link above does not work
>                         //
>                         // notation that is not working for me
>                         // th...@com.foo.bar.MyClass::javaMethod(Ljava/lang/String;)

Thomas Broyer

unread,
Nov 20, 2009, 6:22:07 AM11/20/09
to Google Web Toolkit


On Nov 20, 1:07 am, Bryan <bwn...@gmail.com> wrote:
> I'm following the example seen here for 'calling Java methods from
> JSNI' and it works great.
>
> http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-g...
>
> I'm trying to do something slightly different though.  Here's what it
> is.
>
> package com.foo.bar;
>
> public class MyClass {
>
>         private void javaMethod(String s) {
>                 Window.alert("called the java method with String " + s);
>         }
>
>         private native String nativeMethod() /*-{
>
>                 var myFunc = function() {
>                         // trying to call the 'javaMethod' method from here and the
> notation
>                         // at the link above does not work
>                         //
>                         // notation that is not working for me
>                         // th...@com.foo.bar.MyClass::javaMethod(Ljava/lang/String;)
> ('coming from nativeMethod.myFunc');
>                 }
>
>     }-*/;
>
> }
>
> It doesn't surprise me that what I'm trying doesn't work, I'm assuming
> the 'this' I'm trying to use is in the wrong scope.  Anyone have any
> suggestions for how I can get to that java method from within a native
> method that defines a function?

It's as easy as the following ;-)

var self = this;
var myFunc = function() {
se...@com.foo.bar.MyClass::javaMethod(Ljava/lang/String;)('coming
from nativeMethod.myFunc');
};

Bryan

unread,
Nov 20, 2009, 11:07:05 AM11/20/09
to Google Web Toolkit
Nice. Thanks Thomas. Worked great. I knew it had to be something as
simple as that. Much appreciated.
>    s...@com.foo.bar.MyClass::javaMethod(Ljava/lang/String;)('coming
> from nativeMethod.myFunc');
>
> };
Reply all
Reply to author
Forward
0 new messages