0

我在一个包中有一个数学课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);
4

1 回答 1

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

于 2014-06-24T10:59:57.063 回答