use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method addCaseWithStmts.
private void addCaseWithStmts(final SwitchStatement switchStmt, final List<Expression> caseValues, final List<Statement> innerStmts) {
final ASTBuilder b = ctx.getASTBuilder();
final List<Statement> switchStmts = statements(switchStmt);
// Add the case statement(s)
if (caseValues != null) {
for (final Expression caseValue : caseValues) {
switchStmts.add(b.case0(b.move(caseValue)));
}
} else {
switchStmts.add(b.default0());
}
// Add the statement(s) for this case(s)
boolean isBreakNeeded = true;
if (!innerStmts.isEmpty()) {
for (final Statement stmt : innerStmts) {
switchStmts.add(b.move(stmt));
}
isBreakNeeded = !isEndingWithJump(getLast(innerStmts));
}
// When required: end with a break;
if (isBreakNeeded) {
switchStmts.add(b.break0());
}
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getVariableProposals.
public static void getVariableProposals(IInvocationContext context, IProblemLocation problem, IVariableBinding resolvedField, Collection<CUCorrectionProposal> 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.is18OrHigher(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) {
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 = 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.SwitchStatement in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method addRemoveIncludingConditionProposal.
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<CUCorrectionProposal> proposals) {
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
AST ast = toRemove.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION);
if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block) replacement).statements().size() == 0) {
if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) {
rewrite.replace(toRemove, toRemove.getAST().newBlock(), null);
} else {
rewrite.remove(toRemove, null);
}
} else if (toRemove instanceof Expression && replacement instanceof Expression) {
Expression moved = (Expression) rewrite.createMoveTarget(replacement);
Expression toRemoveExpression = (Expression) toRemove;
Expression replacementExpression = (Expression) replacement;
ITypeBinding explicitCast = ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(moved);
moved = parenthesized;
}
cast.setExpression(moved);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(toRemove, imports);
cast.setType(imports.addImport(explicitCast, ast, importRewriteContext, TypeLocation.CAST));
moved = cast;
}
rewrite.replace(toRemove, moved, null);
} else {
ASTNode parent = toRemove.getParent();
ASTNode moveTarget;
if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) {
ListRewrite listRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
List<Statement> list = ((Block) replacement).statements();
int lastIndex = list.size() - 1;
moveTarget = listRewrite.createMoveTarget(list.get(0), list.get(lastIndex));
} else {
moveTarget = rewrite.createMoveTarget(replacement);
}
rewrite.replace(toRemove, moveTarget, null);
}
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.
the class GenericBuilderAdapterBuilder method getEnumMethodSwitch.
protected List getEnumMethodSwitch(String type) {
if (enumCases == null) {
MethodDeclaration method = newMethodDeclaration("void", "wEntity", newSingleVariableDeclaration(newParameterizedType(EntityDescriptor.class.getName(), ast.newWildcardType()), "entityDesc"), newSingleVariableDeclaration(EnumValue.class.getName(), "value"));
SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("entityDesc", "getOrdinal"));
method.getBody().statements().add(switchStm);
addBodyDeclaration(method);
enumCases = switchStm.statements();
}
return enumCases;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.
the class GenericBuilderAdapterBuilder method getCompositeMethodSwitch.
protected List getCompositeMethodSwitch(String type) {
List cases = compositeSwitchMap.get(type);
if (cases == null) {
MethodDeclaration method = newMethodDeclaration("void", type, newSingleVariableDeclaration(newParameterizedType(EntityDescriptor.class.getName(), ast.newWildcardType()), "entityDesc"), newSingleVariableDeclaration("int", "initialCapacity"));
SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("entityDesc", "getOrdinal"));
method.getBody().statements().add(switchStm);
addBodyDeclaration(method);
compositeSwitchMap.put(type, cases = switchStm.statements());
}
return cases;
}
Aggregations