在 GraalVM CE 上运行。
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-jvmci-19.3-b05-LTS)
OpenJDK 64-Bit GraalVM CE 19.3.0 (build 11.0.5+10-jvmci-19.3-b05-LTS, mixed mode, sharing)
情况1:
import org.graalvm.polyglot.Context;
public class Test {
static class Data {
public String name = "HelloWorld";
public String getName() {
return this.name;
}
}
public static void main(String[] args) {
Context context = Context.newBuilder("js").allowHostAccess(true).build();
context.getBindings("js").putMember("d", new Data());
context.eval("js", "var x = d.name");
System.out.println(
context.getBindings("js").getMember("x").asString()
);
}
}
结果:
null
为什么?
据我所知,d
正确通过:
((Data) context.getBindings("js").getMember("d").as(Data.class)).name
返回"HelloWorld"
。
案例二:
context.eval("js", "d.getName()");
例外
Exception in thread "main" TypeError: invokeMember (getName)
on JavaObject[task.Test$Data@35a3d49f (task.Test$Data)] failed due to:
Unknown identifier: getName
但是getName
是公开的……怎么了?