Search in sources :

Example 56 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression 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 57 with InfixExpression

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

the class AdvancedQuickAssistProcessor method getCombineStringProposals.

private static boolean getCombineStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    // we work with InfixExpressions
    InfixExpression infixExpression;
    if (node instanceof InfixExpression) {
        infixExpression = (InfixExpression) node;
    } else if (node.getParent() instanceof InfixExpression) {
        infixExpression = (InfixExpression) node.getParent();
    } else {
        return false;
    }
    // only + is valid for combining strings
    if (!(infixExpression.getOperator().equals(InfixExpression.Operator.PLUS))) {
        return false;
    }
    // all expressions must be strings
    Expression leftOperand = infixExpression.getLeftOperand();
    Expression rightOperand = infixExpression.getRightOperand();
    if (!(leftOperand instanceof StringLiteral && rightOperand instanceof StringLiteral)) {
        return false;
    }
    StringLiteral leftString = (StringLiteral) leftOperand;
    StringLiteral rightString = (StringLiteral) rightOperand;
    if (resultingCollections == null) {
        return true;
    }
    // begin building combined string
    StringBuilder stringBuilder = new StringBuilder(leftString.getLiteralValue());
    stringBuilder.append(rightString.getLiteralValue());
    // append extended string literals
    for (Object operand : infixExpression.extendedOperands()) {
        if (!(operand instanceof StringLiteral))
            return false;
        StringLiteral stringLiteral = (StringLiteral) operand;
        stringBuilder.append(stringLiteral.getLiteralValue());
    }
    // prepare new string literal
    AST ast = node.getAST();
    StringLiteral combinedStringLiteral = ast.newStringLiteral();
    combinedStringLiteral.setLiteralValue(stringBuilder.toString());
    ASTRewrite rewrite = ASTRewrite.create(ast);
    rewrite.replace(infixExpression, combinedStringLiteral, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_combineSelectedStrings;
    // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.COMBINE_STRINGS);
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) 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) 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 58 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 59 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 60 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final PrefixExpression node) {
    final Expression operand = node.getOperand();
    PrefixExpression.Operator _operator = node.getOperator();
    boolean _matched = false;
    if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) {
        _matched = true;
    }
    if (!_matched) {
        if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) {
            _matched = true;
        }
    }
    if (_matched) {
        if ((operand instanceof ArrayAccess)) {
            final String arrayName = this.computeArrayName(((ArrayAccess) operand));
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("_tPreInx_");
            _builder.append(arrayName);
            final String idxName = _builder.toString();
            String op = "-";
            PrefixExpression.Operator _operator_1 = node.getOperator();
            boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT);
            if (_equals) {
                op = "+";
            }
            StringConcatenation _builder_1 = new StringConcatenation();
            _builder_1.append("{val ");
            _builder_1.append(idxName);
            _builder_1.append("=");
            this.appendToBuffer(_builder_1.toString());
            ((ArrayAccess) operand).getIndex().accept(this);
            StringConcatenation _builder_2 = new StringConcatenation();
            _builder_2.append(" ");
            _builder_2.append("val ");
            _builder_2.append(idxName, " ");
            _builder_2.append("_res=");
            _builder_2.append(arrayName, " ");
            _builder_2.append(".get(");
            _builder_2.append(idxName, " ");
            _builder_2.append(")");
            _builder_2.append(op, " ");
            _builder_2.append("1");
            this.appendToBuffer(_builder_2.toString());
            StringConcatenation _builder_3 = new StringConcatenation();
            _builder_3.append(" ");
            _builder_3.append(arrayName, " ");
            _builder_3.append(".set(");
            _builder_3.append(idxName, " ");
            _builder_3.append(", ");
            _builder_3.append(idxName, " ");
            _builder_3.append("_res)  ");
            _builder_3.append(idxName, " ");
            _builder_3.append("_res}");
            this.appendToBuffer(_builder_3.toString());
            return false;
        } else {
            final AST dummyAST = AST.newAST(node.getAST().apiLevel());
            final Assignment assigment = dummyAST.newAssignment();
            final InfixExpression infixOp = dummyAST.newInfixExpression();
            ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand);
            infixOp.setLeftOperand(((Expression) _copySubtree));
            PrefixExpression.Operator _operator_2 = node.getOperator();
            boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT);
            if (_equals_1) {
                infixOp.setOperator(InfixExpression.Operator.MINUS);
            } else {
                infixOp.setOperator(InfixExpression.Operator.PLUS);
            }
            infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
            ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand);
            final Expression leftSide = ((Expression) _copySubtree_1);
            assigment.setLeftHandSide(leftSide);
            assigment.setRightHandSide(infixOp);
            this.appendToBuffer("{");
            Type type = null;
            if ((operand instanceof SimpleName)) {
                type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) operand));
            }
            this.handleAssignment(assigment, leftSide, type);
            this.appendToBuffer("}");
            return false;
        }
    }
    if (!_matched) {
        if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) {
            _matched = true;
            node.getOperand().accept(this);
            this.appendToBuffer(".bitwiseNot");
        }
    }
    if (!_matched) {
        {
            this.appendToBuffer(node.getOperator().toString());
            node.getOperand().accept(this);
        }
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)196 Expression (org.eclipse.jdt.core.dom.Expression)137 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)85 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)67 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)55 CastExpression (org.eclipse.jdt.core.dom.CastExpression)47 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)37 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)34 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)31 AST (org.eclipse.jdt.core.dom.AST)29 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)25 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)24 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)24 TextEditGroup (org.eclipse.text.edits.TextEditGroup)22 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Assignment (org.eclipse.jdt.core.dom.Assignment)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)17