Search in sources :

Example 21 with SwitchStatement

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());
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 22 with SwitchStatement

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);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 23 with SwitchStatement

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);
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List) ArrayList(java.util.ArrayList) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 24 with SwitchStatement

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;
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration)

Example 25 with SwitchStatement

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;
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) List(java.util.List)

Aggregations

SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)35 Statement (org.eclipse.jdt.core.dom.Statement)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 DoStatement (org.eclipse.jdt.core.dom.DoStatement)12 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)11 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)10 Expression (org.eclipse.jdt.core.dom.Expression)9 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Block (org.eclipse.jdt.core.dom.Block)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)6 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 TryStatement (org.eclipse.jdt.core.dom.TryStatement)5