Search in sources :

Example 16 with PrefixExpression

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

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

the class AbstractUnitTestCleanUp method maybeRefactorStatement.

/**
 * Maybe refactor the statement.
 *
 * @param classesToUseWithImport The classes to use with import
 * @param importsToAdd           The imports to add
 * @param nodeToReplace          The node
 * @param originalMethod         The method invocation
 * @param isAssertTrue           True if assertTrue is used, False if assertFalse is
 *                               used.
 * @param condition              The condition on which the assert is based.
 * @param failureMessage         The failure message or null.
 * @param isRewriteNeeded        True if is the rewriting is needed.
 * @return True if refactored
 */
protected boolean maybeRefactorStatement(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertTrue, final Expression condition, final Expression failureMessage, final boolean isRewriteNeeded) {
    Expression localCondition = condition;
    boolean localIsAssertTrue = isAssertTrue;
    boolean localIsRewriteNeeded = isRewriteNeeded;
    PrefixExpression localConditionPe = ASTNodes.as(localCondition, PrefixExpression.class);
    while (ASTNodes.hasOperator(localConditionPe, PrefixExpression.Operator.NOT)) {
        localIsRewriteNeeded = true;
        localIsAssertTrue = !localIsAssertTrue;
        localCondition = ASTNodes.as(localConditionPe.getOperand(), Expression.class);
        localConditionPe = ASTNodes.as(localCondition, PrefixExpression.class);
    }
    InfixExpression conditionIe = ASTNodes.as(localCondition, InfixExpression.class);
    MethodInvocation conditionMi = ASTNodes.as(localCondition, MethodInvocation.class);
    Object constantValue = localCondition.resolveConstantExpressionValue();
    return maybeRefactorAssertTrueOrFalse(classesToUseWithImport, importsToAdd, nodeToReplace, originalMethod, localIsAssertTrue, localCondition, conditionIe, conditionMi, constantValue, failureMessage, localIsRewriteNeeded);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 18 with PrefixExpression

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

the class ControlWorkflowMatcher method expandActualNode.

private void expandActualNode(final ControlWorkflowNode actualNode) {
    if (actualNode.getCondition() == null) {
        return;
    }
    PrefixExpression prefixExpression = ASTNodes.as(actualNode.getCondition(), PrefixExpression.class);
    ConditionalExpression ternaryExpression = ASTNodes.as(actualNode.getCondition(), ConditionalExpression.class);
    InfixExpression infixExpression = ASTNodes.as(actualNode.getCondition(), InfixExpression.class);
    if (prefixExpression != null) {
        if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.NOT)) {
            ControlWorkflowNode oppositeNode = actualNode.getThenNode();
            actualNode.setThenNode(actualNode.getElseNode());
            actualNode.setElseNode(oppositeNode);
            actualNode.setCondition(prefixExpression.getOperand());
            expandActualNode(actualNode);
            return;
        }
    } else if (ternaryExpression != null) {
        ControlWorkflowNode node1 = new ControlWorkflowNode();
        node1.setCondition(ternaryExpression.getThenExpression());
        node1.setThenNode(cloneNode(actualNode.getThenNode()));
        node1.setElseNode(cloneNode(actualNode.getElseNode()));
        ControlWorkflowNode node2 = new ControlWorkflowNode();
        node2.setCondition(ternaryExpression.getElseExpression());
        node2.setThenNode(cloneNode(actualNode.getThenNode()));
        node2.setElseNode(cloneNode(actualNode.getElseNode()));
        actualNode.setCondition(ternaryExpression.getExpression());
        actualNode.setThenNode(node1);
        actualNode.setElseNode(node2);
        expandActualNode(actualNode);
        return;
    } else if (infixExpression != null && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
        List<Expression> allOperands = ASTNodes.allOperands(infixExpression);
        Expression firstOperand = allOperands.remove(0);
        ControlWorkflowNode currentNode = actualNode;
        for (Expression operand : allOperands) {
            ControlWorkflowNode subNode = new ControlWorkflowNode();
            subNode.setCondition(operand);
            subNode.setThenNode(cloneNode(currentNode.getThenNode()));
            subNode.setElseNode(cloneNode(currentNode.getElseNode()));
            currentNode.setCondition(firstOperand);
            if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
                currentNode.setThenNode(subNode);
            } else {
                currentNode.setElseNode(subNode);
            }
            currentNode = subNode;
        }
        expandActualNode(actualNode);
        return;
    } else if (infixExpression != null && !infixExpression.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.XOR)) {
        ControlWorkflowNode subNode1 = new ControlWorkflowNode();
        subNode1.setCondition(infixExpression.getRightOperand());
        subNode1.setThenNode(cloneNode(actualNode.getElseNode()));
        subNode1.setElseNode(cloneNode(actualNode.getThenNode()));
        ControlWorkflowNode subNode2 = new ControlWorkflowNode();
        subNode2.setCondition(infixExpression.getRightOperand());
        subNode2.setThenNode(cloneNode(actualNode.getThenNode()));
        subNode2.setElseNode(cloneNode(actualNode.getElseNode()));
        actualNode.setCondition(infixExpression.getLeftOperand());
        actualNode.setThenNode(subNode1);
        actualNode.setElseNode(subNode2);
        expandActualNode(actualNode);
        return;
    }
    expandActualNode(actualNode.getThenNode());
    expandActualNode(actualNode.getElseNode());
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) 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 19 with PrefixExpression

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

