好的家伙来自这个线程Rhino Evaluating a javascript object in Java我试图以不同的方式评估 javascript。即没有defineClass 并且它没有正确评估Person 对象。
有人可以帮我吗?
工作代码如下,
Javascript
importPackage(Packages.com.testpkg); defineClass("com.testpkg.Person"); var parser = new MyParser(); var myObject = new Person(); myObject.string1 = "Hello"; myObject.string2 = "World"; myObject.id = 1; myObject.string4 = "Testing"; myObject.string3 = "Another Test"; try { var returnStr = parser.PrintObj(myObject); } catch (e) { print("An error occured"); }
人物类
public class Person extends ScriptableObject { private int id; private String string1; private String string2; private String string3; public Person() { System.out.println("Person: Person Constructor"); } public void jsConstructor() { System.out.println("Person: jsConstructor"); this.string1 = ""; this.string2 = ""; this.id = 0; } public int getID() { return this.id; } public void jsSet_id(int value) { this.id = value; } public int jsGet_id() { return this.id; } public String jsGet_string1() { return this.string1; } public void jsSet_string1(String value) { this.string1 = value; } public String jsGet_string2() { return this.string2; } public void jsSet_string2(String value) { this.string2 = value; } public String jsGet_string3() { return this.string3; } public void jsSet_string3(String value) { this.string3 = value; } @Override public String toString() { return id + " " + string1 + " " + string2 + " " + string3; } @Override public String getClassName() { return "Person"; } }
MyParser 类
public class MyParser { public MyParser() { System.out.println("Entered constructor"); } public String PrintObj(ScriptableObject obj) { Person pObj = (Person)Context.jsToJava(obj, Person.class); System.out.println("Printing p: " + pObj); return null; } }
输出是,
Entered constructor
Person: Person Constructor
Person: jsConstructor
Printing p: 1 Hello World Another Test
现在问题是当我摆脱defineClass时。在使用(Rhapsody 健康产品)的应用程序中实现的 Rhino 版本没有定义此方法。因此我不能使用这个。我使用了另一种方法来加载类 Person。如果我摆脱 defineClass 的输出将是,
Entered constructor
Person: Person Constructor
Printing p: 0 null null null
请帮忙 !