use of org.eclipse.jdt.core.dom.ForStatement 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.ForStatement in project AutoRefactor by JnRouvignac.
the class HotSpotIntrinsicedAPIsRefactoring method visit.
@Override
public boolean visit(ForStatement node) {
final SystemArrayCopyParams params = new SystemArrayCopyParams();
collectUniqueIndex(node, params);
final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
final List<Statement> stmts = asList(node.getBody());
if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
collectLength(node.getExpression(), incrementedIdx, params);
final Assignment as = asExpression(stmts.get(0), Assignment.class);
if (hasOperator(as, ASSIGN)) {
final Expression lhs = as.getLeftHandSide();
final Expression rhs = as.getRightHandSide();
if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
final ArrayAccess aaLHS = (ArrayAccess) lhs;
final ArrayAccess aaRHS = (ArrayAccess) rhs;
params.destArrayExpr = aaLHS.getArray();
params.srcArrayExpr = aaRHS.getArray();
if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
params.destPos = calcIndex(aaLHS.getIndex(), params);
params.srcPos = calcIndex(aaRHS.getIndex(), params);
return replaceWithSystemArrayCopyCloneAll(node, params);
}
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.ForStatement 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.ForStatement 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.ForStatement in project che by eclipse.
the class ConvertForLoopQuickFixTest method getForStatement.
private static ForStatement getForStatement(ICompilationUnit cu) {
CompilationUnit ast = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, new NullProgressMonitor());
final ForStatement[] statement = new ForStatement[1];
ast.accept(new GenericVisitor() {
protected boolean visitNode(ASTNode node) {
if (node instanceof ForStatement) {
statement[0] = (ForStatement) node;
return false;
} else {
return super.visitNode(node);
}
}
});
return statement[0];
}
Aggregations