2013-01-04 45 views
2

我正在尋找覆蓋從犀牛的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(); 
     } 
    } 
} 
+0

你在哪裏重寫該方法?我在代碼中看不到任何繼承。 – kaysush

+0

@SushilKumar我想重寫從JavaScript的方法。請參閱我的評估字符串代碼:CustomClass。doSomething的函數=(參數){}; –

+0

我無法運行該代碼。請發帖[SSCCE](http://sscce.org/) – linski

回答

1

運行此代碼時,犀牛不調用Java doSomething方法。相反,它試圖調用它的javascript對應文件(缺失),然後拋出一個異常。您的CustomClass是純Java類,沒有JavaScript對應。這條線

String s="CustomClass.doSomething=function(args){};"; 

:如果更換此行源

String s = "CustomClass"; 

,並評估其作爲javasript命令,它會爲[email protected]輸出。這通常意味着它是類CustomClass的一個對象的某個(「15e0be38」)實例。

從apidoc爲方法ScriptableObject.defineClass(Scriptable scope, java.lang.Class clazz)


Defines JavaScript objects from a Java class that implements Scriptable.
(...)
Next, all methods are scanned for special prefixes that indicate that they have special meaning for defining JavaScript objects. These special prefixes are

jsFunction_ for a JavaScript function
jsStaticFunction_ for a JavaScript function that is a property of the constructor
jsGet_ for a getter of a JavaScript property
jsSet_ for a setter of a JavaScript property
jsConstructor for a JavaScript function that is the constructor

If the method's name begins with "jsFunction_", a JavaScript function is created with a name formed from the rest of the Java method name following "jsFunction_". So a Java method named "jsFunction_foo" will define a JavaScript method "foo". Calling this JavaScript function will cause the Java method to be called. The parameters of the method must be of number and types as defined by the FunctionObject class. The JavaScript function is then added as a property of the prototype.


的JavaScript print方法((在 org.mozilla.javascript.tools.shell.Global類中定義))接口到Java System.out.println方法。當運行

public class CustomClass { 

public static class _CustomClass extends ScriptableObject { 

    @Override 
    public String getClassName() { 
     return "CustomClass"; 
    } 

    public int jsFunction_method() { 
     System.out.println("from java method"); 
     return 2; 
    } 
} 

public static void main(String[] args) { 
    Context context = Context.enter(); 
    Global global = new Global(context); 
    try {    
     //ScriptableObject.defineClass(global, CustomClass.class); 
     ScriptableObject.defineClass(global, _CustomClass.class);    
     String script = "myInstance = new CustomClass();myInstance+' ';"; 
     System.out.println(context.evaluateString(global, script, "", 1, null)); 
     script = "myInstance.method+' '+myInstance.method()"; 
     System.out.println(context.evaluateString(global, script, "script", 1, null)); 
     script = "CustomClass.prototype.method=function(){print('overriden from javascript method');return 3;}"; 
     context.evaluateString(global, script, "", 1, null); 
     script = "myInstance.method+' '+myInstance.method();"; 
     System.out.println(context.evaluateString(global, script, "script", 1, null)); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
     Context.exit(); 
    } 
    } 
    } 

它輸出:

[object CustomClass] 
from java method 
function method() { 
[native code, arity=0] 
} 
2 

overriden from javascript method 
function() { 
    print("overriden from javascript method"); 
    return 3; 
} 
3 
  • 注意,如果此相同的JS代碼在Chrome瀏覽器將運行不同 - 回報2的兩倍。
  • 如果使用了外部類(如在註釋中)那樣會出現通用的相關編譯時錯誤。此代碼是使用Rhino 1.7r2編寫的。