Alex Luya
Alex Luya

Reputation: 9912

How to call java method from javascript method that located within another jsni method

 public class A{
    private void javaMethod(int a,int b){}

    private native void init()/*-{
            function OnMouseMove(e) {
             //blow calling doesn't work
              this.@p::javaMethod(Ljava/...teger;Ljava.../Integer;)(intVal,intVal);
            }
    }-*/;
  }

As described above,how to make that invoking work?

Upvotes: 0

Views: 1220

Answers (2)

Chris Cashwell
Chris Cashwell

Reputation: 22859

You're doing two things wrong:

  • You're not defining the class name after @p, (assuming @p is actually just a shortened version of the real package's name);

  • You're attempting to pass java.lang.Integer in place of int. You should be saying (II) as the types, as described here.

Your code should look more like this:

package com.my.package;

public class ClassA {
    private static void javaMethod(int a, int b) { ... }
    public static native void init() /*-{
       $wnd.javaMethod = function(a, b) {
          return @com.my.package.ClassA::javaMethod(II)(a,b);
       }
       function OnMouseMove(e) {
          $wnd.javaMethod(a,b);
       }
    }-*/;
}

Upvotes: 1

Thomas Broyer
Thomas Broyer

Reputation: 64541

Answered on the Google Group: https://groups.google.com/d/msg/google-web-toolkit/qE2-L4u_t4s/YqjOu-bUfsAJ

Copied here for reference and convenience:

First, int is not java.lang.Integer, so your method signature in JSNI is wrong; it should read javaMethod(II). (I suppose the @p:: while javaMethod is defined in class A is over-simplification in your question, but is OK in your code)

You'll also probably have a problem with this, that might not be what you think it is. A common pattern is to assign the current object (this, at the time) to a variable that you'll reference from your closure:

var that = this;
…
function OnMouseMove(e) {
   [email protected]::javaMethod(II)(intVal, intVal);
}

Upvotes: 1

Related Questions