0
 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?

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);
       }
    }-*/;
}

2 Comments

Sorry,I can't understand your code.In my code,"OnMouseMove(e)" locates within a JSNI method,and I want to call java method from this:OnMouseMove(e)
@AlexLuya added a more precise example. Also, as Thomas Broyer pointed out, you're probably expecting this to be something it's not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.