MVELExpressionEvaluator implements ExpressionEvaluator { @Override public Object evaluateLiteralExpression(String rawExpression, String className, List<String> genericClasses) { Object expressionResult = compileAndExecute(rawExpression, new HashMap<>()); Class<Object> requiredClass = loadClass(className, classLoader); if (expressionResult != null && !requiredClass.isAssignableFrom(expressionResult.getClass())) { throw new IllegalArgumentException("Cannot assign a '" + expressionResult.getClass().getCanonicalName() + "' to '" + requiredClass.getCanonicalName() + "'"); } return expressionResult; } MVELExpressionEvaluator(ClassLoader classLoader); @Override boolean evaluateUnaryExpression(String rawExpression, Object resultValue, Class<?> resultClass); @Override Object evaluateLiteralExpression(String rawExpression, String className, List<String> genericClasses); @Override String fromObjectToExpression(Object value);  }
@Test public void evaluateLiteralExpression() { assertEquals(1, evaluator.evaluateLiteralExpression(mvelExpression("1"), Integer.class.getCanonicalName(), Collections.emptyList() )); assertEquals("Value", evaluator.evaluateLiteralExpression(mvelExpression("\"Value\""), String.class.getCanonicalName(), Collections.emptyList() )); assertEquals(6, evaluator.evaluateLiteralExpression(mvelExpression("2 * 3"), Integer.class.getCanonicalName(), Collections.emptyList() )); assertEquals(14, evaluator.evaluateLiteralExpression(mvelExpression("-1 + (3 * 5)"), Integer.class.getCanonicalName(), Collections.emptyList() )); assertEquals(Arrays.asList("Jim"), evaluator.evaluateLiteralExpression(mvelExpression("[\"Jim\"]"), ArrayList.class.getCanonicalName(), Collections.emptyList() )); assertEquals(Collections.emptyList(), evaluator.evaluateLiteralExpression(mvelExpression("[]"), ArrayList.class.getCanonicalName(), Collections.emptyList() )); assertThat(evaluator.evaluateLiteralExpression(mvelExpression("\"abc..\"[2]"), Character.class.getCanonicalName(), Collections.emptyList() )) .isEqualTo('c'); assertThat(evaluator.evaluateLiteralExpression(mvelExpression("1.234B"), BigDecimal.class.getCanonicalName(), Collections.emptyList() )) .isEqualTo(new BigDecimal("1.234")); assertThat(evaluator.evaluateLiteralExpression(mvelExpression("1.234d"), Double.class.getCanonicalName(), Collections.emptyList() )) .isEqualTo(Double.valueOf("1.234")); assertEquals("Value", evaluator.evaluateLiteralExpression("# \"Value\"", String.class.getCanonicalName(), Collections.emptyList())); assertEquals(3, evaluator.evaluateLiteralExpression("# a = 1; b = 2; a+b;", Integer.class.getCanonicalName(), Collections.emptyList())); assertEquals("Test", evaluator.evaluateLiteralExpression("# a = \"Te\"; b = \"st\"; a+b;", String.class.getCanonicalName(), Collections.emptyList())); assertThatThrownBy(() -> evaluator.evaluateLiteralExpression("# a = 1 b = 2 a+b;", Integer.class.getCanonicalName(), Collections.emptyList())) .isInstanceOf(CompileException.class); assertThatThrownBy(() -> evaluator.evaluateLiteralExpression("# a = 1; a+b;", Integer.class.getCanonicalName(), Collections.emptyList())) .isInstanceOf(CompileException.class); assertEquals(Arrays.asList("Bob", "Michael"), evaluator.evaluateLiteralExpression(mvelExpression("a = \"Bob\";\n" + "test = new java.util.ArrayList();\n" + "test.add(a);\n" + "test.add(\"Michael\");\n" + "test;"), ArrayList.class.getCanonicalName(), Collections.emptyList() )); HashMap<String, String> expectedMap = new HashMap<String, String>() {{ put("Jim", "Person"); }}; assertEquals(expectedMap, evaluator.evaluateLiteralExpression(mvelExpression("[\"Jim\" : \"Person\"]"), HashMap.class.getCanonicalName(), Collections.emptyList() )); assertEquals(expectedMap, evaluator.evaluateLiteralExpression(mvelExpression("a = \"Person\";\n" + "test = new java.util.HashMap();\n" + "test.put(\"Jim\", a);\n" + "test;"), HashMap.class.getCanonicalName(), Collections.emptyList() )); assertEquals(expectedMap, evaluator.evaluateLiteralExpression( mvelExpression("a = \"Person\";test = new java.util.HashMap();test.put(\"Jim\", a);test;"), HashMap.class.getCanonicalName(), Collections.emptyList() )); assertEquals(Collections.emptyMap(), evaluator.evaluateLiteralExpression( mvelExpression("a = \"Person\";\n" + "test = new java.util.HashMap();\n" + "test.put(\"Jim\", a);\n" + "test;\n" + "test.clear();\n" + "test;"), HashMap.class.getCanonicalName(), Collections.emptyList() )); assertThatThrownBy(() -> evaluator.evaluateLiteralExpression("1+", String.class.getCanonicalName(), Collections.emptyList())) .isInstanceOf(RuntimeException.class); assertThatThrownBy(() -> evaluator.evaluateLiteralExpression(mvelExpression("1"), String.class.getCanonicalName(), Collections.emptyList() )) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Cannot assign a 'java.lang.Integer"); }
@Ignore("https: @Test public void evaluateLiteralExpression_Array() { assertThat(evaluator.evaluateLiteralExpression(mvelExpression("{\"Jim\", \"Michael\"}"), Object[].class.getCanonicalName(), Collections.emptyList() )) .isEqualTo(new String[]{"Jim", "Michael"}); assertThat(evaluator.evaluateLiteralExpression(mvelExpression("{ }"), Object[].class.getCanonicalName(), Collections.emptyList() )) .isEqualTo(new String[]{}); }