use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse-cs by checkstyle.
the class MissingSwitchDefaultQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(SwitchStatement node) {
if (containsPosition(lineInfo, node.getStartPosition())) {
SwitchCase defNode = node.getAST().newSwitchCase();
defNode.setExpression(null);
node.statements().add(defNode);
node.statements().add(node.getAST().newBreakStatement());
}
// also visit children
return true;
}
};
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method createMissingDefaultProposal.
@SuppressWarnings("deprecation")
private static void createMissingDefaultProposal(IInvocationContext context, ASTNode parent, 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;
}
AST ast = parent.getAST();
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_default_case_description;
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE);
SwitchCase newSwitchCase = ast.newSwitchCase();
listRewrite.insertLast(newSwitchCase, null);
if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(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);
proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, null);
} else {
listRewrite.insertLast(ast.newBreakStatement(), null);
}
} else {
listRewrite.insertLast(ast.newBreakStatement(), null);
}
} else {
newSwitchCase.setExpression(null);
listRewrite.insertLast(ast.newBreakStatement(), null);
}
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method evaluateMissingSwitchCases.
@SuppressWarnings("deprecation")
public static boolean evaluateMissingSwitchCases(ITypeBinding enumBindings, List<Statement> switchStatements, ArrayList<String> enumConstNames) {
for (IVariableBinding field : enumBindings.getDeclaredFields()) {
if (field.isEnumConstant()) {
enumConstNames.add(field.getName());
}
}
boolean hasDefault = false;
List<Statement> statements = switchStatements;
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())) {
List<Expression> expressions = switchCase.expressions();
if (expressions.size() == 0) {
hasDefault = true;
} else {
for (Expression expression : expressions) {
if (expression instanceof SimpleName) {
enumConstNames.remove(((SimpleName) expression).getFullyQualifiedName());
}
}
}
} else {
Expression expression = ((SwitchCase) curr).getExpression();
if (expression instanceof SimpleName) {
enumConstNames.remove(((SimpleName) expression).getFullyQualifiedName());
} else if (expression == null) {
hasDefault = true;
}
}
}
}
return hasDefault;
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.
private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
if (covering instanceof Block) {
List<Statement> statements = ((Block) covering).statements();
int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
if (startIndex == -1 || startIndex >= statements.size()) {
return false;
}
covering = statements.get(startIndex);
} else {
while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
covering = covering.getParent();
}
}
SwitchStatement switchStatement;
if (covering instanceof SwitchStatement) {
switchStatement = (SwitchStatement) covering;
} else {
return false;
}
SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
if (fix == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
// add correction proposal
try {
CompilationUnitChange change = fix.createChange(null);
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
resultingCollections.add(proposal);
} catch (CoreException e) {
// continue
}
return true;
}
Aggregations