我正在调用一个带@JSFunction
注释的方法ScriptableObject
JavaScript 文件
Target = Packages.com.acme.rhino.Target;
function evaluate() {
var t = Target();
t.addModifier("foobar", 1);
return t;
}
Java 文件
public class Target extends ScriptableObject {
private static final long serialVersionUID = 1L;
public List<Modifier> modifiers = new LinkedList<>();
@JSConstructor
public Target() {
}
@JSFunction
public void addModifier(final String message, final int value) {
modifiers.add(new Modifier(message, value));
}
public int getValue() {
int sum = 0;
for (final Modifier modifier : modifiers) {
sum += modifier.getValue();
}
return sum;
}
@Override
public String getClassName() {
return "Target";
}
}
但我明白了
org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object.
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3693)
at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3705)
at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:976 )
at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:895 )
at org.mozilla.javascript.ScriptRuntime.toString(ScriptRuntime.java:761)
at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3774)
at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime. java:2269)
at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime. java:2251)
at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:83)
at org.mozilla.javascript.gen.script_5._c_evaluate_1(script:6)
at org.mozilla.javascript.gen.script_5.call(script)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen.script_5.call(script)
不知道从那里去哪里。当我不调用addModifier
方法时,给定的代码有效,并且notFunctionError
在堆栈跟踪中给出错误,我认为 Rhino 不会将给定的方法解释为 JavaScript 函数。
- OSX 10.8.2
- 爪哇 7
- 犀牛1.7R4