Search in sources :

Example 1 with ConditionalExpression

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

the class NecessaryParenthesesChecker method needsParentheses.

/**
	 * Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
	 * <code>locationInParent</code> ?
	 *
	 * @param expression the expression
	 * @param parent the parent node
	 * @param locationInParent location of expression in the parent
	 * @param leftOperandType the type of the left operand in <code>parent</code> if
	 *            <code>parent</code> is an infix expression with no bindings and
	 *            <code>expression</code> is the right operand in it, <code>null</code> otherwise
	 * @return <code>true</code> if <code>expression</code> needs parentheses, <code>false</code>
	 *         otherwise.
	 *
	 * @since 3.9
	 */
private static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
    if (!expressionTypeNeedsParentheses(expression))
        return false;
    if (!locationNeedsParentheses(locationInParent)) {
        return false;
    }
    if (parent instanceof Expression) {
        Expression parentExpression = (Expression) parent;
        if (expression instanceof PrefixExpression) {
            // see bug 405096
            return needsParenthesesForPrefixExpression(parentExpression, ((PrefixExpression) expression).getOperator());
        }
        int expressionPrecedence = OperatorPrecedence.getExpressionPrecedence(expression);
        int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(parentExpression);
        if (expressionPrecedence > parentPrecedence)
            //(opEx) opParent and opEx binds more -> parentheses not needed
            return false;
        if (expressionPrecedence < parentPrecedence)
            //(opEx) opParent and opEx binds less -> parentheses needed
            return true;
        if (parentExpression instanceof InfixExpression) {
            return needsParenthesesInInfixExpression(expression, (InfixExpression) parentExpression, locationInParent, leftOperandType);
        }
        if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
            return true;
        }
        return false;
    }
    return true;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 2 with ConditionalExpression

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

the class ASTNodes method getTargetType.

/**
	 * Derives the target type defined at the location of the given expression if the target context
	 * supports poly expressions.
	 * 
	 * @param expression the expression at whose location the target type is required
	 * @return the type binding of the target type defined at the location of the given expression
	 *         if the target context supports poly expressions, or <code>null</code> if the target
	 *         type could not be derived
	 * 
	 * @since 3.10
	 */
public static ITypeBinding getTargetType(Expression expression) {
    ASTNode parent = expression.getParent();
    StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
        return ((VariableDeclaration) parent).getName().resolveTypeBinding();
    } else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        return getTargetTypeForReturnStmt((ReturnStatement) parent);
    } else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
        return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
    } else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) parent;
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (methodBinding != null) {
            return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
        }
    } else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
        SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
        IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
        if (superMethodBinding != null) {
            return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
        }
    } else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
        ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
        IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
        if (constructorBinding != null) {
            return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
        }
    } else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
        SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
        IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
        if (superConstructorBinding != null) {
            return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
        }
    } else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
        ClassInstanceCreation creation = (ClassInstanceCreation) parent;
        IMethodBinding creationBinding = creation.resolveConstructorBinding();
        if (creationBinding != null) {
            return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
        }
    } else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
        EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
        IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
        if (enumConstructorBinding != null) {
            return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
        }
    } else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
        IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
        if (methodBinding != null) {
            return methodBinding.getReturnType();
        }
    } else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        return getTargetType((ConditionalExpression) parent);
    } else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
        return ((CastExpression) parent).getType().resolveBinding();
    } else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return getTargetType((ParenthesizedExpression) parent);
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 3 with ConditionalExpression

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

the class QuickAssistProcessor method getInvertEqualsProposal.

private static boolean getInvertEqualsProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof MethodInvocation)) {
        node = node.getParent();
        if (!(node instanceof MethodInvocation)) {
            return false;
        }
    }
    MethodInvocation method = (MethodInvocation) node;
    String identifier = method.getName().getIdentifier();
    if (!"equals".equals(identifier) && !"equalsIgnoreCase".equals(identifier)) {
        //$NON-NLS-1$ //$NON-NLS-2$
        return false;
    }
    List<Expression> arguments = method.arguments();
    if (arguments.size() != 1) {
        //overloaded equals w/ more than 1 argument
        return false;
    }
    Expression right = arguments.get(0);
    ITypeBinding binding = right.resolveTypeBinding();
    if (binding != null && !(binding.isClass() || binding.isInterface() || binding.isEnum())) {
        //overloaded equals w/ non-class/interface argument or null
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    Expression left = method.getExpression();
    AST ast = method.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    if (left == null) {
        // equals(x) -> x.equals(this)
        MethodInvocation replacement = ast.newMethodInvocation();
        replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
        replacement.arguments().add(ast.newThisExpression());
        replacement.setExpression((Expression) rewrite.createCopyTarget(right));
        rewrite.replace(method, replacement, null);
    } else if (right instanceof ThisExpression) {
        // x.equals(this) -> equals(x)
        MethodInvocation replacement = ast.newMethodInvocation();
        replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
        replacement.arguments().add(rewrite.createCopyTarget(left));
        rewrite.replace(method, replacement, null);
    } else {
        ASTNode leftExpression = left;
        while (leftExpression instanceof ParenthesizedExpression) {
            leftExpression = ((ParenthesizedExpression) left).getExpression();
        }
        rewrite.replace(right, rewrite.createCopyTarget(leftExpression), null);
        if (right instanceof CastExpression || right instanceof Assignment || right instanceof ConditionalExpression || right instanceof InfixExpression) {
            ParenthesizedExpression paren = ast.newParenthesizedExpression();
            paren.setExpression((Expression) rewrite.createCopyTarget(right));
            rewrite.replace(left, paren, null);
        } else {
            rewrite.replace(left, rewrite.createCopyTarget(right), null);
        }
    }
    String label = CorrectionMessages.QuickAssistProcessor_invertequals_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_EQUALS);
    resultingCollections.add(proposal);
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) 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) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 4 with ConditionalExpression

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

