尝试访问类成员时出错(使用Rhino) [英] Error trying to access class members (using Rhino)
问题描述
尝试使用Rhino创建一个Java / JS链接,其中包含两个非常简单的对象,一个具有第二个类的实例作为一个成员。
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:找不到对象的默认值
。
org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object.
问题似乎是从第二个对象中访问成员a。我也试过这样的getter:
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);
}
但我得到同样的错误。
new A()。doSmth();工作正常,并输出我正在做某事
new B()。a.doSmth();提出错误
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?
谢谢!
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();
}
}
}
推荐答案
这似乎有效:
- 制作上下文和全局范围的私有静态变量。
- 为A类添加了jsConstructor
- 在B类的jsConstructor中,在代码中创建了一个javascript对象。
- 使用Context.toObject(a,this);返回类型为ssptable in jsGet_a()
-
最后,将cx分配给输入的Context并将范围分配给全局范围。
- Made the context and the global scope private static variables.
- Added jsConstructor for the A class
- In the jsConstructor for the B class, created a javascript object in code.
- Used the Context.toObject(a, this); with return type of Scriptable in jsGet_a()
Finally, assigned the cx to the Context that was entered and scope to the global scope.
public class Test
{
private static Context cx;
private static ScriptableObject scope;
public static class A extends ScriptableObject implements Scriptable {
public A() {
}
public void jsConstructor() {
}
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() {
Scriptable scriptable = cx.newObject(scope, "A");
this.put("a", this, scriptable);
}
public Scriptable jsGet_a() {
return Context.toObject(a, this);
}
}
public static void main(String[] args) {
try {
cx = Context.enter();
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();
}
}
}
这篇关于尝试访问类成员时出错(使用Rhino)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!