Search in sources :

Example 61 with ParserContext

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

the class CoreConfidenceTests method testDynamicImportsWithIdentifierSameAsClassWithDiffCase.

public void testDynamicImportsWithIdentifierSameAsClassWithDiffCase() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("org.mvel2.tests.core.res");
    ctx.setStrictTypeEnforcement(false);
    ExpressionCompiler compiler = new ExpressionCompiler("bar.add(\"hello\")");
    compiler.compile(ctx);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 62 with ParserContext

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

the class CoreConfidenceTests method testDynamicImportsOnNestedExpressions.

public void testDynamicImportsOnNestedExpressions() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("org.mvel2.tests.core.res");
    ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", new Cheese(\"cheddar\", 15))");
    Cheesery p1 = new Cheesery("bobbo", new Cheese("cheddar", 15));
    Cheesery p2 = (Cheesery) executeExpression(compiler.compile(ctx), new DefaultLocalVariableResolverFactory());
    assertEquals(p1, p2);
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 63 with ParserContext

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

the class CoreConfidenceTests method testTestIntToLong.

public void testTestIntToLong() {
    String s = "1+(long)a";
    ParserContext pc = new ParserContext();
    pc.addInput("a", Integer.class);
    ExpressionCompiler compiler = new ExpressionCompiler(s, pc);
    CompiledExpression expr = compiler.compile();
    Map vars = new HashMap();
    vars.put("a", 1);
    Object r = ((ExecutableStatement) expr).getValue(null, new MapVariableResolverFactory(vars));
    assertEquals(new Long(2), r);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 64 with ParserContext

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

the class CoreConfidenceTests method testDynamicImports.

public void testDynamicImports() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("java.util");
    ExpressionCompiler compiler = new ExpressionCompiler("HashMap");
    Serializable s = compiler.compile(ctx);
    assertEquals(HashMap.class, executeExpression(s));
    compiler = new ExpressionCompiler("map = new HashMap(); map.size()");
    s = compiler.compile(ctx);
    assertEquals(0, executeExpression(s, new DefaultLocalVariableResolverFactory()));
}
Also used : Serializable(java.io.Serializable) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 65 with ParserContext

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

the class VariableSpaceCompiler method compileShared.

public static SharedVariableSpaceModel compileShared(String expr, ParserContext pCtx, Object[] vars) {
    String[] varNames = pCtx.getIndexedVarNames();
    ParserContext analysisContext = ParserContext.create();
    analysisContext.setIndexAllocation(true);
    MVEL.analysisCompile(expr, analysisContext);
    Set<String> localNames = analysisContext.getVariables().keySet();
    pCtx.addIndexedLocals(localNames);
    String[] locals = localNames.toArray(new String[localNames.size()]);
    String[] allVars = new String[varNames.length + locals.length];
    System.arraycopy(varNames, 0, allVars, 0, varNames.length);
    System.arraycopy(locals, 0, allVars, varNames.length, locals.length);
    return new SharedVariableSpaceModel(allVars, vars);
}
Also used : ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)372 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)190 HashMap (java.util.HashMap)140 Serializable (java.io.Serializable)100 ParserConfiguration (org.mvel2.ParserConfiguration)86 Map (java.util.Map)74 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)67 LinkedHashMap (java.util.LinkedHashMap)66 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 CompileException (org.mvel2.CompileException)53 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)31 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)24 MapObject (org.mvel2.tests.core.res.MapObject)23 ArrayList (java.util.ArrayList)22 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)19 IOException (java.io.IOException)16 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)16 Debugger (org.mvel2.debug.Debugger)16