Search in sources :

Example 41 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)

Example 42 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 InfixExpression node) {
    final boolean useRichString = this._aSTFlattenerUtils.canConvertToRichText(node);
    if (useRichString) {
        ASTNode _parent = node.getParent();
        final boolean firstEntrance = (!(_parent instanceof InfixExpression));
        if (firstEntrance) {
            this.appendToBuffer("\'\'\'");
        }
        this.appendAsRichString(node.getLeftOperand());
        this.appendAsRichString(node.getRightOperand());
        final Function2<Expression, Expression, Expression> _function = (Expression prevExpr, Expression currExpr) -> {
            this.appendAsRichString(currExpr);
            return currExpr;
        };
        IterableExtensions.<Expression, Expression>fold(node.extendedOperands(), node.getRightOperand(), _function);
        if (firstEntrance) {
            this.appendToBuffer("\'\'\'");
            if (this.fallBackStrategy) {
                this.appendToBuffer(".toString");
            }
        }
    } else {
        node.getLeftOperand().accept(this);
        final InfixExpression.Operator operator = node.getOperator();
        this.handleInfixRightSide(node, operator, node.getRightOperand());
        final List extendedOperands = node.extendedOperands();
        int _size = extendedOperands.size();
        boolean _notEquals = (_size != 0);
        if (_notEquals) {
            final Consumer<Expression> _function_1 = (Expression e) -> {
                this.handleInfixRightSide(node, operator, e);
            };
            extendedOperands.forEach(_function_1);
        }
    }
    return false;
}
Also used : 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ArrayList(java.util.ArrayList) List(java.util.List)

Example 43 with InfixExpression

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

the class SwitchRefactoring method extractVariableAndValuesFromInfixExpression.

private Variable extractVariableAndValuesFromInfixExpression(InfixExpression infixExpr) {
    final Operator op = infixExpr.getOperator();
    final Expression leftOp = infixExpr.getLeftOperand();
    final Expression rightOp = infixExpr.getRightOperand();
    if (extendedOperands(infixExpr).isEmpty() && (CONDITIONAL_OR.equals(op) || OR.equals(op) || XOR.equals(op))) {
        final Variable leftVar = extractVariableAndValues(leftOp);
        final Variable rightVar = extractVariableAndValues(rightOp);
        if (leftVar != null && leftVar.isSameVariable(rightVar)) {
            return leftVar.mergeValues(rightVar);
        }
    } else if (EQUALS.equals(op)) {
        Variable variable = extractVariableWithConstantValue(leftOp, rightOp);
        return variable != null ? variable : extractVariableWithConstantValue(rightOp, leftOp);
    }
    return null;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 44 with InfixExpression

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

the class SimplifyExpressionRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    final Expression lhs = node.getLeftOperand();
    final Expression rhs = node.getRightOperand();
    if (hasOperator(node, CONDITIONAL_OR)) {
        final List<Expression> remainingOperands = removeUselessOperands(node, true, false);
        if (!remainingOperands.equals(allOperands(node))) {
            return replaceWithNewInfixExpr(node, remainingOperands);
        }
    } else if (hasOperator(node, CONDITIONAL_AND)) {
        final List<Expression> remainingOperands = removeUselessOperands(node, false, true);
        if (!remainingOperands.equals(allOperands(node))) {
            return replaceWithNewInfixExpr(node, remainingOperands);
        } else {
            // FIXME this should actually check anywhere in the infix expression,
            // not only for left and right operands,
            // said otherwise: handle extended operands
            final Expression nullCheckedExpressionLHS = getNullCheckedExpression(lhs);
            final Expression nullCheckedExpressionRHS = getNullCheckedExpression(rhs);
            if (nullCheckedExpressionLHS != null) {
                if (isNullCheckRedundant(rhs, nullCheckedExpressionLHS)) {
                    checkNoExtendedOperands(node);
                    return replaceBy(node, rhs);
                }
            } else if (isNullCheckRedundant(lhs, nullCheckedExpressionRHS)) {
                return replaceBy(node, lhs);
            }
        }
    } else if ((hasOperator(node, EQUALS) || hasOperator(node, NOT_EQUALS) || hasOperator(node, XOR)) && !node.hasExtendedOperands()) {
        if (maybeReduceBooleanExpression(node, lhs, rhs) == DO_NOT_VISIT_SUBTREE) {
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    if (shouldHaveParentheses(node)) {
        addParentheses(node);
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) List(java.util.List)

Example 45 with InfixExpression

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

the class SimplifyExpressionRefactoring method getExpressionWithoutParentheses.

private Expression getExpressionWithoutParentheses(ParenthesizedExpression node) {
    final ASTNode parent = node.getParent();
    final Expression innerExpr = node.getExpression();
    if (innerExpr instanceof ParenthesizedExpression) {
        return innerExpr;
    }
    if (parent instanceof InfixExpression) {
        final InfixExpression parentInfixExpr = (InfixExpression) parent;
        if (innerExpr instanceof InfixExpression) {
            final Operator innerOp = ((InfixExpression) innerExpr).getOperator();
            if (innerOp == parentInfixExpr.getOperator() && OperatorEnum.isAssociative(innerOp) && // to other if statements in this method.
            equalNotNull(innerExpr.resolveTypeBinding(), parentInfixExpr.resolveTypeBinding())) {
                return innerExpr;
            }
        }
    }
    // Infix, prefix or postfix without parenthesis is not readable
    if ((parent instanceof InfixExpression && (InfixExpression.Operator.PLUS.equals(((InfixExpression) parent).getOperator()) || InfixExpression.Operator.MINUS.equals(((InfixExpression) parent).getOperator()))) || (parent instanceof PrefixExpression && (PLUS.equals(((PrefixExpression) parent).getOperator()) || MINUS.equals(((PrefixExpression) parent).getOperator())))) {
        if (innerExpr instanceof PrefixExpression && (DECREMENT.equals(((PrefixExpression) innerExpr).getOperator()) || INCREMENT.equals(((PrefixExpression) innerExpr).getOperator()) || PLUS.equals(((PrefixExpression) innerExpr).getOperator()) || MINUS.equals(((PrefixExpression) innerExpr).getOperator()))) {
            return node;
        }
        if (innerExpr instanceof PostfixExpression && (PostfixExpression.Operator.DECREMENT.equals(((PostfixExpression) innerExpr).getOperator()) || PostfixExpression.Operator.INCREMENT.equals(((PostfixExpression) innerExpr).getOperator()))) {
            return node;
        }
    }
    if (isInnerExprHardToRead(innerExpr, parent)) {
        // return (bla != null) ? bla.getSomething() : null;
        return node;
    }
    if (isUselessParenthesesInStatement(parent, node)) {
        return innerExpr;
    }
    final int compareTo = OperatorEnum.compareTo(innerExpr, parent);
    if (compareTo < 0) {
        return node;
    } else if (compareTo > 0) {
        return innerExpr;
    }
    if (// some like it like that
    innerExpr instanceof InfixExpression || // or if it can be removed.
    innerExpr instanceof CastExpression || // infix and prefix or postfix without parenthesis is not readable
    ((parent instanceof InfixExpression || parent instanceof PrefixExpression || parent instanceof PostfixExpression) && (innerExpr instanceof PrefixExpression || innerExpr instanceof PostfixExpression))) {
        return node;
    }
    return innerExpr;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)95 Expression (org.eclipse.jdt.core.dom.Expression)65 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)51 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)39 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)31 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)26 AST (org.eclipse.jdt.core.dom.AST)24 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)20 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)19 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 Assignment (org.eclipse.jdt.core.dom.Assignment)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)10 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)10