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 AutoRefactor by JnRouvignac.
the class IfRatherThanWhileAndFallsThroughRefactoring method visit.
@Override
public boolean visit(WhileStatement node) {
if (isEndingWithExit(node.getBody())) {
final BreakVisitor breakVisitor = new BreakVisitor(node);
breakVisitor.visitNode(node);
if (breakVisitor.canBeRefactored()) {
final ASTBuilder b = ctx.getASTBuilder();
for (final BreakStatement breakStmt : breakVisitor.getBreaks()) {
ctx.getRefactorings().remove(breakStmt);
}
ctx.getRefactorings().replace(node, b.if0(b.copy(node.getExpression()), b.copy(node.getBody())));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.BreakStatement in project AutoRefactor by JnRouvignac.
the class CFGBuilder method buildCFG.
/**
* Builds a CFG for the provided node.
*
* @param node the node for which to build a CFG.
* @param state the blocks liveness state before current node
* @param throwers the thrower blocks information
* @return the blocks liveness state after current node
*/
public LivenessState buildCFG(BreakStatement node, LivenessState state, ThrowerBlocks throwers) {
final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
final Statement targetStmt;
if (node.getLabel() != null) {
targetStmt = findLabeledParentStmt(node);
} else {
targetStmt = findBreakableParentStmt(node);
}
addEdgeToBuild(targetStmt, new CFGEdgeBuilder(basicBlock), true);
return state.copyLiveBasicBlock();
}
use of org.eclipse.jdt.core.dom.BreakStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method copyStatementExceptBreak.
private static Statement copyStatementExceptBreak(AST ast, ASTRewrite rewrite, Statement source) {
if (source instanceof Block) {
Block block = (Block) source;
Block newBlock = ast.newBlock();
for (Iterator<Statement> iter = block.statements().iterator(); iter.hasNext(); ) {
Statement statement = iter.next();
if (statement instanceof BreakStatement) {
continue;
}
newBlock.statements().add(copyStatementExceptBreak(ast, rewrite, statement));
}
return newBlock;
}
return (Statement) rewrite.createMoveTarget(source);
}
use of org.eclipse.jdt.core.dom.BreakStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteIfRatherThanWhileAndFallsThroughCleanUp method replaceByIf.
private void replaceByIf(final WhileStatement visited, final BreakVisitor breakVisitor) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfRatherThanWhileAndFallsThroughCleanUp_description);
for (BreakStatement breakStatement : breakVisitor.getBreaks()) {
if (ASTNodes.canHaveSiblings(breakStatement) || breakStatement.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
rewrite.remove(breakStatement, group);
} else {
ASTNodes.replaceButKeepComment(rewrite, breakStatement, ast.newBlock(), group);
}
}
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())));
newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, visited.getBody()));
ASTNodes.replaceButKeepComment(rewrite, visited, newIfStatement, group);
}
Aggregations