我正在尋找覆蓋從犀牛的JavaScript Java方法。這裏是我的代碼:mozilla犀牛覆蓋從javascript的對象方法
public class CustomClass {
public String s="some string";
public void doSomething(){}
}
然後:
Object wrappedOut = Context.javaToJS(new CustomClass(), scope);
ScriptableObject.putProperty(scope, "CustomClass", wrappedOut);
String s="CustomClass.s='aaa'; \n CustomClass.doSomething=function(args){};";
Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
因此,可以將值改爲CustomClass.s而不是方法DoSomething的
我收到以下錯誤:
org.mozilla.javascript.EvaluatorException: Java method "doSomething" cannot be assigned to. (#2)
UPDATE 正如意見中的要求,這裏是我完整的源代碼:
CustomClass.java
public class CustomClass {
public String s="Do something now!";
public void doSomething(Object ... v){
System.out.println("do something");
}
}
Scripting.java
import org.mozilla.javascript.*;
public class Scripting {
public static void main(String args[])
{
Context cx = Context.enter();
try {
Scriptable scope = cx.initStandardObjects();
Object wrappedOut = Context.javaToJS(new CustomClass(), scope);
ScriptableObject.putProperty(scope, "CustomClass", wrappedOut);
String s="CustomClass.doSomething=function(args){};";
Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
System.err.println(Context.toString(result));
}catch(WrappedException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}finally {
// Exit from the context.
Context.exit();
}
}
}
你在哪裏重寫該方法?我在代碼中看不到任何繼承。 – kaysush
@SushilKumar我想重寫從JavaScript的方法。請參閱我的評估字符串代碼:CustomClass。doSomething的函數=(參數){}; –
我無法運行該代碼。請發帖[SSCCE](http://sscce.org/) – linski