use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method replaceBySwitch.
private void replaceBySwitch(final SwitchStatement visited, final List<Pair<List<Expression>, List<Statement>>> switchStructure, final int caseIndexWithDefault, final List<BreakStatement> overBreaks) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfRatherThanTwoSwitchCasesCleanUp_description);
List<Block> newBlocks = prepareNewBlocks(rewrite, ast, switchStructure);
for (BreakStatement breakStatement : overBreaks) {
rewrite.remove(breakStatement, group);
}
int localCaseIndexWithDefault = caseIndexWithDefault;
Expression discriminant = visited.getExpression();
Statement currentBlock = null;
for (int i = switchStructure.size() - 1; i >= 0; i--) {
Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
Expression newCondition = buildNewCondition(rewrite, ast, discriminant, caseStructure);
if (currentBlock != null) {
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression(newCondition);
newIfStatement.setThenStatement(newBlocks.get(i));
newIfStatement.setElseStatement(currentBlock);
currentBlock = newIfStatement;
} else if (caseStructure.getSecond().isEmpty()) {
localCaseIndexWithDefault = -1;
} else if (localCaseIndexWithDefault == -1) {
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression(newCondition);
newIfStatement.setThenStatement(newBlocks.get(i));
currentBlock = newIfStatement;
} else {
currentBlock = newBlocks.get(i);
}
}
ASTNodes.replaceButKeepComment(rewrite, visited, currentBlock, group);
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteSwitchCleanUp method addCaseWithStatements.
private void addCaseWithStatements(final SwitchStatement switchStatement, final List<Expression> caseValuesOrNullForDefault, final List<Statement> innerStatements) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
List<Statement> switchStatements = switchStatement.statements();
// Add the case statement(s)
if (caseValuesOrNullForDefault != null) {
for (Expression caseValue : caseValuesOrNullForDefault) {
switchStatements.add(ast.newSwitchCase(ASTNodes.createMoveTarget(rewrite, caseValue)));
}
} else {
switchStatements.add(ast.default0());
}
// Add the statement(s) for this case(s)
boolean isBreakNeeded = true;
if (!innerStatements.isEmpty()) {
for (Statement statement : innerStatements) {
switchStatements.add(ASTNodes.createMoveTarget(rewrite, statement));
}
isBreakNeeded = !ASTNodes.fallsThrough(innerStatements.get(innerStatements.size() - 1));
}
// When required: end with a break
if (isBreakNeeded) {
switchStatements.add(ast.newBreakStatement());
}
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project eclipse-pmd by acanda.
the class DefaultLabelNotLastInSwitchStmtQuickFix method apply.
/**
* Moves the default case to the last position. The default case includes the default {@code SwitchCase} and all
* following statements up to the next {@code SwitchCase}.
*/
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final SwitchStatement node) {
final List<Statement> statements = node.statements();
final List<Statement> defaultCaseStatements = new ArrayList<>(statements.size());
boolean isDefaultCaseStatement = false;
for (final Statement statement : statements) {
if (statement instanceof SwitchCase) {
if (((SwitchCase) statement).getExpression() == DEFAULT_LABEL) {
isDefaultCaseStatement = true;
} else {
isDefaultCaseStatement = false;
}
}
if (isDefaultCaseStatement) {
defaultCaseStatements.add(statement);
}
}
statements.removeAll(defaultCaseStatements);
statements.addAll(defaultCaseStatements);
return true;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project che by eclipse.
the class ExtractMethodAnalyzer method getParentLoopBody.
private Statement getParentLoopBody(ASTNode node) {
Statement stmt = null;
ASTNode start = node;
while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
start = start.getParent();
}
if (start instanceof ForStatement) {
stmt = ((ForStatement) start).getBody();
} else if (start instanceof DoStatement) {
stmt = ((DoStatement) start).getBody();
} else if (start instanceof WhileStatement) {
stmt = ((WhileStatement) start).getBody();
} else if (start instanceof EnhancedForStatement) {
stmt = ((EnhancedForStatement) start).getBody();
}
if (start != null && start.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
fEnclosingLoopLabel = labeledStatement.getLabel();
}
return stmt;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project che by eclipse.
the class UnresolvedElementsSubProcessor method getVariableProposals.
public static void getVariableProposals(IInvocationContext context, IProblemLocation problem, IVariableBinding resolvedField, Collection<ICommandAccess> 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 = SimilarElementsRequestor.REF_TYPES;
} else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
if (JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
typeKind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES | SimilarElementsRequestor.ENUMS;
} else {
typeKind = SimilarElementsRequestor.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 = SimilarElementsRequestor.REF_TYPES_AND_VAR;
} else if (parent instanceof QualifiedName) {
Name qualifier = ((QualifiedName) parent).getQualifier();
if (qualifier != node) {
binding = qualifier.resolveTypeBinding();
} else {
typeKind = SimilarElementsRequestor.REF_TYPES;
}
ASTNode outerParent = parent.getParent();
while (outerParent instanceof QualifiedName) {
outerParent = outerParent.getParent();
}
if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
typeKind = SimilarElementsRequestor.REF_TYPES;
suggestVariableProposals = false;
}
} else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
ITypeBinding switchExp = ((SwitchStatement) node.getParent().getParent()).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 = SimilarElementsRequestor.REF_TYPES;
suggestVariableProposals = node.isSimpleName();
}
if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
typeKind = SimilarElementsRequestor.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 &= ~(SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS | SimilarElementsRequestor.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 &= ~SimilarElementsRequestor.ANNOTATIONS;
addNewTypeProposals(cu, node, typeKind, relevance, proposals);
ReorgCorrectionsSubProcessor.addProjectSetupFixProposal(context, problem, node.getFullyQualifiedName(), 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);
}
}
}
Aggregations