Rhino 到 Nashorn - 添加 properties/bean 可用范围

Rhino to Nashorn - Adding properties/bean available to scope

在 Rhino 中,我有一个如下所示的 Scriptable bean

 /**
 * 
 */
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 作为 Scriptable 对象检索并将其传递给编译脚本的 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);

结果打印 Matt Murdock。我们可以在这里直接引用属性,因为 SomeBean 实例作为范围传递给脚本执行。

我无法在 Nashorn 中找到等效的方法。我可以使用源代码获取 CompiledScript 实例,但我不能在不给它一个键的情况下将 SomeBean 添加到绑定中,而且我不能在我的 JavaScript 函数中直接调用它的成员 ( Name )。下面的代码会抛出错误,这很明显,因为我们无法在没有前缀键的情况下访问属性。

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 实例的属性添加到作用域中并在不使用前缀的情况下访问它们?就像我们如何在 Rhino 中做到这一点。

Rhino Scriptable 的 Nashorn 等价物是 jdk.nashorn.api.scripting.JSObject

查看 javadoc:

https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/JSObject.html

https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/AbstractJSObject.html

示例:

http://hg.openjdk.java.net/jdk9/dev/nashorn/file/efeb16c75392/samples/jsobj_example.js

http://hg.openjdk.java.net/jdk9/dev/nashorn/file/efeb16c75392/samples/BufferArray.java

http://hg.openjdk.java.net/jdk9/dev/nashorn/file/efeb16c75392/samples/jsobject_mapreduce.js

也就是说,java.util.Map 个实例被 Nashorn 视为 "special"。即,可以像 Nashorn 中的对象属性一样访问映射键。