从 JSNI 调用 Java 方法
Calling Java method from JSNI
我有一个class,例如:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void sayName() {
System.out.println(name);
}
}
如果我这样调用方法(或者我的无知或错误的地方),它会起作用吗:
public native void someMethod (Person person) /*-{
person.sayName();
}-*/;
package org.example.foo;
public class Flipper {
public native void flipName(String name) /*-{
var re = /(\w+)\s(\w+)/;
var s = name.replace(re, ', ');
this.@org.example.foo.Flipper::onFlip(Ljava/lang/String;)(s);
}-*/;
private void onFlip(String flippedName) {
// do something useful with the flipped name
}
}
来自 Accessing Java Methods and Fields from JavaScript 文档:
语法是:
[instance-expr.]@class-name::method-name(param-signature)(arguments)
instance-expr. : must be present when calling an instance method and must be absent when calling a static method
class-name : is the fully-qualified name of the class in which the method is declared (or a subclass thereof)
param-signature : is the internal Java method signature as specified at JNI Type Signatures but without the trailing signature of
the method return type since it is not needed to choose the overload
arguments : is the actual argument list to pass to the called method
这里是 JNI 类型签名:
Type Signature Java Type
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L fully-qualified-class ; fully-qualified-class
[ type type[]
( arg-types ) ret-type method type
For example, the Java method:
long f (int n, String s, int[] arr);
has the following type signature:
(ILjava/lang/String;[I)J
在你的情况下(没有参数)它将是:
public native void someMethod (Person person) /*-{
person.@your.package.name.client.Person::sayName()();
}-*/;
用真实的包名替换your.package.name
。
我有一个class,例如:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void sayName() {
System.out.println(name);
}
}
如果我这样调用方法(或者我的无知或错误的地方),它会起作用吗:
public native void someMethod (Person person) /*-{
person.sayName();
}-*/;
package org.example.foo;
public class Flipper {
public native void flipName(String name) /*-{
var re = /(\w+)\s(\w+)/;
var s = name.replace(re, ', ');
this.@org.example.foo.Flipper::onFlip(Ljava/lang/String;)(s);
}-*/;
private void onFlip(String flippedName) {
// do something useful with the flipped name
}
}
来自 Accessing Java Methods and Fields from JavaScript 文档:
语法是:
[instance-expr.]@class-name::method-name(param-signature)(arguments)
instance-expr. : must be present when calling an instance method and must be absent when calling a static method
class-name : is the fully-qualified name of the class in which the method is declared (or a subclass thereof)
param-signature : is the internal Java method signature as specified at JNI Type Signatures but without the trailing signature of the method return type since it is not needed to choose the overload
arguments : is the actual argument list to pass to the called method
这里是 JNI 类型签名:
Type Signature Java Type
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L fully-qualified-class ; fully-qualified-class
[ type type[]
( arg-types ) ret-type method type
For example, the Java method:
long f (int n, String s, int[] arr);
has the following type signature:
(ILjava/lang/String;[I)J
在你的情况下(没有参数)它将是:
public native void someMethod (Person person) /*-{
person.@your.package.name.client.Person::sayName()();
}-*/;
用真实的包名替换your.package.name
。