Search in sources :

Example 16 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method combineOperands.

private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
    if (existing == null && removeParentheses) {
        while (originalNode instanceof ParenthesizedExpression) {
            originalNode = ((ParenthesizedExpression) originalNode).getExpression();
        }
    }
    Expression newRight = (Expression) rewrite.createMoveTarget(originalNode);
    if (originalNode instanceof InfixExpression) {
        ((InfixExpression) newRight).setOperator(((InfixExpression) originalNode).getOperator());
    }
    if (existing == null) {
        return newRight;
    }
    AST ast = rewrite.getAST();
    InfixExpression infix = ast.newInfixExpression();
    infix.setOperator(operator);
    infix.setLeftOperand(existing);
    infix.setRightOperand(newRight);
    return infix;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 17 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getParenthesizedExpression.

private static ParenthesizedExpression getParenthesizedExpression(AST ast, Expression expression) {
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    parenthesizedExpression.setExpression(expression);
    return parenthesizedExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression)

Example 18 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method isNegated.

private static boolean isNegated(Expression expression) {
    if (!(expression.getParent() instanceof ParenthesizedExpression))
        return false;
    ParenthesizedExpression parenthesis = (ParenthesizedExpression) expression.getParent();
    if (!(parenthesis.getParent() instanceof PrefixExpression))
        return false;
    PrefixExpression prefix = (PrefixExpression) parenthesis.getParent();
    if (!(prefix.getOperator() == PrefixExpression.Operator.NOT))
        return false;
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 19 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.

the class UnresolvedElementsSubProcessor method useExistingParentCastProposal.

private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ICommandAccess> proposals) {
    ITypeBinding castType = expression.getType().resolveBinding();
    if (castType == null) {
        return false;
    }
    if (paramTypes != null) {
        if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
            return false;
        }
    } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
        return false;
    }
    ITypeBinding bindingToCast = accessExpression.resolveTypeBinding();
    if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
        return false;
    }
    IMethodBinding res = Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
    if (res != null) {
        AST ast = expression.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        CastExpression newCast = ast.newCastExpression();
        newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
        newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
        ParenthesizedExpression parents = ast.newParenthesizedExpression();
        parents.setExpression(newCast);
        ASTNode node = rewrite.createCopyTarget(expression.getExpression());
        rewrite.replace(expression, node, null);
        rewrite.replace(accessExpression, parents, null);
        String label = CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST, image);
        proposals.add(proposal);
        return true;
    }
    return false;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Image(org.eclipse.swt.graphics.Image)

Example 20 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression 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);
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) 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)

Aggregations

ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)30 CastExpression (org.eclipse.jdt.core.dom.CastExpression)20 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 Expression (org.eclipse.jdt.core.dom.Expression)17 AST (org.eclipse.jdt.core.dom.AST)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 Assignment (org.eclipse.jdt.core.dom.Assignment)5 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)4 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)3 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3