我有一个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'值'添加和访问。这样我还可以为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实现这一目标?我知道我的bean实例可以使用公共类变量,但这并没有给我提供与上面显示的相同的功能。我也不想直接访问HashMap。
感谢。
答案 0 :(得分:1)
经过大量挖掘后,我能够通过从'AbstractJSObject'类扩展我的bean来解决这个问题。这个类有代理get,set,并且当我们尝试使用点运算符访问/更改对象属性时会调用这些方法。
更改后的课程如下所示。
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);
}
}