Search in sources :

Example 56 with ParserConfiguration

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

the class PrimitiveTypesTest method testFloatPrimitive.

public void testFloatPrimitive() {
    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("floatValue == 15.1", pctx), new FactWithFloat(15.1f));
    assertTrue(result);
}
Also used : ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 57 with ParserConfiguration

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

the class AsmOptimizerOsgiTest method testCollectionAccessWithInvalidThreadClassLoader.

public void testCollectionAccessWithInvalidThreadClassLoader() {
    String expression = "['A', 'B', 'C'] contains 'B'";
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    parserConfiguration.setClassLoader(MVEL.class.getClassLoader());
    Serializable compiledExpression = MVEL.compileExpression(expression, new ParserContext(parserConfiguration));
    ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(NO_MVEL_CL);
        for (int i = 0; i <= DynamicOptimizer.tenuringThreshold; i++) {
            Object result = MVEL.executeExpression(compiledExpression);
            assertEquals(Boolean.TRUE, result);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(currentCl);
    }
}
Also used : Serializable(java.io.Serializable) MVEL(org.mvel2.MVEL) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 58 with ParserConfiguration

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

the class ArraysTests method testMultiDimensionalArrayType.

public void testMultiDimensionalArrayType() {
    String str = "$c.cheeses[0][0] = new Cheese('brie', 15)";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport(Cheese.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("$c", Column.class);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map<String, Object> vars = new HashMap<String, Object>();
    Column c = new Column("x", 1);
    c.setCheeses(new Cheese[5][5]);
    vars.put("$c", c);
    MVEL.executeExpression(stmt, null, vars);
    assertEquals(new Cheese("brie", 15), c.getCheeses()[0][0]);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) Cheese(org.mvel2.tests.core.res.Cheese) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 59 with ParserConfiguration

use of org.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)

Example 60 with ParserConfiguration

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

the class MVELBeanCreator method createBean.

@Override
public <T> T createBean(ClassLoader cl, String type, QualifierModel qualifier) throws Exception {
    if (qualifier != null) {
        throw new IllegalArgumentException("Cannot use a qualifier without a CDI container");
    }
    ParserConfiguration config = new ParserConfiguration();
    config.setClassLoader(cl);
    ParserContext ctx = new ParserContext(config);
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            ctx.addVariable(entry.getKey(), entry.getValue().getClass());
        }
    }
    Object compiledExpression = MVEL.compileExpression(type, ctx);
    return (T) MVELSafeHelper.getEvaluator().executeExpression(compiledExpression, parameters);
}
Also used : ParserContext(org.mvel2.ParserContext) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)95 ParserContext (org.mvel2.ParserContext)88 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)48 HashMap (java.util.HashMap)43 LinkedHashMap (java.util.LinkedHashMap)30 Map (java.util.Map)27 Serializable (java.io.Serializable)12 CompileException (org.mvel2.CompileException)9 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)9 IOException (java.io.IOException)7 MapObject (org.mvel2.tests.core.res.MapObject)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 Method (java.lang.reflect.Method)5 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)5 RecognitionException (org.antlr.runtime.RecognitionException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3