0

好的家伙来自这个线程Rhino Evaluating a javascript object in Java我试图以不同的方式评估 javascript。即没有defineClass 并且它没有正确评估Person 对象。

有人可以帮我吗?

工作代码如下,

  1. 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");
    }
    
  2. 人物类

    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";
       }
    
    }
    
  3. 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

请帮忙 !

4

1 回答 1

0

按照以下步骤修复它。

  1. Javascript (TestScript.js)

    Person = com.testpkg.Person;
    MyParser = com.testpkg.MyParser;
    var myObject = Person.getInstance("myObject");
    myObject.id = 1;
    myObject.string1 = "Hello";
    myObject.string2 = "World";
    myObject.string3 = "Another Test";
    myObject.string4 = "Testing";
    try
    {
       var parser = new MyParser();
       var returnStr = parser.PrintObj(myObject);
    }
    catch (e)
    {
       print("An error occured");
    }
    
  2. 人物类

    package com.testpkg;
    public class Person extends ScriptableObject {
    
       private int id; 
       private String string1;
       private String string2;
       private String string3;
    
       public Person() {
       }
    
       public void jsConstructor() {
           System.out.println("Person: jsConstructor");
           this.id = 0;
           this.string1 = "";
           this.string2 = "";
           this.string3 = "";
       }
    
       public int jsGet_id() {
           return this.id;
       }
    
       public void jsSet_id(int value) {
           this.id = value;
       }
    
       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";
       }
    
       public static Scriptable getInstance(String objectName) throws             IllegalAccessException, InstantiationException, InvocationTargetException  {
           Context jsContext = Context.enter();
           Scriptable person = null;
           try {
               ScriptableObject rootScope = jsContext.initStandardObjects();
               ScriptableObject.defineClass(rootScope, Person.class);
               person = jsContext.newObject(rootScope, "Person", null);
               rootScope.put(objectName, rootScope, person);
           }
           finally {
               Context.exit();
           }
           return person;
       }
    }
    
  3. MyParser 类

    package com.testpkg;
    public class MyParser {    
        public MyParser() {
            System.out.println("Entered MyParser constructor");
        }
    
        public String PrintObj(ScriptableObject obj) {
            System.out.println("Printing person: " + obj);
            return null;
        }
    }
    

所以当像这样编译执行时,

java -cp js.jar;MyParser.jar org.mozilla.javascript.tools.shell.Main TestScript.js

应显示以下输出,

Person: jsConstructor
Entered MyParser constructor
Printing person: Person 1 Hello World Another Test

应该注意的是 string4 在这里被安全地忽略了!

于 2013-09-05T04:32:18.850 回答