use of org.eclipse.jdt.core.dom.SwitchCase 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.SwitchCase in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method canReplace.
// !! - like one in ExtractTempRefactoring
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 (parent instanceof ExpressionStatement) {
return false;
}
if (parent instanceof SwitchCase) {
if (node instanceof Name) {
Name name = (Name) node;
ITypeBinding typeBinding = name.resolveTypeBinding();
if (typeBinding != null) {
return !typeBinding.isEnum();
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls by eclipse.
the class ExtractFieldRefactoring 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 (parent instanceof LambdaExpression) {
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;
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls 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 (parent instanceof LambdaExpression) {
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;
}
use of org.eclipse.jdt.core.dom.SwitchCase 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);
}
}
Aggregations