@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); }
ParserContext context = new ParserContext(); context.addImport("MVEL", MVEL.class); context.addImport("MvelHelper", MvelHelper.class); exp = MVEL.compileExpression(modifiedRule, context); expressionCache.put(rule, exp); Object test = MVEL.executeExpression(exp, mvelParameters); if (test == null) {
@Test public void test1() { final ParserContext pc = new ParserContext(); pc.addInput("x", String.class); pc.setStrongTyping(true); final Object o = MVEL.compileExpression("x.startsWith('d')", pc); final Map vars = new HashMap(); vars.put("x", "d"); MVEL.executeExpression(o, vars); System.out.println(o); }
private ParserContext ctxJIRA170(boolean strictTypeEnforcement, boolean strongTyping) { ParserContext ctx = new ParserContext(); // ctx.setStrictTypeEnforcement(strictTypeEnforcement); ctx.setStrongTyping(strongTyping); ctx.addInput("x", Collection.class, new Class[]{Integer.class}); ctx.addInput("y", Integer.class); return ctx; }
public void testShouldFail7() { try { ParserContext pctx = new ParserContext(); pctx.setStrongTyping(true); MVEL.compileExpression("String x = 'foo'; int y = 2; new int[] { x, y }", pctx); } catch (Exception e) { // e.printStackTrace(); return; } shouldThrowException(); }
public void testDynamicImportsWithNullConstructorParamWithStrongType() { ParserContext ctx = new ParserContext(); ctx.setStrongTyping(true); ctx.addPackageImport("org.mvel2.tests.core.res"); ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", null)", ctx); Cheesery p1 = new Cheesery("bobbo", null); Cheesery p2 = (Cheesery) executeExpression(compiler.compile(), new DefaultLocalVariableResolverFactory()); assertEquals(p1, p2); }
public void testArrayLength() { ParserContext context = new ParserContext(); context.setStrongTyping(true); context.addInput("x", String[].class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("x.length", context); }
ParserContext context = new ParserContext(); Serializable domainExp1 = MVEL.compileExpression("offer.type.equals(OfferType.FULFILLMENT_GROUP) and (($ in order.fulfillmentGroups if $.type.equals(FulfillmentType.PHYSICAL)) != empty)", context); Boolean expressionOutcome1 = (Boolean)MVEL.executeExpression(domainExp1, domainVars); assert expressionOutcome1 != null && expressionOutcome1;
public void testParameterizedTypeInStrictMode3() { ParserContext ctx = new ParserContext(); ctx.setStrongTyping(true); ctx.addInput("base", Base.class); ExpressionCompiler compiler = new ExpressionCompiler("base.list"); // assertTrue(compiler.compile(ctx).getParserContext().getLastTypeParameters()[0].equals(String.class)); }
public static void exerciseWorkaround() { System.setProperty("mvel2.disable.jit", "true"); String rule = "CollectionUtils.intersection(level1.level2.getMultiValueSkuAttributes()[\"TEST-VALID\"],[\"TEST-VALID\"]).size()>0"; ParserContext context = new ParserContext(); context.addImport("CollectionUtils", SelectizeCollectionUtils.class); Serializable exp = MVEL.compileExpression(rule, context); executeTestCase(exp, "TEST-INVALID"); boolean response = executeTestCase(exp, "TEST-VALID"); if (!response) { //With the workaround, we should never get here System.out.print("false"); } else { //The expression should never be corrupted now that we've removed the overloaded method (this is the workaround) System.out.print("true"); } }
public void testShouldFail6() { try { ParserContext pctx = new ParserContext(); pctx.setStrongTyping(true); MVEL.compileExpression("new int[] {1.5}", pctx); } catch (Exception e) { return; } shouldThrowException(); }
public void testArrayLength() { ParserContext context = new ParserContext(); context.setStrongTyping(true); context.addInput("x", String[].class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("x.length", context); }
@Test public void testOfferAppliesToSpecificItems() { DiscreteOrderItemImpl orderItem = new DiscreteOrderItemImpl(); Sku sku = new SkuImpl(); sku.setRetailPrice(new Money("1")); sku.setId(1234L); orderItem.setSku(sku); OfferImpl offer = new OfferImpl(); offer.setType(OfferType.ORDER_ITEM); //Set up MVEL Context ParserContext context = new ParserContext(); //Import OfferType into the MVEL context since it may be used context.addImport("OfferType", OfferType.class); context.addImport("FulfillmentType", FulfillmentType.class); //Compile the MVEL Expression Serializable domainExp1 = MVEL.compileExpression("offer.type == OfferType.ORDER_ITEM and (currentItem.sku.id in [1234, 2345, 5678])", context); //Add variables to a HashMap that should be passed in to execute the expression HashMap<String, Object> domainVars = new HashMap<>(); domainVars.put("currentItem", orderItem); domainVars.put("offer", offer); //Execute the expression Boolean expressionOutcome1 = (Boolean)MVEL.executeExpression(domainExp1, domainVars); assert expressionOutcome1 != null && expressionOutcome1; }
public void testRegExWithCast() { final ParserContext parserContext = new ParserContext(); parserContext.setStrongTyping(true); parserContext.addInput("this", Foo.class); assertEquals(Boolean.class, MVEL.analyze("(String)bar.name ~= '[a-z].+'", parserContext)); }
public static void exerciseFailure() { System.setProperty("mvel2.disable.jit", "true"); String rule = "CollectionUtils.intersection(level1.level2.getMultiValueSkuAttributes()[\"TEST-VALID\"],[\"TEST-VALID\"]).size()>0"; ParserContext context = new ParserContext(); context.addImport("CollectionUtils", MvelTestOverloadUtils.class); Serializable exp = MVEL.compileExpression(rule, context); executeTestCase(exp, "TEST-INVALID"); boolean response = executeTestCase(exp, "TEST-VALID"); if (!response) { //We received the expected, corrupted expression state, so return true to validate the expected test results System.out.print("true"); } else { //We did not receive the expected, corrupted expression state. This can happen sometimes, since the ordering of methods //returned from the call to Class#getMethods for SelectizeCollectionUtilsTest is undetermined. Return false //since we did not validate the expected test results in this run. System.out.print("false"); } }
private void testMVELTyped(String text) { String str = IMPORTS + text; ParserContext pctx = new ParserContext(); pctx.setStrongTyping(true); Map<String, Object> vars = new HashMap<String, Object>(); Object o = MVEL.compileExpression(str, pctx); MVEL.executeExpression(o, vars); }
ParserContext context = new ParserContext(); Serializable domainExp1 = MVEL.compileExpression("result = false; for (cat : currentItem.product.allParentCategories) {if (cat.name == 't-shirt') {result = true;}}; return result and order.subTotal.amount >= 50", context); Boolean expressionOutcome1 = (Boolean)MVEL.executeExpression(domainExp1, domainVars); assert expressionOutcome1 != null && expressionOutcome1; Serializable domainExp2 = MVEL.compileExpression("($ in currentItem.product.allParentCategories if $.name == 't-shirt') != empty and order.subTotal.amount >= 50", context); Boolean expressionOutcome2 = (Boolean)MVEL.executeExpression(domainExp2, domainVars); assert expressionOutcome2 != null && expressionOutcome2;
public void testParameterizedTypeInStrictMode() { ParserContext ctx = new ParserContext(); ctx.setStrongTyping(true); ctx.addInput("foo", HashMap.class, new Class[]{String.class, String.class}); ExpressionCompiler compiler = new ExpressionCompiler("foo.get('bar').toUpperCase()", ctx); compiler.compile(); }
@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 void testDeepMethodNameResolution() { String expression = "variables = [ \"symbol\" : \"RHT\" ]; \n" + "drools.getProcessManager().startProcess(\"id\", variables );"; // third pass ParserContext ctx = new ParserContext(); ctx.setStrongTyping(true); ctx.addInput("drools", KnowledgeRuntimeHelper.class); Map vars = new HashMap(); vars.put("drools", new KnowledgeRuntimeHelper()); Serializable expr = MVEL.compileExpression(expression, ctx); executeExpression(expr, vars); }