use of org.eclipse.jdt.core.dom.EnhancedForStatement 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.EnhancedForStatement in project AutoRefactor by JnRouvignac.
the class CleanCodeRatherThanSemicolonRefactoring method visit.
@Override
public boolean visit(EmptyStatement node) {
ASTNode parent = node.getParent();
if (parent instanceof Block) {
this.ctx.getRefactorings().remove(node);
return DO_NOT_VISIT_SUBTREE;
}
parent = getParentIgnoring(node, Block.class);
if (parent instanceof IfStatement) {
IfStatement is = (IfStatement) parent;
List<Statement> thenStmts = asList(is.getThenStatement());
List<Statement> elseStmts = asList(is.getElseStatement());
boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
if (thenIsEmptyStmt && elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(parent);
return DO_NOT_VISIT_SUBTREE;
} else if (thenIsEmptyStmt && is.getElseStatement() == null) {
this.ctx.getRefactorings().remove(is);
return DO_NOT_VISIT_SUBTREE;
} else if (elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(is.getElseStatement());
return DO_NOT_VISIT_SUBTREE;
}
} else if (parent instanceof TryStatement) {
TryStatement ts = (TryStatement) parent;
return maybeRemoveEmptyStmtBody(node, ts, ts.getBody());
} else if (parent instanceof EnhancedForStatement) {
EnhancedForStatement efs = (EnhancedForStatement) parent;
return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
} else if (parent instanceof ForStatement) {
ForStatement fs = (ForStatement) parent;
return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
} else if (parent instanceof WhileStatement) {
WhileStatement ws = (WhileStatement) parent;
return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method replace.
private void replace(VariableAccess varDecl, VariableAccess varAccess) {
final ASTBuilder b = this.ctx.getASTBuilder();
final AST ast = b.getAST();
final ASTNode scope = varAccess.getScope();
final Name varName = varAccess.getVariableName();
final Type varType = getType(varDecl.getVariableName().getParent());
if (scope instanceof Block) {
final List<Statement> stmts = statements((Block) scope);
for (int i = 0; i < stmts.size(); i++) {
final Statement stmt = stmts.get(i);
// FIXME i=0
final Expression parentExpr = getAncestor(varName, Expression.class);
// FIXME i=0
final Statement parentStmt = getAncestor(parentExpr, Statement.class);
if (stmt.equals(parentStmt)) {
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
vds.setType(varType);
this.ctx.getRefactorings().replace(stmt, vds);
break;
}
}
} else if (scope instanceof EnhancedForStatement) {
final EnhancedForStatement efs = (EnhancedForStatement) scope;
final EnhancedForStatement newEfs = b.copy(efs);
newEfs.setParameter(b.copy(efs.getParameter()));
newEfs.setExpression(b.copy(efs.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(efs.getBody(), parentStmt)) {
newEfs.setBody(copy(efs.getBody(), varName));
}
this.ctx.getRefactorings().replace(efs, newEfs);
} else if (scope instanceof ForStatement) {
final ForStatement fs = (ForStatement) scope;
final ForStatement newFs = b.copy(fs);
final List<Expression> initializers = initializers(newFs);
if (initializers.size() == 1) {
final Expression init = initializers.remove(0);
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
vde.setType(varType);
initializers.add(vde);
this.ctx.getRefactorings().replace(fs, newFs);
// TODO JNR
// if (equalNotNull(fs.getBody(), parentStmt)) {
// newFs.setBody(copy(fs.getBody()));
// }
} else {
throw new NotImplementedException(scope, "for more than one initializer in for loop.");
}
} else if (scope instanceof WhileStatement) {
final WhileStatement ws = (WhileStatement) scope;
final WhileStatement newWs = ast.newWhileStatement();
newWs.setExpression(b.copy(ws.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(ws.getBody(), parentStmt)) {
newWs.setBody(copy(ws.getBody(), varName));
}
this.ctx.getRefactorings().replace(ws, newWs);
} else if (scope instanceof IfStatement) {
final IfStatement is = (IfStatement) scope;
final IfStatement newIs = ast.newIfStatement();
newIs.setExpression(b.copy(is.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(is.getThenStatement(), parentStmt)) {
newIs.setThenStatement(copy(is.getThenStatement(), varName));
if (is.getElseStatement() != null) {
newIs.setElseStatement(b.copy(is.getElseStatement()));
}
this.ctx.getRefactorings().replace(is, newIs);
} else if (equalNotNull(is.getElseStatement(), parentStmt)) {
if (is.getThenStatement() != null) {
newIs.setThenStatement(b.copy(is.getThenStatement()));
}
newIs.setElseStatement(copy(is.getElseStatement(), varName));
this.ctx.getRefactorings().replace(is, newIs);
} else {
throw new IllegalStateException(is, "Parent statement should be inside the then or else statement of this if statement: " + is);
}
} else {
throw new NotImplementedException(scope);
}
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project AutoRefactor by JnRouvignac.
the class RemoveEmptyStatementRefactoring method visit.
@Override
public boolean visit(EmptyStatement node) {
ASTNode parent = node.getParent();
if (parent instanceof Block) {
this.ctx.getRefactorings().remove(node);
return DO_NOT_VISIT_SUBTREE;
}
parent = getParentIgnoring(node, Block.class);
if (parent instanceof IfStatement) {
final IfStatement is = (IfStatement) parent;
if (isPassive(is.getExpression())) {
final List<Statement> thenStmts = asList(is.getThenStatement());
final List<Statement> elseStmts = asList(is.getElseStatement());
final boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
final boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
if (thenIsEmptyStmt && elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(parent);
return DO_NOT_VISIT_SUBTREE;
} else if (thenIsEmptyStmt && is.getElseStatement() == null) {
this.ctx.getRefactorings().remove(is);
return DO_NOT_VISIT_SUBTREE;
} else if (elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(is.getElseStatement());
return DO_NOT_VISIT_SUBTREE;
}
}
} else if (parent instanceof EnhancedForStatement) {
final EnhancedForStatement efs = (EnhancedForStatement) parent;
if (isPassive(efs.getExpression()) && efs.getExpression().resolveTypeBinding().isArray()) {
return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
}
} else if (parent instanceof ForStatement) {
final ForStatement fs = (ForStatement) parent;
if (arePassive(fs.initializers()) && isPassive(fs.getExpression())) {
return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
}
} else if (parent instanceof WhileStatement) {
final WhileStatement ws = (WhileStatement) parent;
if (isPassive(ws.getExpression()) && !Boolean.TRUE.equals(ws.getExpression().resolveConstantExpressionValue())) {
return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
}
} else if (parent instanceof DoStatement) {
final DoStatement ds = (DoStatement) parent;
if (isPassive(ds.getExpression()) && !Boolean.TRUE.equals(ds.getExpression().resolveConstantExpressionValue())) {
return maybeRemoveEmptyStmtBody(node, ds, ds.getBody());
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project AutoRefactor by JnRouvignac.
the class AllInOneMethodRatherThanLoopRefactoring method visit.
@Override
public boolean visit(EnhancedForStatement node) {
final Expression iterable = node.getExpression();
final List<Statement> stmts = asList(node.getBody());
if (stmts.size() != 1) {
return VISIT_SUBTREE;
}
final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
final IVariableBinding foreachVariable = node.getParameter().resolveBinding();
// As we replace only one, there should be no more than one occurrence
if (getVariableUseCount(foreachVariable, node.getBody()) == 1) {
if (instanceOf(iterable, "java.util.Collection")) {
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, iterable, "addAll", mi);
} else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, iterable, "removeAll", mi);
}
} else if (isArray(iterable) && isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), iterable) && isSameLocalVariable(foreachVariable, arg0(mi))) {
replaceWithCollectionsAddAll(node, iterable, mi);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
Aggregations