/** * Construct the new {@link BlockStatement} for the given branch. */ @Nonnull public final BlockStatement handleBranch(@Nonnull ModelASTBranch branch) { BlockStatement newBlock = block(); for (ModelASTStep s : branch.getSteps()) { // Don't process script blocks or a step that for some reason isn't an expression statement at all if (s instanceof AbstractModelASTCodeBlock || !(s.getSourceLocation() instanceof ExpressionStatement)) { newBlock.addStatement((Statement)s.getSourceLocation()); } else { ExpressionStatement es = (ExpressionStatement) s.getSourceLocation(); if (es.getExpression() instanceof MethodCallExpression) { MethodCallExpression methodCall = (MethodCallExpression) es.getExpression(); newBlock.addStatement(stmt(handleStep(s, methodCall))); } else { newBlock.addStatement(es); } } } return newBlock; }
/** * Call {@link #transformStep(ModelASTStep, MethodCallExpression)} if appropriate, after handling any nested steps as well. */ @Nonnull public final MethodCallExpression handleStep(@Nonnull ModelASTStep step, @Nonnull MethodCallExpression methodCall) { // No transformation inside script blocks. if (step instanceof AbstractModelASTCodeBlock) { return methodCall; } TupleExpression originalArgs = (TupleExpression) methodCall.getArguments(); if (step instanceof ModelASTTreeStep && originalArgs.getExpressions().size() > 0) { ArgumentListExpression newArgs = new ArgumentListExpression(); // Technically we can't get here if there 0 expressions, so the loop below is safe. for (int i = 0; i < originalArgs.getExpressions().size() - 1; i++) { newArgs.addExpression(originalArgs.getExpression(i)); } ClosureExpression originalClosure = (ClosureExpression) originalArgs.getExpression(originalArgs.getExpressions().size() - 1); BlockStatement newBlock = block(); for (ModelASTStep nested : ((ModelASTTreeStep) step).getChildren()) { ExpressionStatement es = (ExpressionStatement) nested.getSourceLocation(); newBlock.addStatement(stmt(handleStep(nested, (MethodCallExpression)es.getExpression()))); } originalClosure.setCode(newBlock); newArgs.addExpression(originalClosure); methodCall.setArguments(newArgs); } return transformStep(step, methodCall); }
assertEquals(1, firstBranch.getSteps().size()); ModelASTStep firstStep = firstBranch.getSteps().get(0); assertNull(firstStep.getSourceLocation()); assertEquals("junit", firstStep.getName()); assertTrue(firstStep.getArgs() instanceof ModelASTNamedArgumentList);
assertEquals(1, firstBranch.getSteps().size()); ModelASTStep firstStep = firstBranch.getSteps().get(0); assertNull(firstStep.getSourceLocation()); assertEquals("echo", firstStep.getName()); ModelASTValue val = null; assertEquals(2, secondBranch.getSteps().size()); ModelASTStep scriptStep = secondBranch.getSteps().get(0); assertNull(scriptStep.getSourceLocation()); assertTrue(scriptStep instanceof ModelASTScriptBlock); assertNull(scriptStep.getArgs().getSourceLocation()); assertNull(timeoutStep.getSourceLocation()); assertTrue(timeoutStep instanceof ModelASTTreeStep); assertEquals("timeout", timeoutStep.getName()); assertEquals(1, treeStep.getChildren().size()); assertEquals("echo", treeStep.getChildren().get(0).getName()); assertNull(treeStep.getChildren().get(0).getSourceLocation());