0

I am trying to invoke a java method right from my JSNI function but for some reason it never works. What am I doing wrong here? :(

Here is my code

/**
   For UI button click method...
*/
private native void test(String param)
/*-{

var a=(function b(p)
{
 this.@com.(...).TestClass::setTest(Ljava/lang/String;)(p);

})(param);

}-*/

private void setTest(String param){Window.alert(param);}

All useful comments are appreciated

3 Answers 3

2

You need to take a reference to this outside the function block:

/**
 * For UI button click method...
 */
private native void test(String param) /*-{
    var theInstance = this;
    var a = ( function b(p) {
        theInstance.@com.(...).TestClass::setTest(Ljava/lang/String;)(p);
    })(param);
}-*/;

private void setTest(String param){
    Window.alert(param);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your usage of the this keyword may cause the problem. In your context the this keywords points to the closure

(function b(p)
{
 this.@com.(...).TestClass::setTest(Ljava/lang/String;)(p);

})(param);

Ideally it should point to the function that GWT compiles from

private native void test(String param)

this statement.

Try using this code segment (I'm not sure if I got the syntax right, verify with GWT JSNI wiki) :

private native void test(String param)
/*-{    
var a = this.@com.(...).TestClass::setTest(Ljava/lang/String;)(param);    
}-*/

BTW, Having a function whose sole purpose is to call another function is a code smell.

4 Comments

Emm I use "this" because all methods are in the same class. I get no compilation error but the Java method invokation works only in the out of the JS function block :( Maybe I do some wrong invokation then? Help me to understand please
The problem is the THIS pointer points to a different anonymous class (or prototype) if you use it inside a function block. In order to call GWT functions from javascript the THIS keyword should point to the class generated by GWT. If its too confusing try removing the THIS keyword/qualifier altogether.
no method call works within any function block :( I tried to remove "this" but it gave no effect :( So I've still being thinking of... can java method be called right from JS function block or it is useless thing?
It's not necessarily a code smell with GWT JSNI. Instead of invoking the wrapper and assigning it to a, the wrapper could instead be assigned to $wnd.test. Then test() could be invoked from pure JS, allowing external JS to call GWT methods.
0

Yes, as Zasz is pointing out, you are overcomplicating your code (expect if you really want to provide a JavaScript method, but in that case you have to do it in a complete different way...)

So I tested the code and this works:

/*    JNI Example method... */
private native void test(String param) /*-{  
    [email protected]._53_JavaScriptOverlayTypes::setTest(Ljava/lang/String;)(param); 
}-*/;

private void setTest(String param){
    Window.alert(param);
} 

1 Comment

It's greate but I need to invoke java method right from JS function block and that is the problem I guess :( Is there a optimal way to do so?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.