use of org.eclipse.jdt.core.dom.LabeledStatement in project che by eclipse.
the class SourceProvider method isDangligIf.
public boolean isDangligIf() {
List<Statement> statements = fDeclaration.getBody().statements();
if (statements.size() != 1)
return false;
ASTNode p = statements.get(0);
while (true) {
if (p instanceof IfStatement) {
return ((IfStatement) p).getElseStatement() == null;
} else {
ChildPropertyDescriptor childD;
if (p instanceof WhileStatement) {
childD = WhileStatement.BODY_PROPERTY;
} else if (p instanceof ForStatement) {
childD = ForStatement.BODY_PROPERTY;
} else if (p instanceof EnhancedForStatement) {
childD = EnhancedForStatement.BODY_PROPERTY;
} else if (p instanceof DoStatement) {
childD = DoStatement.BODY_PROPERTY;
} else if (p instanceof LabeledStatement) {
childD = LabeledStatement.BODY_PROPERTY;
} else {
return false;
}
Statement body = (Statement) p.getStructuralProperty(childD);
if (body instanceof Block) {
return false;
} else {
p = body;
}
}
}
}
use of org.eclipse.jdt.core.dom.LabeledStatement in project che by eclipse.
the class CallInliner method initializeInsertionPoint.
private void initializeInsertionPoint(int nos) {
fInsertionIndex = -1;
fNeedsStatement = false;
// if we have a constructor invocation the invocation itself is already a statement
ASTNode parentStatement = fInvocation instanceof Statement ? fInvocation : ASTNodes.getParent(fInvocation, Statement.class);
if (parentStatement == null)
return;
ASTNode container = parentStatement.getParent();
int type = container.getNodeType();
if (type == ASTNode.BLOCK) {
Block block = (Block) container;
fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
} else if (type == ASTNode.SWITCH_STATEMENT) {
SwitchStatement switchStatement = (SwitchStatement) container;
fListRewrite = fRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
} else if (isControlStatement(container) || type == ASTNode.LABELED_STATEMENT) {
fNeedsStatement = true;
if (nos > 1 || needsBlockAroundDanglingIf()) {
Block block = fInvocation.getAST().newBlock();
fInsertionIndex = 0;
Statement currentStatement = null;
switch(type) {
case ASTNode.LABELED_STATEMENT:
currentStatement = ((LabeledStatement) container).getBody();
break;
case ASTNode.FOR_STATEMENT:
currentStatement = ((ForStatement) container).getBody();
break;
case ASTNode.ENHANCED_FOR_STATEMENT:
currentStatement = ((EnhancedForStatement) container).getBody();
break;
case ASTNode.WHILE_STATEMENT:
currentStatement = ((WhileStatement) container).getBody();
break;
case ASTNode.DO_STATEMENT:
currentStatement = ((DoStatement) container).getBody();
break;
case ASTNode.IF_STATEMENT:
IfStatement node = (IfStatement) container;
Statement thenPart = node.getThenStatement();
if (fTargetNode == thenPart || ASTNodes.isParent(fTargetNode, thenPart)) {
currentStatement = thenPart;
} else {
currentStatement = node.getElseStatement();
}
break;
}
Assert.isNotNull(currentStatement);
fRewrite.replace(currentStatement, block, null);
fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
// The method to be inlined is not the body of the control statement.
if (currentStatement != fTargetNode) {
fListRewrite.insertLast(fRewrite.createCopyTarget(currentStatement), null);
} else {
// We can't replace a copy with something else. So we
// have to insert all statements to be inlined.
fTargetNode = null;
}
}
}
// We only insert one new statement or we delete the existing call.
// So there is no need to have an insertion index.
}
use of org.eclipse.jdt.core.dom.LabeledStatement 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.LabeledStatement 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.LabeledStatement 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];
}
Aggregations