the class RemoveParenthesisCleanUp method getExpressionWithoutParentheses.

private Expression getExpressionWithoutParentheses(final ParenthesizedExpression parenthesis) {
    ASTNode parent = parenthesis.getParent();
    Expression child = parenthesis.getExpression();
    if (isParenthesesUselessForParent(parent, parenthesis) || isParenthesesUselessForChild(child)) {
        return child;
    }
    if (parent instanceof InfixExpression) {
        InfixExpression parentInfixExpression = (InfixExpression) parent;
        if (child instanceof InfixExpression) {
            InfixExpression.Operator innerOp = ((InfixExpression) child).getOperator();
            if (innerOp == parentInfixExpression.getOperator() && OperatorEnum.isAssociative(innerOp) && // to other if statements in this method.
            Utils.equalNotNull(child.resolveTypeBinding(), parentInfixExpression.resolveTypeBinding())) {
                return child;
            }
        }
    }
    // Infix, prefix or postfix without parenthesis is not readable
    if (isInnerExprHardToRead(child, parent)) {
        return null;
    }
    if (parent instanceof InfixExpression && ASTNodes.hasOperator((InfixExpression) parent, InfixExpression.Operator.PLUS, InfixExpression.Operator.MINUS) || parent instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) parent, PrefixExpression.Operator.PLUS, PrefixExpression.Operator.MINUS)) {
        if (child instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) child, PrefixExpression.Operator.DECREMENT, PrefixExpression.Operator.INCREMENT, PrefixExpression.Operator.PLUS, PrefixExpression.Operator.MINUS) || child instanceof PostfixExpression && ASTNodes.hasOperator((PostfixExpression) child, PostfixExpression.Operator.DECREMENT, PostfixExpression.Operator.INCREMENT)) {
            return null;
        }
        if (child instanceof NumberLiteral && (((NumberLiteral) child).getToken().startsWith("+") || ((NumberLiteral) child).getToken().startsWith("-"))) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return null;
        }
    }
    int compareTo = OperatorEnum.compareTo(child, parent);
    if (compareTo < 0) {
        return null;
    }
    if (compareTo > 0) {
        return child;
    }
    if (// some like it like that
    child instanceof InfixExpression || child instanceof CastExpression || // Infix and prefix or postfix without parenthesis is not readable
    (parent instanceof InfixExpression || parent instanceof PrefixExpression || parent instanceof PostfixExpression) && (child instanceof PrefixExpression || child instanceof PostfixExpression)) {
        return null;
    }
    return child;
}
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) 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) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 20 with PrefixExpression

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

the class ASTNodeFactory method newPrefixExpression.

private PrefixExpression newPrefixExpression(final PrefixExpression.Operator operator, final Expression operand) {
    PrefixExpression pe = ast.newPrefixExpression();
    pe.setOperator(operator);
    pe.setOperand(operand);
    return pe;
}
Also used : PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Aggregations

PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)60 Expression (org.eclipse.jdt.core.dom.Expression)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)47 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)27 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)22 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)15 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 AST (org.eclipse.jdt.core.dom.AST)9 Assignment (org.eclipse.jdt.core.dom.Assignment)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)7 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 List (java.util.List)5 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)5