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 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())); } }
MVEL.executeExpression(indexCompile, model.createFactory()));
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); }