use of org.eclipse.jdt.core.dom.ContinueStatement in project che by eclipse.
the class ExtractMethodRefactoring method replaceBranches.
private void replaceBranches(final CompilationUnitChange result) {
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
for (int i = 0; i < selectedNodes.length; i++) {
ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
private LinkedList<String> fOpenLoopLabels = new LinkedList<String>();
private void registerLoopLabel(Statement node) {
String identifier;
if (node.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
identifier = labeledStatement.getLabel().getIdentifier();
} else {
identifier = null;
}
fOpenLoopLabels.add(identifier);
}
@Override
public boolean visit(ForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(ForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(WhileStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(WhileStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(EnhancedForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(EnhancedForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(DoStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(DoStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public void endVisit(ContinueStatement node) {
final SimpleName label = node.getLabel();
if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
result.addTextEditGroup(description);
ReturnStatement rs = fAST.newReturnStatement();
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
}
fRewriter.replace(node, rs, description);
}
}
});
}
}
use of org.eclipse.jdt.core.dom.ContinueStatement 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.ContinueStatement 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.ContinueStatement in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method replaceBranches.
private void replaceBranches(final CompilationUnitChange result) {
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
for (int i = 0; i < selectedNodes.length; i++) {
ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
private LinkedList<String> fOpenLoopLabels = new LinkedList<>();
private void registerLoopLabel(Statement node) {
String identifier;
if (node.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
identifier = labeledStatement.getLabel().getIdentifier();
} else {
identifier = null;
}
fOpenLoopLabels.add(identifier);
}
@Override
public boolean visit(ForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(ForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(WhileStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(WhileStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(EnhancedForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(EnhancedForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(DoStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(DoStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public void endVisit(ContinueStatement node) {
final SimpleName label = node.getLabel();
if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
result.addTextEditGroup(description);
ReturnStatement rs = fAST.newReturnStatement();
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
}
fRewriter.replace(node, rs, description);
}
}
});
}
}
use of org.eclipse.jdt.core.dom.ContinueStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInverseIfContinueIntoIfThenInLoopsProposals.
private static boolean getInverseIfContinueIntoIfThenInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// check that 'then' is 'continue'
if (!(ifStatement.getThenStatement() instanceof ContinueStatement)) {
return false;
}
// check that 'if' statement is statement in block that is body of loop
Block loopBlock = null;
if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof ForStatement) {
loopBlock = (Block) ifStatement.getParent();
} else if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof WhileStatement) {
loopBlock = (Block) ifStatement.getParent();
} else {
return false;
}
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// create inverted 'if' statement
Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(inversedExpression);
// prepare 'then' for new 'if'
Block thenBlock = ast.newBlock();
int ifIndex = loopBlock.statements().indexOf(ifStatement);
for (int i = ifIndex + 1; i < loopBlock.statements().size(); i++) {
Statement statement = (Statement) loopBlock.statements().get(i);
thenBlock.statements().add(rewrite.createMoveTarget(statement));
}
newIf.setThenStatement(thenBlock);
// replace 'if' statement in loop
rewrite.replace(ifStatement, newIf, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfContinue_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_IF_CONTINUE);
resultingCollections.add(proposal);
return true;
}
Aggregations