2016-06-16 121 views
0

在Rhino中,我有一個腳本化豆像下面犀牛到犀牛 - 添加屬性/豆腐可用範圍

/** 
* 
*/ 
package test.rhino; 

import java.util.Collection; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Set; 

import org.mozilla.javascript.FunctionObject; 
import org.mozilla.javascript.Scriptable; 

/** 
* @author 
* 
*/ 
public class SomeBean implements Scriptable, Map<String,Object> { 

    /** 
    * The current values for this object. 
    */ 
    private HashMap<String, Object> values = new HashMap<>(); 

    /** 
    * 
    */ 
    public SomeBean() { 
     System.out.println("SomeBean();"); 
    } 

    /* 
    * @see org.mozilla.javascript.Scriptable#getClassName() 
    */ 
    @Override 
    public String getClassName() { 
     return "SomeBean"; 
    } 

    /* 
    * @see org.mozilla.javascript.Scriptable#get(java.lang.String, 
    * org.mozilla.javascript.Scriptable) 
    */ 
    @Override 
    public Object get(String name, Scriptable start) { 

     System.out.println("Get is called."); 
     System.out.println("Called for this" + name + " and returned :" + values.get(name)); 

     return values.get(name); 
    } 

    /* 
    * @see org.mozilla.javascript.Scriptable#put(java.lang.String, 
    * org.mozilla.javascript.Scriptable, java.lang.Object) 
    */ 
    @Override 
    public void put(String name, Scriptable start, Object value) { 
     System.out.println("Put is called. Input name: " + name + "\n Input values: " + value); 

     values.put(name, value); 

    } 

    @Override 
    public Object get(int index, Scriptable start) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public boolean has(String name, Scriptable start) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean has(int index, Scriptable start) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void put(int index, Scriptable start, Object value) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void delete(String name) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void delete(int index) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public Scriptable getPrototype() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void setPrototype(Scriptable prototype) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public Scriptable getParentScope() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void setParentScope(Scriptable parent) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public Object[] getIds() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public Object getDefaultValue(Class<?> hint) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public boolean hasInstance(Scriptable instance) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public int size() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public boolean isEmpty() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean containsKey(Object key) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean containsValue(Object value) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public Object get(Object key) { 
     System.out.println("MAP -- Get is called."); 
     // TODO Auto-generated method stub 
     return values.get(key); 
    } 

    @Override 
    public Object put(String key, Object value) { 
     values.put(key, value); 
     System.out.println("MAP -- Put is called."); 
     // TODO Auto-generated method stub 
     return value; 
    } 

    @Override 
    public Object remove(Object key) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void putAll(Map<? extends String, ? extends Object> m) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void clear() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public Set<String> keySet() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public Collection<Object> values() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public Set<java.util.Map.Entry<String, Object>> entrySet() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    private static Scriptable globalPrototype; 

    public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype) { 
     System.out.println("finishInit is called."); 
     globalPrototype = prototype; 
    } 

} 

我可以檢索這個bean作爲腳本化的對象,並把它傳遞給的eval()函數像下面的編譯腳本。

String src5 = "(function(){return Name;})();\n"; 
Script sc = getCompiledScript(src5); 
SomeBean sb = new SomeBean(); 
sb.put("Name", "Matt Murdock"); 
Scriptable sp = (Scriptable) sb; 
result = script.exec(context, sp); 

結果打印馬特Murdock。我們可以直接在這裏引用屬性,因爲SomeBean實例作爲範圍傳遞給腳本執行。

我無法在Nashorn中找到相應的方法。我可以使用源代碼來獲取CompiledScript實例,但是我不能將SomeBean添加到綁定中,而無需給它一個鍵,並且我無法直接在JavaScript函數中調用其成員(名稱)。下面的代碼會拋出錯誤,這是顯而易見的,因爲我們無法訪問沒有前綴鍵的屬性。

NSomeOtherBean nsob = new NSomeOtherBean(); // extends AbstractJSObject 
nsob.setMember("Name", "Bruce Wayne"); 

Bindings binding = engine.createBindings(); 
binding.put("nsob", nsob); 
engine.setBindings(binding, ScriptContext.GLOBAL_SCOPE); 
result = engine.eval("(function(){return Name;})();\n",binding); 

Nashorn有沒有將NSomeOtherBean實例的屬性添加到作用域並在不使用前綴的情況下訪問它們的方法?比如我們如何在犀牛中做到這一點。

回答

2
+0

謝謝,但我仍然必須使用符號來訪問javascript中的屬性。爲了向範圍添加某些東西,我必須將其放在綁定對象中,並使用鍵名稱。在Rhino中,如果我們將腳本添加爲執行範圍,則可以直接通過訪問屬性。 – NishM

+0

當我們使用點運算符訪問屬性時,會調用AbstractJSObject.getMember()。 – NishM