@Override public Object compile(String expression, Map<String, Object> parameters) { logger.debug("About to compile mvel expression {}", expression); ClassLoader classLoader = (ClassLoader) parameters.get("classloader"); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(classLoader); ParserContext context = new ParserContext(config); if (parameters != null) { @SuppressWarnings("unchecked") Set<String> imports = (Set<String>)parameters.get("imports"); if (imports != null) { for(String clazz : imports) { try { Class<?> cl = Class.forName(clazz, true, classLoader); context.addImport(cl.getSimpleName(), cl); } catch (ClassNotFoundException e) { logger.warn("Unable to load class {} due to {}", clazz, e.getException()); }; } } } return MVEL.compileExpression(expression, context); }
@Override public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) { Object instance = null; InternalRuntimeManager manager = null; if (contextParams.containsKey("runtimeManager")) { manager = (InternalRuntimeManager) contextParams.get("runtimeManager"); instance = manager.getCacheManager().get(model.getIdentifier()); if (instance != null) { return instance; } } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(cl); ParserContext ctx = new ParserContext(config); if (contextParams != null) { for (Map.Entry<String, Object> entry : contextParams.entrySet()) { ctx.addVariable(entry.getKey(), entry.getValue().getClass()); } } Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx); instance = MVELSafeHelper.getEvaluator().executeExpression( compiledExpression, contextParams ); if (manager != null && instance instanceof Cacheable) { manager.getCacheManager().add(model.getIdentifier(), instance); } return instance; }
public static Object eval(String str, Map<String, Object> vars) { ParserConfiguration pconf = new ParserConfiguration(); pconf.addPackageImport("org.jbpm.services.task"); // pconf.addPackageImport("org.jbpm.services.task.service"); pconf.addPackageImport("org.jbpm.services.task.query"); pconf.addPackageImport("java.util"); for(String entry : getInputs().keySet()){ pconf.addImport(entry, getInputs().get(entry)); } ParserContext context = new ParserContext(pconf); Serializable s = MVEL.compileExpression(str.trim(), context); if( vars != null ) { return MVELSafeHelper.getEvaluator().executeExpression(s, vars); } else { return MVELSafeHelper.getEvaluator().executeExpression(s); } } public static String toString(Reader reader) throws IOException {
public static Object eval(String str, Map<String, Object> vars) { ParserConfiguration pconf = new ParserConfiguration(); pconf.addPackageImport("org.kie.internal.task.api.model"); pconf.addPackageImport("org.jbpm.services.task"); pconf.addPackageImport("org.jbpm.services.task.impl.model"); pconf.addPackageImport("org.jbpm.services.task.query"); pconf.addPackageImport("org.jbpm.services.task.internals.lifecycle"); pconf.addImport(Status.class); pconf.addImport(Allowed.class); pconf.addPackageImport("java.util"); ParserContext context = new ParserContext(pconf); Serializable s = MVEL.compileExpression(str.trim(), context); if (vars != null) { return MVELSafeHelper.getEvaluator().executeExpression(s, vars); } else { return MVELSafeHelper.getEvaluator().executeExpression(s); } }
@Override protected ParserConfiguration getParserConfiguration(InternalWorkingMemory workingMemory) { ParserConfiguration parserConfiguration = new ParserConfiguration(); parserConfiguration.addImport(Cheese.class); return parserConfiguration; }
public ParserContext(Map<String, Object> imports, Map<String, Interceptor> interceptors, String sourceFile) { this.sourceFile = sourceFile; this.parserConfiguration = new ParserConfiguration(imports, interceptors); }
public void testForLoopWithVar() { String str = "int height = 100; int j = 0; for (i = 0; i < height; i++) {j++ }; return j;"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); Map vars = new HashMap(); assertEquals(new Integer(100), MVEL.executeExpression(stmt, vars)); }
public void testPrimitiveSubtyping() { ParserConfiguration conf = new ParserConfiguration(); ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); BigDecimal result = (BigDecimal)MVEL.executeExpression(MVEL.compileExpression("java.math.BigDecimal.valueOf(100)", pctx), new HashMap()); assertEquals("100", result.toString()); }
public void testBigDecimalOutput() { String str = "import java.math.BigDecimal; BigDecimal test = new BigDecimal(\"50000\"); System.out.println(test / new BigDecimal(\"1.13\"));"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.setStrictTypeEnforcement(true); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); MVEL.executeExpression(stmt, new HashMap()); }
public void testMethodReturningPrimitiveTypeAnalysis() { String str = "value"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.addInput("this", MyObj.class); pctx.setStrongTyping(true); Class<?> returnType = MVEL.analyze(str, pctx); assertEquals(long.class, returnType); }
public void testGetCorrectInputs() { String str = "total = total + $cheese.price"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("total", int.class); pctx.addInput("$cheese", Cheese.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty()); }
public void testMapAccessWithNestedMethodCall() { String str = "map[aMethod(1)] == \"one\""; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("this", POJO.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); POJO ctx = new POJO(); ctx.getMap().put("1", "one"); Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx); assertTrue(result); }
public void testMapAccessWithNestedProperty() { String str = "map[key] == \"one\""; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("this", POJO.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); POJO ctx = new POJO(); ctx.getMap().put("1", "one"); Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx); assertTrue(result); }
public void testWithInsideBlock() { String str = "Foo f = new Foo(); with(f) { setBoolTest( true ) }; f.isBoolTest()"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("this", Bar.class); pctx.addImport(Foo.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); Bar ctx = new Bar(); Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx, new HashMap()); assertTrue(result); }
public void testContextObjMethodCall() { String str = "getName() == \"bob\""; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("this", Bar.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); Bar ctx = new Bar(); ctx.setName("bob"); Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx); assertTrue(result); }
public void testUnaryNegative() { ParserConfiguration conf = new ParserConfiguration(); ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("value", int.class); Map vars = new HashMap() {{ put("value", 42); }}; assertEquals(-42, MVEL.executeExpression(MVEL.compileExpression("-value", pctx), vars)); }
public void testFloat() { ParserConfiguration conf = new ParserConfiguration(); conf.addImport( FactWithFloat.class ); ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("this", FactWithFloat.class); boolean result = ( Boolean ) MVEL.executeExpression(MVEL.compileExpression("floatObjectValue == 15.1", pctx), new FactWithFloat(15.1f)); assertTrue( result ); } }
public void testInstanceofOnInnerClass() { ParserConfiguration conf = new ParserConfiguration(); conf.addImport(ARef.class); ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("value", Object.class); Map vars = new HashMap() {{ put("value", new ARef()); }}; assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof ARef", pctx), vars)); assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof " + ARef.class.getCanonicalName(), pctx), vars)); }
public void testInvokeVarargConstructor() { ParserConfiguration conf = new ParserConfiguration(); conf.addImport( Thingy.class ); ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("name", String.class); Map vars = new HashMap() {{ put("name", "test"); }}; Thingy result = (Thingy) MVEL.executeExpression(MVEL.compileExpression("new Thingy(name)", pctx), vars); assertEquals( "test", result.getName() ); }
public void testMultiplyIntByDouble() { ParserConfiguration conf = new ParserConfiguration(); ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("i", Integer.class); pctx.addInput("d", Double.class); Map vars = new HashMap() {{ put("i", 10); put("d", 0.3); }}; assertEquals(3.0, MVEL.executeExpression(MVEL.compileExpression("i*d", pctx), vars)); assertEquals(3.0, MVEL.executeExpression(MVEL.compileExpression("i*0.3", pctx), vars)); }