OperandStack operandStack = controller.getOperandStack(); compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels()); BytecodeVariable variable = compileStack.defineVariable(loop.getVariable(), false); MethodCallExpression iterator = new MethodCallExpression(loop.getCollectionExpression(), "iterator", new ArgumentListExpression()); iterator.visit(controller.getAcg()); operandStack.doGroovyCast(ClassHelper.Iterator_TYPE); loop.getLoopBlock().visit(controller.getAcg());
forLoop.getLoopBlock().visit(new VariableExpressionTypeMemoizer(varOrigType)); Expression collectionExpression = forLoop.getCollectionExpression(); if (collectionExpression instanceof ClosureListExpression) { collectionExpression.visit(this); final ClassNode collectionType = getType(collectionExpression); ClassNode forLoopVariableType = forLoop.getVariableType(); ClassNode componentType; if (Character_TYPE.equals(ClassHelper.getWrapper(forLoopVariableType)) && STRING_TYPE.equals(collectionType)) { typeCheckingContext.controlStructureVariables.put(forLoop.getVariable(), componentType); try { super.visitForLoop(forLoop); } finally { typeCheckingContext.controlStructureVariables.remove(forLoop.getVariable());
@Override public void visitForLoop(ForStatement stat) { stat.setCollectionExpression(replaceExpr(stat.getCollectionExpression())); stat.setLoopBlock(replace(stat.getLoopBlock())); }
@Override public void visitForLoop(ForStatement stat) { stat.getCollectionExpression().visit(this); stat.setLoopBlock(replace(stat.getLoopBlock())); }
public void visitForLoop(ForStatement forLoop) { forLoop.getCollectionExpression().visit(this); forLoop.getLoopBlock().visit(this); }
Parameter loopVar = loop.getVariable(); if (loopVar == ForStatement.FOR_LOOP_DUMMY) { visitForLoopWithClosureList(loop); compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabel()); Variable variable = compileStack.defineVariable(loop.getVariable(), false); MethodCallExpression iterator = new MethodCallExpression(loop.getCollectionExpression(), "iterator", new ArgumentListExpression()); iterator.visit(this); loop.getLoopBlock().visit(this);
public void visitForLoop(ForStatement forLoop) { forLoop.setCollectionExpression(transform(forLoop.getCollectionExpression())); super.visitForLoop(forLoop); }
@Override public void visitForLoop(ForStatement forLoop) { Expression exp = forLoop.getCollectionExpression(); exp.visit(this); Parameter loopParam = forLoop.getVariable(); if (loopParam != null) { varStack.getLast().add(loopParam.getName()); } super.visitForLoop(forLoop); }
@Override public void visitForLoop(final ForStatement forLoop) { super.visitForLoop(forLoop); Expression collectionExpression = forLoop.getCollectionExpression(); if (!(collectionExpression instanceof ClosureListExpression)) { final ClassNode collectionType = getType(forLoop.getCollectionExpression()); ClassNode forLoopVariableType = forLoop.getVariableType(); ClassNode componentType; if (Character_TYPE.equals(ClassHelper.getWrapper(forLoopVariableType)) && STRING_TYPE.equals(collectionType)) { // we allow auto-coercion here componentType = forLoopVariableType; } else { componentType = inferLoopElementType(collectionType); } forLoop.getVariable().setType(componentType); forLoop.getVariable().setOriginType(componentType); } }
Parameter parameter = forStatement.getVariable(); if (variable.equals(parameter.getName())) { return parameter; Expression collectionExpression = forStatement.getCollectionExpression(); if (collectionExpression instanceof ClosureListExpression) { ASTNode result = getVariableInClosureList((ClosureListExpression) collectionExpression, variable); Statement code = forStatement.getLoopBlock(); if (code instanceof BlockStatement) { ASTNode result = getVariableInBlockStatement((BlockStatement) code, variable);
public void visitForLoop(ForStatement forLoop) { pushState(); forLoop.setVariableScope(currentScope); Parameter p = forLoop.getVariable(); p.setInStaticContext(currentScope.isInStaticContext()); if (p != ForStatement.FOR_LOOP_DUMMY) declare(p, forLoop); super.visitForLoop(forLoop); popState(); }
public void writeForStatement(ForStatement loop) { Parameter loopVar = loop.getVariable(); if (loopVar == ForStatement.FOR_LOOP_DUMMY) { writeForLoopWithClosureList(loop); } else { writeForInLoop(loop); } }
public void visitForLoop(ForStatement forLoop) { resolveOrFail(forLoop.getVariableType(), forLoop); super.visitForLoop(forLoop); }
@Override public void visitForLoop(ForStatement forLoop) { if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { addOccurrences(forLoop.getVariableType(), (ClassNode) FindTypeUtils.findCurrentNode(path, doc, cursorOffset)); } else { final Parameter forLoopVar = forLoop.getVariable(); final String varName = forLoopVar.getName(); if (leaf instanceof Variable) { if (varName.equals(((Variable) leaf).getName())) { occurrences.add(forLoopVar); } } else if (leaf instanceof ForStatement) { if (varName.equals(((ForStatement) leaf).getVariable().getName())) { occurrences.add(forLoopVar); } } } super.visitForLoop(forLoop); }
private void writeEnumerationBasedForEachLoop( CompileStack compileStack, OperandStack operandStack, MethodVisitor mv, ForStatement loop, Expression collectionExpression, ClassNode collectionType, Parameter loopVariable) { // Declare the loop counter. BytecodeVariable variable = compileStack.defineVariable(loopVariable, false); collectionExpression.visit(controller.getAcg()); // Then get the iterator and generate the loop control int enumIdx = compileStack.defineTemporaryVariable("$enum", ENUMERATION_CLASSNODE, true); Label continueLabel = compileStack.getContinueLabel(); Label breakLabel = compileStack.getBreakLabel(); mv.visitLabel(continueLabel); mv.visitVarInsn(ALOAD, enumIdx); ENUMERATION_HASMORE_METHOD.call(mv); // note: ifeq tests for ==0, a boolean is 0 if it is false mv.visitJumpInsn(IFEQ, breakLabel); mv.visitVarInsn(ALOAD, enumIdx); ENUMERATION_NEXT_METHOD.call(mv); operandStack.push(ClassHelper.OBJECT_TYPE); operandStack.storeVar(variable); // Generate the loop body loop.getLoopBlock().visit(controller.getAcg()); mv.visitJumpInsn(GOTO, continueLabel); mv.visitLabel(breakLabel); }
@Override public void run() { literal(forLoop.getStatementLabel()); visit(loop.getExpressions()); visitWithSafepoint(forLoop.getLoopBlock()); } });
public void testLoop() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "coll")}; Statement loopStatement = createPrintlnStatement(new VariableExpression("i")); ForStatement statement = new ForStatement(new Parameter(ClassHelper.OBJECT_TYPE, "i"), new VariableExpression("coll"), loopStatement); classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke a method with looping"); Object[] array = {new Integer(1234), "abc", "def"}; try { InvokerHelper.invokeMethod(bean, "iterateDemo", new Object[]{array}); } catch (InvokerInvocationException e) { System.out.println("Caught: " + e.getCause()); e.getCause().printStackTrace(); fail("Should not have thrown an exception"); } System.out.println("################ Done"); }
} else if (statement instanceof ForStatement) { ForStatement fs = (ForStatement) statement; Statement loopBlock = fs.getLoopBlock(); BlockStatement newLoopBlock = new BlockStatement(); addStatementToNewQuery(loopBlock, newLoopBlock, addAll, propertyNames, variableScope); fs.setLoopBlock(flattenStatementIfNecessary(newLoopBlock)); newCode.addStatement(fs); } else if (statement instanceof WhileStatement) {
&& (((ForStatement) scope).getCollectionExpression() instanceof ClosureListExpression)) { blocks.add(((ForStatement) scope).getCollectionExpression());
add.setMethodTarget(ARRAYLIST_ADD_METHOD); ForStatement stmt = new ForStatement( iterator, receiver, new ExpressionStatement(add) ); stmt.visit(controller.getAcg());