Exporting Instance Methods to Hand-Written Javascript

303 views
Skip to first unread message

Geoffrey Wiseman

unread,
Apr 2, 2012, 4:26:27 PM4/2/12
to google-we...@googlegroups.com
I have a piece of GWT code that I wanted to invoke from outside handwritten JavaScript. There's an example shown here under "Calling a Java method from Handwritten JavaScript":

That example uses a static method; I can make that work, but my first instinct was that i'd rather use an instance, so using the syntax shown under JSNI invocations, I tried:
/* package */ native void exportJavascriptMethods( PreviewPane x ) /*-{
       $wnd.imageSelected =
          $entry(this@ca.cpp.spike.gwtapplet.client.PreviewPane::imageSelected(Ljava/lang/String;Ljava/lang/String;));
  }-*/;
That doesn't work. Eclipse (and the GWT compiler) complain:
"JavaScript parsing: Missing ) after argument list PreviewPane.java"

If I replace 'this' with 'x', same problem. If I take 'this' out and leave it binding to the static method:

/* package */ native void exportJavascriptMethods( PreviewPane x ) /*-{
       $wnd.imageSelected =
          $entry(@ca.cpp.spike.gwtapplet.client.PreviewPane::imageSelected(Ljava/lang/String;Ljava/lang/String;));
  }-*/;

All is well in the world.

So -- is that just not an option, exporting an instance method like that?

Alfredo Quiroga-Villamil

unread,
Apr 2, 2012, 11:34:20 PM4/2/12
to google-we...@googlegroups.com
At first glance it seems like:

"PreviewPane::imageSelected(Ljava/lang/String;Ljava/lang/String;))"

takes two arguments, in this case two Strings. The JSNI error seems to indicate that you are not passing the two arguments as per the method signature. I would think that you would end up with something like this:

PreviewPane::imageSelected(Ljava/lang/String;Ljava/lang/String;))(string1, string2);

Best regards,

Alfredo

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/mC9EADg67lcJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.



--
Alfredo Quiroga-Villamil

AOL/Yahoo/Gmail/MSN IM:  lawwton


Thomas Broyer

unread,
Apr 3, 2012, 4:00:27 AM4/3/12
to google-we...@googlegroups.com

On Monday, April 2, 2012 10:26:27 PM UTC+2, Geoffrey Wiseman wrote:
I have a piece of GWT code that I wanted to invoke from outside handwritten JavaScript. There's an example shown here under "Calling a Java method from Handwritten JavaScript":

That example uses a static method; I can make that work, but my first instinct was that i'd rather use an instance, so using the syntax shown under JSNI invocations, I tried:
/* package */ native void exportJavascriptMethods( PreviewPane x ) /*-{
       $wnd.imageSelected =
          $entry(this@ca.cpp.spike.gwtapplet.client.PreviewPane::imageSelected(Ljava/lang/String;Ljava/lang/String;));
  }-*/;
That doesn't work. Eclipse (and the GWT compiler) complain:
"JavaScript parsing: Missing ) after argument list PreviewPane.java"

You're missing the '.' before the '@', so it might be the issue (the parser chokes on the '@' and says the $entry() call is missing its closing paren, or something like that).

You'll have another issue when you fix this syntax error though: you get a reference on the method but, like in Java with java.lang.reflect.Method, the instance it's called on has to be given at call time.
You have to write:
   var that = this;
   $wnd.imageSelected = $entry(function(s1, s2) {
         return that.@ca.cpp.spike.gwtapplet.client.PreviewPane::imageSelected(Ljava/lang/String;Ljava/lang/String;)(s1, s2);
      });


Reply all
Reply to author
Forward
0 new messages