Search in sources :

Example 56 with ParserConfiguration

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

the class CoreConfidenceTests method testGenericsMap.

public void testGenericsMap() throws Exception {
    try {
        String str = "triangle.deliveries[0].containsKey( \"x\" )";
        ParserConfiguration pconf = new ParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        pctx.setStrongTyping(true);
        pctx.addInput("triangle", Triangle.class);
        ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    } catch (Exception e) {
        // it should not raise CCE
        e.printStackTrace();
        throw e;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException) IOException(java.io.IOException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 57 with ParserConfiguration

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

the class CoreConfidenceTests method testEmptyOperatorOnStrings.

public void testEmptyOperatorOnStrings() {
    ParserConfiguration conf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("nullString", String.class);
    pctx.addInput("emptyString", String.class);
    pctx.addInput("blankString", String.class);
    pctx.addInput("nonEmptyString", String.class);
    Map vars = new HashMap() {

        {
            put("nullString", null);
            put("emptyString", "");
            put("blankString", "   ");
            put("nonEmptyString", "abc");
        }
    };
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("nullString == empty", pctx), vars));
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("emptyString == empty", pctx), vars));
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("blankString == empty", pctx), vars));
    assertEquals(false, MVEL.executeExpression(MVEL.compileExpression("nonEmptyString == empty", pctx), vars));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 58 with ParserConfiguration

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

the class CoreConfidenceTests method testStrTriangleEqualsEquals.

public void testStrTriangleEqualsEquals() {
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    try {
        ParserConfiguration pconf = new ParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        pctx.addInput("this", Triangle.class);
        pctx.setStrongTyping(true);
        String str = "this.strLabel == this";
        try {
            ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
            fail("should have failed");
        } catch (CompileException e) {
            System.out.println();
            return;
        }
    } finally {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 59 with ParserConfiguration

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

the class CoreConfidenceTests method testContextObjMethodCall.

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

Example 60 with ParserConfiguration

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

the class ASTNode method optimize.

private Object optimize(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if ((fields & DEOP) != 0) {
        fields ^= DEOP;
    }
    AccessorOptimizer optimizer;
    Object retVal = null;
    if ((fields & NOJIT) != 0 || factory != null && factory.isResolveable(nameCache)) {
        optimizer = getAccessorCompiler(SAFE_REFLECTIVE);
    } else {
        optimizer = getDefaultAccessorCompiler();
    }
    ParserContext pCtx;
    if ((fields & PCTX_STORED) != 0) {
        pCtx = (ParserContext) literal;
    } else {
        pCtx = new ParserContext(new ParserConfiguration(getInjectedImports(factory), null));
    }
    try {
        pCtx.optimizationNotify();
        setAccessor(optimizer.optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, egressType));
    } catch (OptimizationNotSupported ne) {
        setAccessor((optimizer = getAccessorCompiler(SAFE_REFLECTIVE)).optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, null));
    }
    if (accessor == null) {
        return get(expr, start, offset, ctx, factory, thisValue, pCtx);
    }
    if (retVal == null) {
        retVal = optimizer.getResultOptPass();
    }
    if (egressType == null) {
        egressType = optimizer.getEgressType();
    }
    return retVal;
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) ParserContext(org.mvel2.ParserContext) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) 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