将 Rhino Scriptable bean 迁移到 Nashorn

Migrating a Rhino Scriptable bean to Nashorn

我有一个 Scriptable bean,如下所示

package test.rhino;

import java.util.HashMap;

import org.mozilla.javascript.Scriptable;

public class SomeBean implements Scriptable {

    /**
     * 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;
    }

}

在 Rhino 中,使用 javascript 我可以访问底层 HashMap 中的键作为 bean 的属性。

var bean = new SomeBean();"
                + "bean.nomen = 'John Doe';\n"
                + "bean.nomen2 = bean.nomen + ' is cool';

输出显示 get 和 put 被称为从 HashMap 'values' 添加和访问的键和值。这样我还可以向 get() 和 put() 方法定义添加额外的功能。

在将此 bean 移植到 Nashorn 时,我找不到具有相同功能的方法。 Nashorn 确实在 Javascript 中公开了 HashMap 并允许我们添加条目,但对我来说,应该可以通过公共 setter 或 getter 从 bean 实例访问此行为,以便我可以做更多东西,同时从 HashMap 添加和检索值。

所以它应该像下面这样工作:-

Assume bean = new SomeBean(); 
bean.name = 'John Doe' // Adds name and John Doe to the HashMap
print(bean.name) // Retrieves John Doe. 

有没有办法在 Nashorn 中完成此操作?。我知道 public class 变量可用于我的 bean 实例,但这并没有给我提供与上面所示相同的功能。我也不想直接访问 HashMap。

谢谢。

经过大量挖掘,我能够通过从 'AbstractJSObject' class 扩展我的 bean 来解决这个问题。此 class 具有代理获取、设置并具有当我们尝试使用点运算符 access/change 对象属性时将被调用的方法。

更改后的 class 如下所示。

package test.nashorn;

import java.util.HashMap;

import jdk.nashorn.api.scripting.AbstractJSObject;

public class NSomeOtherBean extends AbstractJSObject {

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

    public NSomeOtherBean() {
        System.out.println("Constructor called.");
    }

    // do you have a property of that given name?
    @Override
    public boolean hasMember(String name) {
        return has(name);
    }

    // get the value of that named property
    @Override
    public Object getMember(String name) {

        return get(name);

    }

    // get the value of that named property
    @Override
    public void setMember(String name,Object value) {

         put(name,value);

    }

    public Object get(String name) {

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

        return values.get(name);
    }

    public void put(String name, Object value) {
        System.out.println("JAVA Put is called. Input name: " + name + "\n Input values: " + value);

        values.put(name, value);

    }

    public boolean has(String name) {
        System.out.println("JAVA Has is called. Input name: " + name);

        return values.containsKey(name);

    }
}