Search in sources :

Example 71 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class TypesAndInferenceTests method testGenericMethods.

public void testGenericMethods() {
    String str = "Integer.parseInt( a.getMap().get(\"x\") )";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("a", AGenericTestClass.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    AGenericTestClass a = new AGenericTestClass();
    a.setMap(new HashMap<String, String>());
    a.getMap().put("x", "10");
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("a", a);
    Number result = (Number) MVEL.executeExpression(stmt, null, variables);
    assertEquals(10, result.intValue());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 72 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class TypesAndInferenceTests method testGetCorrectInputs.

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());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 73 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class WithTests method testWithAndEnumInPackageImport.

public void testWithAndEnumInPackageImport() {
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addPackageImport(MyEnum.class.getPackage().getName());
    ParserContext pCtx = new ParserContext(pconf);
    pCtx.setStrongTyping(true);
    pCtx.addInput("thing", Thing.class);
    Thing thing = new Thing("xxx");
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("thing", thing);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("with( thing ) { myEnum = MyEnum.FULL_DOCUMENTATION }", pCtx);
    MVEL.executeExpression(stmt, null, vars);
    assertEquals(MyEnum.FULL_DOCUMENTATION, thing.getMyEnum());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) ParserContext(org.mvel2.ParserContext) Thing(org.mvel2.tests.core.res.Thing) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 74 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project kie-wb-common by kiegroup.

the class EnumDropdownServiceImpl method loadDropDownExpression.

protected String[] loadDropDownExpression(final ClassLoader classLoader, final MVELEvaluator mvelEvaluator, final String[] valuePairs, String expression) {
    try {
        final Map<String, String> context = new HashMap<String, String>();
        for (final String valuePair : valuePairs) {
            if (valuePair == null) {
                return new String[0];
            }
            String[] pair = valuePair.split("=");
            if (pair.length == 1) {
                String[] swap = new String[2];
                swap[0] = pair[0];
                swap[1] = "";
                pair = swap;
            }
            context.put(pair[0], pair[1]);
        }
        // first interpolate the pairs
        expression = (String) TemplateRuntime.eval(expression, context);
        // now we can eval it for real...
        final ParserConfiguration pconf = new ParserConfiguration();
        final ParserContext pctx = new ParserContext(pconf);
        pconf.setClassLoader(classLoader);
        final Serializable compiled = MVEL.compileExpression(expression, pctx);
        Object result = mvelEvaluator.executeExpression(compiled, new HashMap<String, Object>());
        // Handle result of evaluation
        if (result instanceof String[]) {
            return (String[]) result;
        } else if (result instanceof List) {
            List l = (List) result;
            String[] xs = new String[l.size()];
            for (int i = 0; i < xs.length; i++) {
                Object el = l.get(i);
                xs[i] = el.toString();
            }
            return xs;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw ExceptionUtilities.handleException(e);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) List(java.util.List) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 75 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project drools by kiegroup.

the class PatternBuilder method setInputs.

protected void setInputs(RuleBuildContext context, ExprBindings descrBranch, Class<?> thisClass, String expr) {
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    ParserConfiguration conf = data.getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    final ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(false);
    pctx.setStrongTyping(false);
    pctx.addInput("this", thisClass);
    // overrides the mvel empty label
    pctx.addInput("empty", boolean.class);
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
    MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
    MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
    try {
        MVEL.analysisCompile(expr, pctx);
    } catch (Exception e) {
        // reported during expression analysis, so swallow it at the moment
        return;
    }
    if (!pctx.getInputs().isEmpty()) {
        for (String v : pctx.getInputs().keySet()) {
            // to an "empty" property, or the if will evaluate to true even if it doesn't
            if ("this".equals(v) || (PropertyTools.getFieldOrAccessor(thisClass, v) != null && expr.matches("(^|.*\\W)empty($|\\W.*)"))) {
                descrBranch.getFieldAccessors().add(v);
            } else if ("empty".equals(v)) {
            // do nothing
            } else if (!context.getPkg().getGlobals().containsKey(v)) {
                descrBranch.getRuleBindings().add(v);
            } else {
                descrBranch.getGlobalBindings().add(v);
            }
        }
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ParserContext(org.mvel2.ParserContext) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)74 ParserContext (org.mvel2.ParserContext)70 HashMap (java.util.HashMap)39 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)32 LinkedHashMap (java.util.LinkedHashMap)29 Map (java.util.Map)23 Serializable (java.io.Serializable)10 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)7 CompileException (org.mvel2.CompileException)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MapObject (org.mvel2.tests.core.res.MapObject)5 Method (java.lang.reflect.Method)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 CachedMapVariableResolverFactory (org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory)3 MVELExpressionExecutor (org.mule.runtime.core.internal.el.mvel.MVELExpressionExecutor)3 Bar (org.mvel2.tests.core.res.Bar)3