在MVEL中导入此类,并为其属性赋值并访问其方法

时间:2013-10-25 09:01:06

标签: mvel

我在包mypackage中有一个数学课。 现在我想在MVEL中导入这个类,并为其属性赋值并访问其方法。 我写了以下代码,但它给出了错误

Exception in thread "main" [Error: unknown class or illegal statement: 
                      ^

代码是

ParserContext context = new ParserContext();
context.addImport("math",mypackage.MyMaths.class);//MyMaths.class is public
context.addInput("obj", mypackage.MyMaths.class);

String expression1 = "obj.a == 20";//a is public property

Serializable compiled1 = MVEL.compileExpression(expression1,context);

MVEL.executeExpression(compiled1);

1 个答案:

答案 0 :(得分:0)

请尝试以下方法。

 public static void main(String[] args) {

        Map map = new HashMap();
        MyMaths mayMaths = new MyMaths();
        map.put("obj", mayMaths);

        String expression1 = "obj.a = 20";

        Serializable compiled1 = MVEL.compileExpression(expression1);

        MVEL.executeExpression(compiled1, map);

        System.out.println(mayMaths.getA());
    }

输出 - 20