use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class GenerateForLoopAssistProposal method generateForRewrite.
/**
* Helper to generate an index based <code>for</code> loop to iterate over an array.
*
* @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
* @return an applicable {@link ASTRewrite} instance
*/
private ASTRewrite generateForRewrite(AST ast) {
ASTRewrite rewrite = ASTRewrite.create(ast);
ForStatement loopStatement = ast.newForStatement();
//$NON-NLS-1$
SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "int", null, true);
loopStatement.initializers().add(getForInitializer(ast, loopVariableName));
FieldAccess getArrayLengthExpression = ast.newFieldAccess();
getArrayLengthExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
//$NON-NLS-1$
getArrayLengthExpression.setName(ast.newSimpleName("length"));
loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), getArrayLengthExpression, InfixExpression.Operator.LESS));
loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));
Block forLoopBody = ast.newBlock();
forLoopBody.statements().add(ast.newExpressionStatement(getForBodyAssignment(rewrite, loopVariableName)));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
rewrite.replace(fCurrentNode, loopStatement, null);
return rewrite;
}
use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class GenerateForLoopAssistProposal method generateIteratorBasedForRewrite.
/**
* Helper to generate an iterator based <code>for</code> loop to iterate over an
* {@link Iterable}.
*
* @param ast the {@link AST} instance to rewrite the loop to
* @return the complete {@link ASTRewrite} object
*/
private ASTRewrite generateIteratorBasedForRewrite(AST ast) {
ASTRewrite rewrite = ASTRewrite.create(ast);
ForStatement loopStatement = ast.newForStatement();
ITypeBinding loopOverType = extractElementType(ast);
//$NON-NLS-1$
SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true);
loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName));
MethodInvocation loopExpression = ast.newMethodInvocation();
//$NON-NLS-1$
loopExpression.setName(ast.newSimpleName("hasNext"));
SimpleName expressionName = ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier());
loopExpression.setExpression(expressionName);
loopStatement.setExpression(loopExpression);
Block forLoopBody = ast.newBlock();
Assignment assignResolvedVariable = getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName);
forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
rewrite.replace(fCurrentNode, loopStatement, null);
return rewrite;
}
use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class ConvertForLoopQuickFixTest method satisfiesPrecondition.
private boolean satisfiesPrecondition(ICompilationUnit cu) {
ForStatement statement = getForStatement(cu);
ConvertLoopOperation op = new ConvertForLoopOperation(statement);
return op.satisfiesPreconditions().isOK();
}
use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class ExtractMethodRefactoring method replaceBranches.
private void replaceBranches(final CompilationUnitChange result) {
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
for (int i = 0; i < selectedNodes.length; i++) {
ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
private LinkedList<String> fOpenLoopLabels = new LinkedList<String>();
private void registerLoopLabel(Statement node) {
String identifier;
if (node.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
identifier = labeledStatement.getLabel().getIdentifier();
} else {
identifier = null;
}
fOpenLoopLabels.add(identifier);
}
@Override
public boolean visit(ForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(ForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(WhileStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(WhileStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(EnhancedForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(EnhancedForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(DoStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(DoStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public void endVisit(ContinueStatement node) {
final SimpleName label = node.getLabel();
if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
result.addTextEditGroup(description);
ReturnStatement rs = fAST.newReturnStatement();
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
}
fRewriter.replace(node, rs, description);
}
}
});
}
}
use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.
private static boolean isReferringToLocalVariableFromFor(Expression expression) {
ASTNode current = expression;
ASTNode parent = current.getParent();
while (parent != null && !(parent instanceof BodyDeclaration)) {
if (parent instanceof ForStatement) {
ForStatement forStmt = (ForStatement) parent;
if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
List<Expression> initializers = forStmt.initializers();
if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
expression.accept(checker);
if (checker.isReferringToForVariable())
return true;
}
}
}
current = parent;
parent = current.getParent();
}
return false;
}
Aggregations