Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT:How to access java variable from JSNI

Tags:

gwt

jsni

I have this method:

public void testJSNI2(){
  String x = "test";
}

I can access this method like this:

[email protected]::testJSNI2(Ljava/lang/String;)

But how can I access the String x, which is defined inside a method?

like image 959
user1226162 Avatar asked May 09 '26 08:05

user1226162


2 Answers

The answer is not correct. JavaScript and Java dose not act the same. Person can access any field from js with the help of JSNI:

public class JSNIExample {

  String myInstanceField;
  static int myStaticField;

  void instanceFoo(String s) {
    // use s
  }

  static void staticFoo(String s) {
    // use s
  }

  public native void bar(JSNIExample x, String s) /*-{
    // Call instance method instanceFoo() on this
    [email protected]::instanceFoo(Ljava/lang/String;)(s);

    // Call instance method instanceFoo() on x
    [email protected]::instanceFoo(Ljava/lang/String;)(s);

    // Call static method staticFoo()
    @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);

    // Read instance field on this
    var val = [email protected]::myInstanceField;

    // Write instance field on x
    [email protected]::myInstanceField = val + " and stuff";

    // Read static field (no qualifier)
    @com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
  }-*/;

}

You can see this here: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

like image 56
Blaze Avatar answered May 12 '26 11:05

Blaze


You can't access the variable x because it is in the scope of the method, the same way you wouldn't be able to access it in Java code.

like image 31
Jason Hall Avatar answered May 12 '26 09:05

Jason Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!