Search in sources :

Example 26 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class ArithmeticTests method testJIRA161.

public void testJIRA161() {
    Serializable s = MVEL.compileExpression("1==-(-1)", ParserContext.create().stronglyTyped());
    assertEquals(1 == -(-1), MVEL.executeExpression(s));
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    CompiledExpression compiledExpression = new ExpressionCompiler("1==-(-1)").compile(ctx);
    assertEquals(1 == -(-1), MVEL.executeExpression(compiledExpression));
}
Also used : Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 27 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class ArithmeticTests method testStrongTypingModeComparison.

public void testStrongTypingModeComparison() {
    ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("a", Long.class);
    CompiledExpression compiledExpression = new ExpressionCompiler("a==0").compile(parserContext);
    HashMap<String, Object> variables = new HashMap<String, Object>();
    variables.put("a", 0l);
    MVEL.executeExpression(compiledExpression, variables);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 28 with ParserContext

use of org.mule.mvel2.ParserContext in project stargate-core by tuplejump.

the class AggregateFunction method init.

@Override
public void init(Options options) {
    this.options = options;
    int k = 0;
    positions = new HashMap<>();
    aggregateFields = new String[aggregates.length];
    for (AggregateFactory aggregateFactory : aggregates) {
        String field = aggregateFactory.getField();
        if (field != null) {
            aggregateFields[k] = field;
            positions.put(field, k++);
        }
    }
    ParserConfiguration parserConfig = getParserConfiguration();
    if (groupBy != null) {
        this.groupByExpressions = new ExecutableStatement[groupBy.length];
        this.simpleExpressions = new boolean[groupBy.length];
        groupByFields = new ArrayList<>();
        for (int i = 0; i < groupBy.length; i++) {
            String groupByField = groupBy[i];
            String groupByCol = getGroupBy(groupByField);
            boolean isSimpleExpression = options.types.containsKey(getColumnName(groupByCol));
            ParserContext parserContext = new ParserContext(parserConfig);
            groupByExpressions[i] = (ExecutableStatement) MVEL.compileExpression(groupByField, parserContext);
            if (isSimpleExpression) {
                simpleExpressions[i] = true;
                int pos = k++;
                positions.put(groupByCol, pos);
                groupByFields.add(groupByCol);
            } else {
                simpleExpressions[i] = false;
                Set<String> keys = parserContext.getInputs().keySet();
                for (String key : keys) {
                    int pos = k++;
                    boolean canResolve = options.types.containsKey(getColumnName(key));
                    if (canResolve) {
                        String groupByColField = getGroupBy(key);
                        positions.put(key, pos);
                        groupByFields.add(groupByColField);
                    }
                }
            }
        }
    }
    group = new Group(options, aggregates, groupBy, groupByExpressions);
}
Also used : ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 29 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class FailureTests method testShouldFail8.

public void testShouldFail8() {
    try {
        ParserContext pCtx = new ParserContext();
        pCtx.setStrongTyping(true);
        MVEL.compileExpression("for (String s : new java.util.HashMap()) { }", pCtx);
    } catch (Exception e) {
        // e.printStackTrace();
        return;
    }
    shouldThrowException();
}
Also used : ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException)

Example 30 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class FailureTests method testShouldFail7.

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

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10