use of org.eclipse.jdt.core.dom.Statement 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.Statement in project AutoRefactor by JnRouvignac.
the class StringBuilderRefactoring method maybeRefactorAppending.
private boolean maybeRefactorAppending(Expression node) {
final LinkedList<Pair<ITypeBinding, Expression>> allAppendedStrings = new LinkedList<Pair<ITypeBinding, Expression>>();
final AtomicBoolean isRefactoringNeeded = new AtomicBoolean(false);
final AtomicBoolean isInstanceCreationToRewrite = new AtomicBoolean(false);
final Expression lastExpr = readAppendMethod(node, allAppendedStrings, isRefactoringNeeded, isInstanceCreationToRewrite);
if (lastExpr != null) {
removeEmptyStrings(allAppendedStrings, isRefactoringNeeded);
removeCallsToToString(allAppendedStrings, isRefactoringNeeded, isInstanceCreationToRewrite.get());
if (isRefactoringNeeded.get()) {
if (allAppendedStrings.isEmpty() && isVariable(lastExpr) && node.getParent() instanceof Statement) {
ctx.getRefactorings().remove(node.getParent());
} else {
replaceWithNewStringAppends(node, allAppendedStrings, lastExpr, isInstanceCreationToRewrite.get());
}
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method canVarOccurrenceBeRefactored0.
private boolean canVarOccurrenceBeRefactored0(final Block node, final List<VariableDeclaration> varDecls, final List<MethodInvocation> methodCallsToRefactor, final List<VariableDeclaration> otherVarDecls) {
for (final VariableDeclaration varDecl : varDecls) {
final VarOccurrenceVisitor varOccurrenceVisitor = new VarOccurrenceVisitor(varDecl);
final Statement parent = getAncestorOrNull(varDecl, Statement.class);
Statement nextSibling = getNextSibling(parent);
while (nextSibling != null) {
varOccurrenceVisitor.visitNode(nextSibling);
nextSibling = getNextSibling(nextSibling);
}
if (varOccurrenceVisitor.isUsedInAnnonymousClass()) {
return false;
}
for (final SimpleName varOccurrence : varOccurrenceVisitor.getVarOccurrences()) {
final List<VariableDeclaration> subVarDecls = new ArrayList<VariableDeclaration>();
if (!canBeRefactored(node, varOccurrence, varOccurrence.resolveTypeBinding(), subVarDecls, methodCallsToRefactor)) {
return false;
}
otherVarDecls.addAll(subVarDecls);
}
}
return true;
}
use of org.eclipse.jdt.core.dom.Statement 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;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method visitIfStatement.
private boolean visitIfStatement(final IfStatement node) {
final BooleanASTMatcher matcher = new BooleanASTMatcher();
if (match(matcher, node.getThenStatement(), node.getElseStatement())) {
// Then and else statement are matching, bar the boolean values
// which are opposite
final Statement copyStmt = b.copySubtree(node.getThenStatement());
// identify the node that need to be replaced after the copy
final BooleanASTMatcher matcher2 = new BooleanASTMatcher(matcher.matches);
if (match(matcher2, copyStmt, node.getElseStatement())) {
final Expression ifCondition = node.getExpression();
copyStmt.accept(new BooleanReplaceVisitor(ifCondition, matcher2.matches.values(), getBooleanName(node)));
// make sure to keep curly braces if the node is an else statement
ctx.getRefactorings().replace(node, isElseStatementOfParent(node) ? copyStmt : toSingleStmt(copyStmt));
return DO_NOT_VISIT_SUBTREE;
}
}
final ReturnStatement thenRs = as(node.getThenStatement(), ReturnStatement.class);
if (thenRs != null) {
final ReturnStatement elseRs = as(node.getElseStatement() != null ? node.getElseStatement() : getNextSibling(node), ReturnStatement.class);
if (elseRs != null) {
return withThenReturnStmt(node, thenRs, elseRs);
}
return VISIT_SUBTREE;
} else {
return noThenReturnStmt(node);
}
}
Aggregations