public static SimpleVariableSpaceModel compile(String expr, ParserContext pCtx) { 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 SimpleVariableSpaceModel(allVars); } }
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); }
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); }
public static SimpleVariableSpaceModel compile(String expr, ParserContext pCtx) { 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 SimpleVariableSpaceModel(allVars); } }
ctx.setIndexAllocation(false); ExpressionCompiler compiler = new ExpressionCompiler(expr, blockStart, blockOffset, ctx); compiler.setVerifyOnly(true); compiler.compile(); ctx.setIndexAllocation(true);
ctx.setIndexAllocation(false); ExpressionCompiler compiler = new ExpressionCompiler(expr, blockStart, blockOffset, ctx); compiler.setVerifyOnly(true); compiler.compile(); ctx.setIndexAllocation(true);
ctx.setIndexAllocation(true);
parserContext.setIndexAllocation( true );
ctx.setIndexAllocation(true);
public void testVariableInjection2() { String[] varNames = {"x", "y", "z"}; Object[] values = {10, 20, 30}; String expr = "foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + " res = z - y - x;\n" + " int k = 5;\n" + " foo = k;" + "}; \n" + "for (i = 0; i < 100000; i++) { foo++; }; foo;"; ParserContext ctx = ParserContext.create(); ctx.addIndexedInput(varNames); ctx.setIndexAllocation(true); SimpleVariableSpaceModel model = VariableSpaceCompiler.compile(expr, ctx); Serializable indexCompile = MVEL.compileExpression(expr, ctx); Serializable dynamicCompile = MVEL.compileExpression(expr, ParserContext.create()); Map<String, Object> map = new HashMap<String, Object>(); map.put("x", 10); map.put("y", 20); map.put("z", 30); assertEquals(MVEL.executeExpression(dynamicCompile, map), MVEL.executeExpression(indexCompile, model.createFactory(values))); }
ctx.setIndexAllocation(true);
public void testIndexedVariableFactory() { ParserContext ctx = ParserContext.create(); String[] vars = {"a", "b"}; Object[] vals = {"foo", "bar"}; ctx.setIndexAllocation(true); ctx.addIndexedInput(vars); String expr = "def myfunc(z) { a + b + z }; myfunc('poop');"; SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, vals); Serializable s = MVEL.compileExpression(expr, ctx); // VariableResolverFactory locals = new CachingMapVariableResolverFactory(new HashMap<String, Object>()); // VariableResolverFactory injected = new IndexedVariableResolverFactory(vars, vals, locals); assertEquals("foobarpoop", MVEL.executeExpression(s, model.createFactory())); } }
public void testAssignmentOnTwoDimensionalArrayUsingIndexedInput() { ParserConfiguration conf = new ParserConfiguration(); ParserContext pctx = new ParserContext( conf ); pctx.setIndexAllocation( true ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("array", Double[][].class); pctx.addIndexedInput("array"); Map vars = new HashMap() {{ put("array", new Double[2][2]); }}; assertEquals(42.0, MVEL.executeExpression(MVEL.compileExpression("array[1][1] = 42.0;\narray[1][1]", pctx), vars)); } }