私はこの方法を持っています:
public void testJSNI2(){
String x = "test";
}
私はこのようにこのメソッドにアクセスできます:
helloJsni.@com.jsni.client.HelloJSNIImpl::testJSNI2(Ljava/lang/String;)
x
しかし、メソッド内で定義されている文字列にアクセスするにはどうすればよいですか?
答えは正しくありません。JavaScriptとJavaは同じようには機能しません。人はJSNIの助けを借りてjsから任意のフィールドにアクセスできます:
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
this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call instance method instanceFoo() on x
x.@com.google.gwt.examples.JSNIExample::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 = this.@com.google.gwt.examples.JSNIExample::myInstanceField;
// Write instance field on x
x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";
// Read static field (no qualifier)
@com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
}-*/;
}
これはここで見ることができます:http ://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html
x
変数はメソッドのスコープ内にあるため、Javaコードでアクセスできないのと同じように、変数にアクセスできません。