use of org.eclipse.jdt.core.dom.Statement in project che by eclipse.
the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.
private void addLocalDeclarationSplit(ASTRewrite rewrite) {
VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
ASTNode parentStatement = tempDeclarationStatement.getParent();
ListRewrite listRewrite;
if (parentStatement instanceof SwitchStatement) {
listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
listRewrite = null;
Assert.isTrue(false);
}
int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
Assert.isTrue(statementIndex != -1);
Statement newStatement = createNewAssignmentStatement(rewrite);
List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
Assert.isTrue(fragmentIndex != -1);
if (fragments.size() == 1) {
rewrite.replace(tempDeclarationStatement, newStatement, null);
return;
}
for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
VariableDeclarationFragment fragment = fragments.get(i1);
rewrite.remove(fragment, null);
}
if (fragmentIndex == 0)
rewrite.remove(tempDeclarationStatement, null);
Assert.isTrue(tempHasInitializer());
listRewrite.insertAt(newStatement, statementIndex + 1, null);
if (fragmentIndex + 1 < fragments.size()) {
VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
statement.setType(type);
List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
if (modifiers.size() > 0) {
ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
ASTNode firstModifier = (ASTNode) modifiers.get(0);
ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
statement.modifiers().add(modifiersCopy);
}
for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
VariableDeclarationFragment fragment = fragments.get(i);
VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
statement.fragments().add(fragmentCopy);
}
listRewrite.insertAt(statement, statementIndex + 2, null);
}
}
use of org.eclipse.jdt.core.dom.Statement 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.Statement in project AutoRefactor by JnRouvignac.
the class TryWithResourceRefactoring method newFragment.
private VariableDeclarationFragment newFragment(List<Statement> tryStmts, VariableDeclarationFragment existingFragment, List<ASTNode> nodesToRemove) {
final VariableDefinitionsUsesVisitor visitor = new VariableDefinitionsUsesVisitor(existingFragment).find();
final List<SimpleName> definitions = visitor.getDefinitions();
final ASTBuilder b = ctx.getASTBuilder();
if (!tryStmts.isEmpty()) {
final Statement tryStmt = tryStmts.get(0);
final Assignment assignResource = asExpression(tryStmt, Assignment.class);
if (assignResource != null && isSameVariable(existingFragment, assignResource.getLeftHandSide())) {
nodesToRemove.add(tryStmt);
if (containsOnly(definitions, assignResource.getLeftHandSide(), existingFragment.getName())) {
return b.declareFragment(b.move(existingFragment.getName()), b.move(assignResource.getRightHandSide()));
}
return null;
}
}
return containsOnly(definitions, existingFragment.getName()) ? b.move(existingFragment) : null;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class JUnitAssertRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final List<Statement> stmts = asList(node.getThenStatement());
if (node.getElseStatement() == null && stmts.size() == 1) {
final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
int i = 0;
boolean shouldVisit = VISIT_SUBTREE;
while (shouldVisit == VISIT_SUBTREE && i < PACKAGE_PATHES.length) {
shouldVisit = maybeRefactorIf(node, mi, PACKAGE_PATHES[i]);
i++;
}
return shouldVisit;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class UpdateSetRatherThanTestingFirstRefactoring method maybeReplaceSetContains.
private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpr, final Statement stmt, final Statement oppositeStmt, final boolean negate, final String methodName) {
final List<Statement> stmts = asList(stmt);
final MethodInvocation miContains = as(ifExpr, MethodInvocation.class);
if (!stmts.isEmpty() && isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) {
final Statement firstStmt = getFirst(stmts);
final MethodInvocation miAddOrRemove = asExpression(firstStmt, MethodInvocation.class);
final ASTMatcher astMatcher = new ASTMatcher();
if (isMethod(miAddOrRemove, "java.util.Set", methodName, "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAddOrRemove.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAddOrRemove))) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
if (stmts.size() == 1 && asList(oppositeStmt).isEmpty()) {
// Only one statement: replace if statement with col.add() (or col.remove())
r.replace(ifStmtToReplace, b.move(firstStmt));
return DO_NOT_VISIT_SUBTREE;
} else {
// There are other statements, replace the if condition with col.add() (or col.remove())
r.replace(ifStmtToReplace.getExpression(), negate ? b.negate(miAddOrRemove, ASTBuilder.Copy.MOVE) : b.move(miAddOrRemove));
r.remove(firstStmt);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
Aggregations