use of org.eclipse.jdt.core.dom.BreakStatement in project che by eclipse.
the class ExtractMethodAnalyzer method canHandleBranches.
private String canHandleBranches() {
if (fReturnValue != null)
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
ASTNode[] selectedNodes = getSelectedNodes();
final ASTNode lastSelectedNode = selectedNodes[selectedNodes.length - 1];
Statement body = getParentLoopBody(lastSelectedNode.getParent());
if (!(body instanceof Block))
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
if (body != lastSelectedNode) {
Block block = (Block) body;
List<Statement> statements = block.statements();
ASTNode lastStatementInLoop = statements.get(statements.size() - 1);
if (lastSelectedNode != lastStatementInLoop)
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
final String[] continueMatchesLoopProblem = { null };
for (int i = 0; i < selectedNodes.length; i++) {
final ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
ArrayList<String> fLocalLoopLabels = new ArrayList<String>();
@Override
public boolean visit(BreakStatement node) {
SimpleName label = node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_break_mismatch, //$NON-NLS-1$
new Object[] { ("break " + label.getIdentifier()) });
}
return false;
}
@Override
public boolean visit(LabeledStatement node) {
SimpleName label = node.getLabel();
if (label != null)
fLocalLoopLabels.add(label.getIdentifier());
return true;
}
@Override
public void endVisit(ContinueStatement node) {
SimpleName label = node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
if (fEnclosingLoopLabel == null || !label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_continue_mismatch, //$NON-NLS-1$
new Object[] { "continue " + label.getIdentifier() });
}
}
}
});
}
return continueMatchesLoopProblem[0];
}
use of org.eclipse.jdt.core.dom.BreakStatement in project AutoRefactor by JnRouvignac.
the class IfRatherThanWhileAndFallsThroughRefactoring method isEndingWithExit.
/**
* Return true if the statement falls through.
*
* @param stmt the statement
* @return true if the statement falls through.
*/
private boolean isEndingWithExit(final Statement stmt) {
final List<Statement> stmts = asList(stmt);
if (stmts.isEmpty()) {
return false;
}
final Statement lastStmt = stmts.get(stmts.size() - 1);
switch(lastStmt.getNodeType()) {
case RETURN_STATEMENT:
case THROW_STATEMENT:
return true;
case BREAK_STATEMENT:
final BreakStatement breakStmt = (BreakStatement) lastStmt;
return breakStmt.getLabel() == null;
case IF_STATEMENT:
final IfStatement ifStmt = (IfStatement) lastStmt;
final Statement thenStmt = ifStmt.getThenStatement();
final Statement elseStmt = ifStmt.getElseStatement();
return isEndingWithExit(thenStmt) && isEndingWithExit(elseStmt);
default:
return false;
}
}
use of org.eclipse.jdt.core.dom.BreakStatement in project eclipse.jdt.ls by eclipse.
the class ExtractMethodAnalyzer method canHandleBranches.
private String canHandleBranches() {
if (fReturnValue != null) {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
ASTNode[] selectedNodes = getSelectedNodes();
final ASTNode lastSelectedNode = selectedNodes[selectedNodes.length - 1];
Statement body = getParentLoopBody(lastSelectedNode.getParent());
if (!(body instanceof Block)) {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
if (body != lastSelectedNode) {
Block block = (Block) body;
List<Statement> statements = block.statements();
ASTNode lastStatementInLoop = statements.get(statements.size() - 1);
if (lastSelectedNode != lastStatementInLoop) {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
}
final String[] continueMatchesLoopProblem = { null };
for (int i = 0; i < selectedNodes.length; i++) {
final ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
ArrayList<String> fLocalLoopLabels = new ArrayList<>();
@Override
public boolean visit(BreakStatement node) {
SimpleName label = node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
// $NON-NLS-1$
continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_break_mismatch, new Object[] { ("break " + label.getIdentifier()) });
}
return false;
}
@Override
public boolean visit(LabeledStatement node) {
SimpleName label = node.getLabel();
if (label != null) {
fLocalLoopLabels.add(label.getIdentifier());
}
return true;
}
@Override
public void endVisit(ContinueStatement node) {
SimpleName label = node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
if (fEnclosingLoopLabel == null || !label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
// $NON-NLS-1$
continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_continue_mismatch, new Object[] { "continue " + label.getIdentifier() });
}
}
}
});
}
return continueMatchesLoopProblem[0];
}
use of org.eclipse.jdt.core.dom.BreakStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method hasStopAsLastExecutableStatement.
private static boolean hasStopAsLastExecutableStatement(Statement lastStatement) {
if (lastStatement instanceof ReturnStatement || lastStatement instanceof BreakStatement) {
return true;
}
if (lastStatement instanceof Block) {
Block block = (Block) lastStatement;
lastStatement = (Statement) block.statements().get(block.statements().size() - 1);
return hasStopAsLastExecutableStatement(lastStatement);
}
return false;
}
use of org.eclipse.jdt.core.dom.BreakStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method visit.
@SuppressWarnings("deprecation")
@Override
public boolean visit(final SwitchStatement visited) {
List<?> statements = visited.statements();
if (statements.isEmpty()) {
return true;
}
Set<SimpleName> previousVarIds = new HashSet<>();
Set<SimpleName> caseVarIds = new HashSet<>();
List<Pair<List<Expression>, List<Statement>>> switchStructure = new ArrayList<>();
List<Expression> caseExprs = new ArrayList<>();
List<Statement> caseStatements = new ArrayList<>();
boolean isPreviousStmtACase = true;
int caseIndexWithDefault = -1;
for (Object object : statements) {
Statement statement = (Statement) object;
if (statement instanceof SwitchCase) {
if (!isPreviousStmtACase) {
if (switchStructure.size() > 2) {
return true;
}
previousVarIds.addAll(caseVarIds);
caseVarIds.clear();
switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
caseExprs = new ArrayList<>();
caseStatements = new ArrayList<>();
}
if (((SwitchCase) statement).isDefault()) {
caseIndexWithDefault = switchStructure.size();
} else {
caseExprs.add(((SwitchCase) statement).getExpression());
}
isPreviousStmtACase = true;
} else {
VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(previousVarIds, false);
varOccurrenceVisitor.traverseNodeInterruptibly(statement);
if (varOccurrenceVisitor.isVarConflicting()) {
return true;
}
caseVarIds.addAll(ASTNodes.getLocalVariableIdentifiers(statement, false));
caseStatements.add(statement);
isPreviousStmtACase = false;
}
}
switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
if (caseIndexWithDefault != -1) {
Pair<List<Expression>, List<Statement>> caseWithDefault = switchStructure.remove(caseIndexWithDefault);
switchStructure.add(caseWithDefault);
}
if (switchStructure.size() > 2 || !switchStructure.get(switchStructure.size() - 1).getFirst().isEmpty() && !ASTNodes.isPassive(visited.getExpression())) {
return true;
}
List<BreakStatement> overBreaks = new ArrayList<>();
for (int i = 0; i < switchStructure.size(); i++) {
Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
if (!caseStructure.getSecond().isEmpty()) {
Statement lastStatement = caseStructure.getSecond().get(caseStructure.getSecond().size() - 1);
if (i < switchStructure.size() - 1 && !ASTNodes.fallsThrough(lastStatement)) {
return true;
}
BreakStatement breakStatement = ASTNodes.as(lastStatement, BreakStatement.class);
if (breakStatement != null && breakStatement.getLabel() == null) {
caseStructure.getSecond().remove(caseStructure.getSecond().size() - 1);
}
} else if (i < switchStructure.size() - 1) {
return true;
}
}
for (Pair<List<Expression>, List<Statement>> caseStructure : switchStructure) {
for (Statement oneStatement : caseStructure.getSecond()) {
BreakVisitor breakVisitor = new BreakVisitor();
breakVisitor.traverseNodeInterruptibly(oneStatement);
if (!breakVisitor.canBeRefactored()) {
return true;
}
overBreaks.addAll(breakVisitor.getBreaks());
}
}
replaceBySwitch(visited, switchStructure, caseIndexWithDefault, overBreaks);
return false;
}
Aggregations