0

I'm having trouble with a JSNI calling a Java method:

public static native void update() /*-{
    this.@app.client.local.MyPage::populate()();
}-*/;

No errors, its just that the method is not getting triggered, the Java method, populate() which shows a alert box when called, is not firing.

MyPage however is a Errai page annotated with @Page

quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

3

I think you are miss-understanding the meaning of the instance preceding the @ symbol in JSNI.

You are calling the method populate() of the instance this, but your update() method is static.

You have either define populate() as static and call it in a static way.

package app.client.local;
class MyPage {

  public static native void update() /*-{
    @app.client.local.MyPage::populate()();
  }-*/;

  public static void populate() {
  }
}

Or you can pass the instance of the class having the method as argument to your jsni code:

package app.client.local;

class MyClass {
  public void populate() {
  }
}

class MyPage {
  public static native void update(MyClass instance) /*-{
    instance.@app.client.local.MyClass::populate()();
  }-*/;
}
Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • I'm not sure if I understand your answer correctly, you mean I have to make it either static or pass the MyClass instance? I'm not sure I can do both 1) make it static, can't do this with the method 2) pass the MyClass instance, can do this also, the JNSI method update() is part of a Utility class – quarks Jun 19 '13 at 12:21
  • Actually the point by which I want to call java method from JSNI is to be able to trigger the populate() method anywhere in the app... – quarks Jun 19 '13 at 12:22
  • What I say is that if the method is static, you don't need an instance of the class, so the call is like the first block. If the method is not static, you need the instance, so you have to pass it as a parameter, or you need a js variable pointing to the instance (ie: you could assign the instance to a window variable) – Manolo Carrasco Moñino Jun 19 '13 at 12:33
  • To call a static method anywhere in the app you dont need JSNI. If it is an instance method, save the instance reference anywhere in your app (maybe an abstrace class with static properties). Another option is save the instance in a window var, but is this what you want? – Manolo Carrasco Moñino Jun 19 '13 at 12:36
  • @manola, the MyClass populate() method is not static method, so I can't call it anywhere. Also I need to call populate() java method also inside Javscript code in JSNI... like during Jquery events. – quarks Jun 19 '13 at 16:12
  • This is a different issue because you have to export your GWT methods/classes to JS, could you open a new Query with a full description about what you want? I will answer it. – Manolo Carrasco Moñino Jun 19 '13 at 16:21
  • Your answer is right for the question, however I decided to use Gwt EventBus for this purpose since its more natural for subscribe/trigger methods anywhere in the app... – quarks Jun 22 '13 at 01:31
  • Yep `EventBus` is the way to decouple different pieces of your app. You cannot call the eventbus from jquery events though. I dont know why are you mixing jquery + gwt, maybe you could consider using gwtquery. – Manolo Carrasco Moñino Jun 22 '13 at 10:21