Search in sources :

Example 6 with InfixExpression

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

the class AssociativeInfixExpressionFragment method createFragmentForFullSubtree.

public static IExpressionFragment createFragmentForFullSubtree(InfixExpression node) {
    Assert.isNotNull(node);
    if (!isAssociativeInfix(node))
        return null;
    InfixExpression groupRoot = findGroupRoot(node);
    Assert.isTrue(isAGroupRoot(groupRoot));
    List<Expression> groupMembers = AssociativeInfixExpressionFragment.findGroupMembersInOrderFor(node);
    return new AssociativeInfixExpressionFragment(groupRoot, groupMembers);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 7 with InfixExpression

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

the class NecessaryParenthesesChecker method needsParenthesesInInfixExpression.

private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
    InfixExpression.Operator parentInfixOperator = parentInfix.getOperator();
    ITypeBinding rightOperandType;
    ITypeBinding parentInfixExprType;
    if (leftOperandType == null) {
        // parentInfix has bindings
        leftOperandType = parentInfix.getLeftOperand().resolveTypeBinding();
        rightOperandType = parentInfix.getRightOperand().resolveTypeBinding();
        parentInfixExprType = parentInfix.resolveTypeBinding();
    } else {
        rightOperandType = expression.resolveTypeBinding();
        parentInfixExprType = getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType);
    }
    boolean isAllOperandsHaveSameType = isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType);
    if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
        //infix expressions are evaluated from left to right -> parentheses not needed
        return false;
    } else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) {
        //left op (right) == (right) op left == right op left
        if (expression instanceof InfixExpression) {
            InfixExpression infixExpression = (InfixExpression) expression;
            Operator operator = infixExpression.getOperator();
            if (isStringType(parentInfixExprType)) {
                if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) {
                    // "" + (2 + "") == "" + 2 + ""
                    return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType);
                }
                //"" + (1 + 2), "" + (1 - 2) etc
                return true;
            }
            if (parentInfixOperator != InfixExpression.Operator.TIMES)
                return false;
            if (operator == InfixExpression.Operator.TIMES)
                // x * (y * z) == x * y * z
                return false;
            if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
                // x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
                return true;
            return false;
        }
        return false;
    } else {
        return true;
    }
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 8 with InfixExpression

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

the class NecessaryParenthesesChecker method isAllOperandsHaveSameType.

/*
	 * Do all operands in expression have same type
	 */
private static boolean isAllOperandsHaveSameType(InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
    ITypeBinding binding = leftOperandType;
    if (binding == null)
        return false;
    ITypeBinding current = rightOperandType;
    if (binding != current)
        return false;
    for (Iterator<Expression> iterator = expression.extendedOperands().iterator(); iterator.hasNext(); ) {
        Expression operand = iterator.next();
        current = operand.resolveTypeBinding();
        if (binding != current)
            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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 9 with InfixExpression

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

the class AdvancedQuickAssistProcessor method getPickOutStringProposals.

private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    // we work with String's
    if (!(node instanceof StringLiteral)) {
        return false;
    }
    // user should select part of String
    int selectionPos = context.getSelectionOffset();
    int selectionLen = context.getSelectionLength();
    if (selectionLen == 0) {
        return false;
    }
    int valueStart = node.getStartPosition() + 1;
    int valueEnd = node.getStartPosition() + node.getLength() - 1;
    // selection must be inside node and the quotes and not contain the full value
    if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
        return false;
    }
    // prepare string parts positions
    StringLiteral stringLiteral = (StringLiteral) node;
    String stringValue = stringLiteral.getEscapedValue();
    int firstPos = selectionPos - node.getStartPosition();
    int secondPos = firstPos + selectionLen;
    // prepare new string literals
    AST ast = node.getAST();
    StringLiteral leftLiteral = ast.newStringLiteral();
    StringLiteral centerLiteral = ast.newStringLiteral();
    StringLiteral rightLiteral = ast.newStringLiteral();
    try {
        leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
        centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
        rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
    } catch (IllegalArgumentException e) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare new expression instead of StringLiteral
    InfixExpression expression = ast.newInfixExpression();
    expression.setOperator(InfixExpression.Operator.PLUS);
    if (firstPos != 1) {
        expression.setLeftOperand(leftLiteral);
    }
    if (firstPos == 1) {
        expression.setLeftOperand(centerLiteral);
    } else {
        expression.setRightOperand(centerLiteral);
    }
    if (secondPos < stringValue.length() - 1) {
        if (firstPos == 1) {
            expression.setRightOperand(rightLiteral);
        } else {
            expression.extendedOperands().add(rightLiteral);
        }
    }
    // use new expression instead of old StirngLiteral
    rewrite.replace(stringLiteral, expression, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING);
    //$NON-NLS-1$
    proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING");
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 10 with InfixExpression

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

the class AdvancedQuickAssistProcessor method getBooleanExpression.

private static Expression getBooleanExpression(ASTNode node) {
    if (!(node instanceof Expression)) {
        return null;
    }
    // check if the node is a location where it can be negated
    StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
    if (locationInParent == QualifiedName.NAME_PROPERTY) {
        node = node.getParent();
        locationInParent = node.getLocationInParent();
    }
    while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        node = node.getParent();
        locationInParent = node.getLocationInParent();
    }
    Expression expression = (Expression) node;
    if (!isBoolean(expression)) {
        return null;
    }
    if (expression.getParent() instanceof InfixExpression) {
        return expression;
    }
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY || locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
        return expression;
    }
    return null;
}
Also used : 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) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)73 Expression (org.eclipse.jdt.core.dom.Expression)50 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)30 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)23 CastExpression (org.eclipse.jdt.core.dom.CastExpression)22 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)19 AST (org.eclipse.jdt.core.dom.AST)18 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)10 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)10 Assignment (org.eclipse.jdt.core.dom.Assignment)8 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)8 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)8 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7