java 代码
- 1. import org.apache.commons.jexl.Expression;
- 2. import org.apache.commons.jexl.ExpressionFactory;
- 3. import org.apache.commons.jexl.JexlContext;
- 4. import org.apache.commons.jexl.JexlHelper;
- 5.
- 6. import junit.framework.TestCase;
- 7.
- 8. public class JexlTest extends TestCase {
- 9. public void test() throws Exception {
- 10.
- 11. //Create an expression object
- 12. String jexlExp = "9*(1+2)";
- 13. Expression e = ExpressionFactory.createExpression(jexlExp);
- 14.
- 15. // Create a context
- 16. JexlContext jc = JexlHelper.createContext();
- 17.
- 18. // Now evaluate the expression, getting the result
- 19. Object result = e.evaluate(jc);
- 20.
- 21. System.out.println("result:" + result);
- 22. System.out.println("result type:" + result.getClass().getName());
- 23.
- 24. assertEquals(((Long) result).intValue(), 27);
- 25. }
- 26. }