尝试访问类成员时出错(使用Rhino)
尝试使用 Rhino 与两个非常简单的对象创建 Java/JS 链接,其中一个对象具有第二个类的实例作为一个成员。
运行下面的代码会出现以下错误:
org.mozilla.javascript.EcmaError: TypeError: Cannot find default value 对于对象。
问题似乎是从第二个对象中访问成员“a”。我也尝试过这样的吸气剂:
public Object jsGet_a() {
return Context.toObject(a, this);
}
但我得到了同样的错误。
new A().doSmth();工作正常,并输出“我正在做某事” 新 B().a.doSmth();引发错误
任何人都可以帮助我找到可能的解决方案吗?
谢谢你!
public class Test {
public static class A extends ScriptableObject implements Scriptable {
public A() {
};
public String getClassName() {
return "A";
}
public void jsFunction_doSmth() {
System.out.println("I'm doing something");
};
}
public static class B extends ScriptableObject implements Scriptable {
private A a = new A();
public B() {
};
public String getClassName() {
return "B";
}
public void jsConstructor() {
}
public A jsGet_a() {
return a;
}
}
public static void main(String[] args) {
try {
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects(null, true);
ScriptableObject.defineClass(scope, A.class);
ScriptableObject.defineClass(scope, B.class);
cx.compileString("" +
"new A().doSmth();" +
"new B().a.doSmth();" +
"", "", 1, null).exec(cx, scope);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Trying to create a Java/JS link using Rhino with two very simple objects, one having as one member an instance of the second class.
Running the code below gives the following error:
org.mozilla.javascript.EcmaError: TypeError: Cannot find default value
for object.
The problem seems to be accessing the member "a" from within second object. I've also tried with a getter like this:
public Object jsGet_a() {
return Context.toObject(a, this);
}
but i get the same error.
new A().doSmth(); is working fine, and outputs "I'm doing something"
new B().a.doSmth(); raises the error
Can anyone help me with a possible solution for this?
Thank you!
public class Test {
public static class A extends ScriptableObject implements Scriptable {
public A() {
};
public String getClassName() {
return "A";
}
public void jsFunction_doSmth() {
System.out.println("I'm doing something");
};
}
public static class B extends ScriptableObject implements Scriptable {
private A a = new A();
public B() {
};
public String getClassName() {
return "B";
}
public void jsConstructor() {
}
public A jsGet_a() {
return a;
}
}
public static void main(String[] args) {
try {
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects(null, true);
ScriptableObject.defineClass(scope, A.class);
ScriptableObject.defineClass(scope, B.class);
cx.compileString("" +
"new A().doSmth();" +
"new B().a.doSmth();" +
"", "", 1, null).exec(cx, scope);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎有效:
最后,将 cx 分配给输入的 Context,并将范围分配给全局范围。
This seems to work:
Finally, assigned the cx to the Context that was entered and scope to the global scope.
将无法工作,因为
a
是私有的。似乎应该有效。
will not work as
a
is private.seems like it should work.
根据
“ rel="nofollow">API 文档,您可以在 java 对象中 。我的猜测是你在上面的反射转换是错误的。
According to the API docs, you could use the method
in your java object. My guess is that you got the reflection conversion wrong in the above.