use of org.eclipse.jdt.core.dom.SwitchExpression in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method addMissingDefaultCaseProposal.
public static void addMissingDefaultCaseProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof Expression) {
StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
ASTNode parent = selectedNode.getParent();
List<Statement> statements;
if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
statements = ((SwitchStatement) parent).statements();
} else if (locationInParent == SwitchExpression.EXPRESSION_PROPERTY) {
statements = ((SwitchExpression) parent).statements();
} else {
return;
}
for (Statement statement : statements) {
if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
return;
}
}
createMissingDefaultProposal(context, parent, proposals);
}
}
use of org.eclipse.jdt.core.dom.SwitchExpression 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.SwitchExpression in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getVariableProposals.
public static void getVariableProposals(IInvocationContext context, IProblemLocationCore problem, IVariableBinding resolvedField, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveredNode(astRoot);
if (selectedNode == null) {
return;
}
// type that defines the variable
ITypeBinding binding = null;
ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
if (declaringTypeBinding == null) {
return;
}
// possible type kind of the node
boolean suggestVariableProposals = true;
int typeKind = 0;
while (selectedNode instanceof ParenthesizedExpression) {
selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
}
Name node = null;
switch(selectedNode.getNodeType()) {
case ASTNode.SIMPLE_NAME:
node = (SimpleName) selectedNode;
ASTNode parent = node.getParent();
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == ExpressionMethodReference.EXPRESSION_PROPERTY) {
typeKind = TypeKinds.REF_TYPES;
} else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
if (JavaModelUtil.is1d8OrHigher(cu.getJavaProject())) {
typeKind = TypeKinds.CLASSES | TypeKinds.INTERFACES | TypeKinds.ENUMS;
} else {
typeKind = TypeKinds.CLASSES;
}
} else if (locationInParent == FieldAccess.NAME_PROPERTY) {
Expression expression = ((FieldAccess) parent).getExpression();
if (expression != null) {
binding = expression.resolveTypeBinding();
if (binding == null) {
node = null;
}
}
} else if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
suggestVariableProposals = false;
typeKind = TypeKinds.REF_TYPES_AND_VAR;
} else if (parent instanceof QualifiedName) {
Name qualifier = ((QualifiedName) parent).getQualifier();
if (qualifier != node) {
binding = qualifier.resolveTypeBinding();
} else {
typeKind = TypeKinds.REF_TYPES;
}
ASTNode outerParent = parent.getParent();
while (outerParent instanceof QualifiedName) {
outerParent = outerParent.getParent();
}
if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
typeKind = TypeKinds.REF_TYPES;
suggestVariableProposals = false;
}
} else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY) {
ASTNode caseParent = node.getParent().getParent();
ITypeBinding switchExp = null;
if (caseParent instanceof SwitchStatement) {
switchExp = ((SwitchStatement) caseParent).getExpression().resolveTypeBinding();
} else if (caseParent instanceof SwitchExpression) {
switchExp = ((SwitchExpression) caseParent).getExpression().resolveTypeBinding();
}
if (switchExp != null && switchExp.isEnum()) {
binding = switchExp;
}
} else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
binding = declaringTypeBinding.getSuperclass();
}
break;
case ASTNode.QUALIFIED_NAME:
QualifiedName qualifierName = (QualifiedName) selectedNode;
ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
if (qualifierBinding != null) {
node = qualifierName.getName();
binding = qualifierBinding;
} else {
node = qualifierName.getQualifier();
typeKind = TypeKinds.REF_TYPES;
suggestVariableProposals = node.isSimpleName();
}
if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
typeKind = TypeKinds.REF_TYPES;
suggestVariableProposals = false;
}
break;
case ASTNode.FIELD_ACCESS:
FieldAccess access = (FieldAccess) selectedNode;
Expression expression = access.getExpression();
if (expression != null) {
binding = expression.resolveTypeBinding();
if (binding != null) {
node = access.getName();
}
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
binding = declaringTypeBinding.getSuperclass();
node = ((SuperFieldAccess) selectedNode).getName();
break;
default:
}
if (node == null) {
return;
}
// add type proposals
if (typeKind != 0) {
if (!JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
typeKind &= ~(TypeKinds.ANNOTATIONS | TypeKinds.ENUMS | TypeKinds.VARIABLES);
}
int relevance = Character.isUpperCase(ASTNodes.getSimpleNameIdentifier(node).charAt(0)) ? IProposalRelevance.VARIABLE_TYPE_PROPOSAL_1 : IProposalRelevance.VARIABLE_TYPE_PROPOSAL_2;
addSimilarTypeProposals(typeKind, cu, node, relevance + 1, proposals);
typeKind &= ~TypeKinds.ANNOTATIONS;
addNewTypeProposals(cu, node, typeKind, relevance, proposals);
}
if (!suggestVariableProposals) {
return;
}
SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
boolean isWriteAccess = ASTResolving.isWriteAccess(node);
// similar variables
addSimilarVariableProposals(cu, astRoot, binding, resolvedField, simpleName, isWriteAccess, proposals);
if (binding == null) {
addStaticImportFavoriteProposals(context, simpleName, false, proposals);
}
if (resolvedField == null || binding == null || resolvedField.getDeclaringClass() != binding.getTypeDeclaration() && Modifier.isPrivate(resolvedField.getModifiers())) {
// new fields
addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);
// new parameters and local variables
if (binding == null) {
addNewVariableProposals(cu, node, simpleName, proposals);
}
}
}
use of org.eclipse.jdt.core.dom.SwitchExpression in project eclipse.jdt.ls by eclipse.
the class NavigateToDefinitionHandler method computeBreakContinue.
private Location computeBreakContinue(ITypeRoot typeRoot, int line, int column) throws CoreException {
int offset = JsonRpcHelpers.toOffset(typeRoot.getBuffer(), line, column);
if (offset >= 0) {
CompilationUnit unit = SharedASTProviderCore.getAST(typeRoot, SharedASTProviderCore.WAIT_YES, null);
if (unit == null) {
return null;
}
ASTNode selectedNode = NodeFinder.perform(unit, offset, 0);
ASTNode node = null;
SimpleName label = null;
if (selectedNode instanceof BreakStatement) {
node = selectedNode;
label = ((BreakStatement) node).getLabel();
} else if (selectedNode instanceof ContinueStatement) {
node = selectedNode;
label = ((ContinueStatement) node).getLabel();
} else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof BreakStatement) {
node = selectedNode.getParent();
label = ((BreakStatement) node).getLabel();
} else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof ContinueStatement) {
node = selectedNode.getParent();
label = ((ContinueStatement) node).getLabel();
}
if (node != null) {
ASTNode parent = node.getParent();
ASTNode target = null;
while (parent != null) {
if (parent instanceof MethodDeclaration || parent instanceof Initializer) {
break;
}
if (label == null) {
if (parent instanceof ForStatement || parent instanceof EnhancedForStatement || parent instanceof WhileStatement || parent instanceof DoStatement) {
target = parent;
break;
}
if (node instanceof BreakStatement) {
if (parent instanceof SwitchStatement || parent instanceof SwitchExpression) {
target = parent;
break;
}
}
if (node instanceof LabeledStatement) {
target = parent;
break;
}
} else if (LabeledStatement.class.isInstance(parent)) {
LabeledStatement ls = (LabeledStatement) parent;
if (ls.getLabel().getIdentifier().equals(label.getIdentifier())) {
target = ls;
break;
}
}
parent = parent.getParent();
}
if (target != null) {
int start = target.getStartPosition();
int end = new TokenScanner(unit.getTypeRoot()).getNextEndOffset(node.getStartPosition(), true) - start;
if (start >= 0 && end >= 0) {
return JDTUtils.toLocation((ICompilationUnit) typeRoot, start, end);
}
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.SwitchExpression 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);
}
Aggregations