2013-10-25 34 views
0

我在包mypackage中有一個數學課。 現在我想在MVEL中導入這個類,併爲其屬性賦值並訪問它的方法。 我寫了下面的代碼,但它給錯誤的在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); 

回答

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

相關問題