use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class ASTHelper method getNextStatement.
/**
* Returns the next statement in the source file if it exists.
*
* @param startNode the start node
* @return the next statement in the source file if it exists, null otherwise
*/
public static Statement getNextStatement(Statement startNode) {
final Statement nextSibling = getNextSibling(startNode);
if (nextSibling != null) {
return nextSibling;
}
final ASTNode parent = startNode.getParent();
if (parent instanceof Statement) {
return getNextStatement((Statement) parent);
}
return null;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class NoSettingRatherThanUselessSettingRefactoring method visit.
@Override
public boolean visit(VariableDeclarationStatement node) {
if (node.fragments() != null && node.fragments().size() == 1) {
final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.fragments().get(0);
if (fragment.getInitializer() != null && fragment.getInitializer().resolveConstantExpressionValue() != null) {
final IVariableBinding variable = fragment.resolveBinding();
Statement stmtToInspect = getNextSibling(node);
boolean isOverridden = false;
boolean isRead = false;
while (stmtToInspect != null && !isOverridden && !isRead) {
if (stmtToInspect instanceof ExpressionStatement) {
final Assignment assignment = asExpression(stmtToInspect, Assignment.class);
isOverridden = hasOperator(assignment, ASSIGN) && isSameVariable(fragment.getName(), assignment.getLeftHandSide());
}
isRead = !new VariableDefinitionsUsesVisitor(variable, stmtToInspect).find().getUses().isEmpty();
stmtToInspect = getNextSibling(stmtToInspect);
}
if (isOverridden && !isRead) {
ctx.getRefactorings().remove(fragment.getInitializer());
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class RemoveEmptyIfRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Refactorings r = this.ctx.getRefactorings();
final Statement thenStmt = node.getThenStatement();
final Statement elseStmt = node.getElseStatement();
if (elseStmt != null && asList(elseStmt).isEmpty()) {
r.remove(elseStmt);
return DO_NOT_VISIT_SUBTREE;
} else if (thenStmt != null && asList(thenStmt).isEmpty()) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Expression condition = node.getExpression();
if (elseStmt != null) {
r.replace(node, b.if0(b.negate(condition), b.move(elseStmt)));
} else if (isPassive(condition)) {
removeBlock(node, r, b);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method mergeCases.
private void mergeCases(Merge merge, SwitchCaseSection sectionToKeep, SwitchCaseSection sectionToRemove) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
final Statement caseKept;
if (merge == Merge.BEFORE_SWITCH_CASES) {
caseKept = sectionToKeep.existingCases.get(0);
} else {
// move == Move.AFTER_SWITCH_CASES
caseKept = sectionToKeep.stmts.get(0);
}
for (final SwitchCase caseToMove : sectionToRemove.existingCases) {
r.insertBefore(b.move(caseToMove), caseKept);
}
r.remove(sectionToRemove.stmts);
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method visit.
@Override
public boolean visit(final IfStatement node) {
if (hasUnlabeledBreak(node)) {
return VISIT_SUBTREE;
}
Variable variable = extractVariableAndValues(node);
if (variable == null) {
return VISIT_SUBTREE;
}
final SimpleName switchExpr = variable.name;
final List<SwitchCaseSection> cases = new ArrayList<SwitchCaseSection>();
Statement remainingStmt = null;
final Set<String> variableDeclarationIds = new HashSet<String>();
IfStatement currentNode = node;
while (haveSameIdentifier(switchExpr, variable.name) && haveSameType(switchExpr, variable.name)) {
if (detectDeclarationConflicts(currentNode.getThenStatement(), variableDeclarationIds)) {
// Cannot declare two variables with the same name in two cases
return VISIT_SUBTREE;
}
cases.add(new SwitchCaseSection(variable.constantValues, asList(currentNode.getThenStatement())));
remainingStmt = currentNode.getElseStatement();
variable = extractVariableAndValues(remainingStmt);
if (variable == null) {
break;
}
currentNode = (IfStatement) remainingStmt;
}
final List<SwitchCaseSection> filteredCases = filterDuplicateCaseValues(cases);
return maybeReplaceWithSwitchStmt(node, switchExpr, filteredCases, remainingStmt);
}
Aggregations