the class AdvancedQuickAssistProcessor method getPushNegationDownProposals.

private static boolean getPushNegationDownProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    PrefixExpression negationExpression = null;
    ParenthesizedExpression parenthesizedExpression = null;
    // check for case when cursor is on '!' before parentheses
    if (covering instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) covering;
        if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT && prefixExpression.getOperand() instanceof ParenthesizedExpression) {
            negationExpression = prefixExpression;
            parenthesizedExpression = (ParenthesizedExpression) prefixExpression.getOperand();
        }
    }
    // check for case when cursor is on parenthesized expression that is negated
    if (covering instanceof ParenthesizedExpression && covering.getParent() instanceof PrefixExpression && ((PrefixExpression) covering.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
        negationExpression = (PrefixExpression) covering.getParent();
        parenthesizedExpression = (ParenthesizedExpression) covering;
    }
    if (negationExpression == null || (!(parenthesizedExpression.getExpression() instanceof InfixExpression) && !(parenthesizedExpression.getExpression() instanceof ConditionalExpression))) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    final AST ast = covering.getAST();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepared inverted expression
    Expression inversedExpression = getInversedExpression(rewrite, parenthesizedExpression.getExpression());
    // check, may be we should keep parentheses
    boolean keepParentheses = false;
    if (negationExpression.getParent() instanceof Expression) {
        int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(((Expression) negationExpression.getParent()));
        int inversedExpressionPrecedence = OperatorPrecedence.getExpressionPrecedence(inversedExpression);
        keepParentheses = parentPrecedence > inversedExpressionPrecedence;
    }
    // replace negated expression with inverted one
    if (keepParentheses) {
        ParenthesizedExpression pe = ast.newParenthesizedExpression();
        pe.setExpression(inversedExpression);
        rewrite.replace(negationExpression, pe, null);
    } else {
        rewrite.replace(negationExpression, inversedExpression, null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_pushNegationDown;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_DOWN);
    resultingCollections.add(proposal);
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 5 with ConditionalExpression

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

the class FullConstraintCreator method create.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ConditionalExpression)
	 */
@Override
public ITypeConstraint[] create(ConditionalExpression node) {
    List<ITypeConstraint> result = new ArrayList<ITypeConstraint>();
    Expression thenExpression = node.getThenExpression();
    Expression elseExpression = node.getElseExpression();
    ConstraintVariable whole = fConstraintVariableFactory.makeExpressionOrTypeVariable(node, getContext());
    ConstraintVariable ev1 = fConstraintVariableFactory.makeExpressionOrTypeVariable(thenExpression, getContext());
    ConstraintVariable ev2 = fConstraintVariableFactory.makeExpressionOrTypeVariable(elseExpression, getContext());
    ITypeConstraint[] constraints1 = fTypeConstraintFactory.createEqualsConstraint(ev1, ev2);
    ITypeConstraint[] constraints2 = fTypeConstraintFactory.createSubtypeConstraint(ev1, whole);
    ITypeConstraint[] constraints3 = fTypeConstraintFactory.createSubtypeConstraint(ev2, whole);
    result.addAll(Arrays.asList(constraints1));
    result.addAll(Arrays.asList(constraints2));
    result.addAll(Arrays.asList(constraints3));
    return result.toArray(new ITypeConstraint[result.size()]);
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList)

Aggregations

ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)13 Expression (org.eclipse.jdt.core.dom.Expression)12 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)8 AST (org.eclipse.jdt.core.dom.AST)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 Assignment (org.eclipse.jdt.core.dom.Assignment)5 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)2 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)2 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)2 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)2