use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method createMissingCaseProposals.
@SuppressWarnings("deprecation")
public static void createMissingCaseProposals(IInvocationContext context, ASTNode parent, ArrayList<String> enumConstNames, Collection<ChangeCorrectionProposal> proposals) {
List<Statement> statements;
Expression expression;
if (parent instanceof SwitchStatement) {
SwitchStatement switchStatement = (SwitchStatement) parent;
statements = switchStatement.statements();
expression = switchStatement.getExpression();
} else if (parent instanceof SwitchExpression) {
SwitchExpression switchExpression = (SwitchExpression) parent;
statements = switchExpression.statements();
expression = switchExpression.getExpression();
} else {
return;
}
int defaultIndex = statements.size();
for (int i = 0; i < statements.size(); i++) {
Statement curr = statements.get(i);
if (curr instanceof SwitchCase) {
SwitchCase switchCase = (SwitchCase) curr;
if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(switchCase.getAST())) {
if (switchCase.expressions().size() == 0) {
defaultIndex = i;
break;
}
} else if (switchCase.getExpression() == null) {
defaultIndex = i;
break;
}
}
}
boolean hasDefault = defaultIndex < statements.size();
AST ast = parent.getAST();
if (enumConstNames.size() > 0) {
ASTRewrite astRewrite = ASTRewrite.create(ast);
ListRewrite listRewrite;
if (parent instanceof SwitchStatement) {
listRewrite = astRewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
} else {
listRewrite = astRewrite.getListRewrite(parent, SwitchExpression.STATEMENTS_PROPERTY);
}
String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description;
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_CASE_STATEMENTS);
for (int i = 0; i < enumConstNames.size(); i++) {
SwitchCase newSwitchCase = ast.newSwitchCase();
String enumConstName = enumConstNames.get(i);
Name newName = ast.newName(enumConstName);
if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(ast)) {
newSwitchCase.expressions().add(newName);
} else {
newSwitchCase.setExpression(newName);
}
listRewrite.insertAt(newSwitchCase, defaultIndex, null);
defaultIndex++;
if (!hasDefault) {
if (ASTHelper.isSwitchExpressionNodeSupportedInAST(ast)) {
if (statements.size() > 0) {
Statement firstStatement = statements.get(0);
SwitchCase switchCase = (SwitchCase) firstStatement;
boolean isArrow = switchCase.isSwitchLabeledRule();
newSwitchCase.setSwitchLabeledRule(isArrow);
if (isArrow || parent instanceof SwitchExpression) {
ThrowStatement newThrowStatement = getThrowForUnsupportedCase(expression, ast, astRewrite);
listRewrite.insertLast(newThrowStatement, null);
proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, enumConstName);
} else {
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
} else {
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
} else {
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
defaultIndex++;
}
}
if (!hasDefault) {
SwitchCase newSwitchCase = ast.newSwitchCase();
listRewrite.insertAt(newSwitchCase, defaultIndex, null);
defaultIndex++;
if (ASTHelper.isSwitchExpressionNodeSupportedInAST(ast)) {
if (statements.size() > 0) {
Statement firstStatement = statements.get(0);
SwitchCase switchCase = (SwitchCase) firstStatement;
boolean isArrow = switchCase.isSwitchLabeledRule();
newSwitchCase.setSwitchLabeledRule(isArrow);
if (isArrow || parent instanceof SwitchExpression) {
ThrowStatement newThrowStatement = getThrowForUnexpectedDefault(expression, ast, astRewrite);
listRewrite.insertLast(newThrowStatement, null);
// $NON-NLS-1$
proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, "defaultCase");
} else {
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
} else {
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
} else {
newSwitchCase.setExpression(null);
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
}
proposals.add(proposal);
}
if (!hasDefault) {
createMissingDefaultProposal(context, parent, proposals);
}
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method addCasesOmittedProposals.
public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
AST ast = selectedNode.getAST();
SwitchStatement parent = (SwitchStatement) selectedNode.getParent();
for (Statement statement : (List<Statement>) parent.statements()) {
if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
// insert //$CASES-OMITTED$:
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
// $NON-NLS-1$
ASTNode casesOmittedComment = rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT);
listRewrite.insertBefore(casesOmittedComment, statement, null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED);
proposals.add(proposal);
break;
}
}
}
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.
public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
ASTNode parent = selectedNode.getParent();
while (parent instanceof ExpressionStatement) {
selectedNode = parent;
parent = selectedNode.getParent();
}
if (parent instanceof WhileStatement) {
addRemoveIncludingConditionProposal(context, parent, null, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
Statement elseStatement = ((IfStatement) parent).getElseStatement();
addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
Statement thenStatement = ((IfStatement) parent).getThenStatement();
addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
} else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
Statement body = ((ForStatement) parent).getBody();
addRemoveIncludingConditionProposal(context, parent, body, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
} else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
// also offer split && / || condition proposals:
InfixExpression infixExpression = (InfixExpression) parent;
Expression leftOperand = infixExpression.getLeftOperand();
ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
Expression replacement = leftOperand;
while (replacement instanceof ParenthesizedExpression) {
replacement = ((ParenthesizedExpression) replacement).getExpression();
}
Expression toReplace = infixExpression;
while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = (Expression) toReplace.getParent();
}
if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
if (leftOperand instanceof ParenthesizedExpression) {
replacement = (Expression) replacement.getParent();
} else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = ((ParenthesizedExpression) toReplace).getExpression();
}
}
rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
addRemoveProposal(context, rewrite, label, proposals);
InnovationContext assistContext = new InnovationContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
assistContext.setASTRoot(root);
InvertBooleanUtility.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
InvertBooleanUtility.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
} else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
// remove all statements following the unreachable:
List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
int idx = statements.indexOf(selectedNode);
ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
if (idx > 0) {
Object prevStatement = statements.get(idx - 1);
if (prevStatement instanceof IfStatement) {
IfStatement ifStatement = (IfStatement) prevStatement;
if (ifStatement.getElseStatement() == null) {
// remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
}
}
}
for (int i = idx; i < statements.size(); i++) {
ASTNode statement = statements.get(i);
if (statement instanceof SwitchCase) {
// stop at case *: and default:
break;
}
rewrite.remove(statement, null);
}
addRemoveProposal(context, rewrite, label, proposals);
} else {
// no special case, just remove the node:
addRemoveProposal(context, selectedNode, proposals);
}
}
use of org.eclipse.jdt.core.dom.SwitchCase in project flux by eclipse.
the class AdvancedQuickAssistProcessor method createSwitchCaseStatements.
private static SwitchCase[] createSwitchCaseStatements(AST ast, ASTRewrite rewrite, List<Expression> caseExpressions) {
int len = (caseExpressions.size() == 0) ? 1 : caseExpressions.size();
SwitchCase[] switchCaseStatements = new SwitchCase[len];
if (caseExpressions.size() == 0) {
switchCaseStatements[0] = ast.newSwitchCase();
switchCaseStatements[0].setExpression(null);
} else {
for (int i = 0; i < caseExpressions.size(); i++) {
ASTNode astNode = caseExpressions.get(i);
switchCaseStatements[i] = ast.newSwitchCase();
switchCaseStatements[i].setExpression((Expression) rewrite.createCopyTarget(astNode));
}
}
return switchCaseStatements;
}
use of org.eclipse.jdt.core.dom.SwitchCase in project che by eclipse.
the class ExtractTempRefactoring method canReplace.
private static boolean canReplace(IASTFragment fragment) {
ASTNode node = fragment.getAssociatedNode();
ASTNode parent = node.getParent();
if (parent instanceof VariableDeclarationFragment) {
VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
if (node.equals(vdf.getName()))
return false;
}
if (isMethodParameter(node))
return false;
if (isThrowableInCatchBlock(node))
return false;
if (parent instanceof ExpressionStatement)
return false;
if (isLeftValue(node))
return false;
if (isReferringToLocalVariableFromFor((Expression) node))
return false;
if (isUsedInForInitializerOrUpdater((Expression) node))
return false;
if (parent instanceof SwitchCase)
return false;
return true;
}
Aggregations