Search in sources :

Example 61 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 62 with InfixExpression

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

the class AndroidViewHolderCleanUp method visit.

@Override
public boolean visit(final MethodDeclaration node) {
    Block body = node.getBody();
    if (body != null && // $NON-NLS-1$//$NON-NLS-2$
    ASTNodes.usesGivenSignature(// $NON-NLS-1$//$NON-NLS-2$
    node, // $NON-NLS-1$//$NON-NLS-2$
    "android.widget.Adapter", // $NON-NLS-1$//$NON-NLS-2$
    "getView", // $NON-NLS-1$
    int.class.getSimpleName(), // $NON-NLS-1$
    "android.view.View", "android.view.ViewGroup")) {
        // $NON-NLS-1$
        GetViewVariableVisitor visitor = new GetViewVariableVisitor();
        body.accept(visitor);
        if (visitor.canApplyRefactoring()) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidViewHolderCleanUp_description);
            TypeNameDecider typeNameDecider = new TypeNameDecider(visitor.viewVariableName);
            // Transform tree
            // Create If statement
            SingleVariableDeclaration viewArg = (SingleVariableDeclaration) node.parameters().get(1);
            Variable convertViewVar = new Variable(viewArg.getName().getIdentifier(), ast);
            InfixExpression newInfixExpression = ast.newInfixExpression();
            newInfixExpression.setLeftOperand(convertViewVar.varName());
            newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
            newInfixExpression.setRightOperand(ast.newNullLiteral());
            InfixExpression condition = newInfixExpression;
            Block thenBlock = ast.newBlock();
            IfStatement newIfStatement = ast.newIfStatement();
            newIfStatement.setExpression(condition);
            newIfStatement.setThenStatement(thenBlock);
            rewrite.insertBefore(newIfStatement, visitor.viewAssignmentStatement, group);
            List<Statement> thenStatements = thenBlock.statements();
            thenStatements.add(ast.newExpressionStatement(ast.newAssignment(convertViewVar.varName(), Assignment.Operator.ASSIGN, ast.createCopyTarget(visitor.getInflateExpression()))));
            // Assign to local view variable when necessary
            if (!"convertView".equals(visitor.viewVariableName.getIdentifier())) {
                // $NON-NLS-1$
                Statement assignConvertViewToView = null;
                if (visitor.viewVariableDeclFragment != null) {
                    assignConvertViewToView = ast.declareStatement(ast.copyType(visitor.viewVariableName, typeNameDecider), ast.createCopyTarget(visitor.viewVariableName), convertViewVar.varName());
                } else if (visitor.viewVariableAssignment != null) {
                    assignConvertViewToView = ast.newExpressionStatement(ast.newAssignment(ast.createCopyTarget(visitor.viewVariableName), Assignment.Operator.ASSIGN, convertViewVar.varName()));
                }
                if (assignConvertViewToView != null) {
                    rewrite.insertBefore(assignConvertViewToView, visitor.viewAssignmentStatement, group);
                }
            }
            // Make sure method returns the view to be reused
            if (visitor.returnStatement != null) {
                rewrite.insertAfter(ast.newReturnStatement(ast.createCopyTarget(visitor.viewVariableName)), visitor.returnStatement, group);
                rewrite.remove(visitor.returnStatement, group);
            }
            // Optimize findViewById calls
            FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor(visitor.viewVariableName);
            body.accept(findViewByIdVisitor);
            if (!findViewByIdVisitor.items.isEmpty()) {
                // TODO JNR name conflict? Use VariableNameDecider
                // $NON-NLS-1$ //$NON-NLS-2$
                Variable viewHolderItemVar = new Variable("ViewHolderItem", "viewHolderItem", ast);
                // Create ViewHolderItem class
                rewrite.insertBefore(createViewHolderItemClass(findViewByIdVisitor, viewHolderItemVar.typeName(), typeNameDecider), node, group);
                // Declare viewhHolderItem object
                rewrite.insertFirst(body, Block.STATEMENTS_PROPERTY, viewHolderItemVar.declareStatement(), group);
                // Initialize viewHolderItem
                thenStatements.add(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN, ast.newClassInstanceCreation(viewHolderItemVar.type()))));
                // Assign findViewById to ViewHolderItem
                for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
                    // Ensure we are accessing convertView object
                    FieldAccess fieldAccess = ast.newFieldAccess(viewHolderItemVar.varName(), ast.newSimpleName(item.variable.getIdentifier()));
                    // FIXME This does not work: not sure why??
                    // rewrite.set(item.findViewByIdInvocation,
                    // MethodInvocation.EXPRESSION_PROPERTY, convertViewVar.varName());
                    item.findViewByIdInvocation.setExpression(convertViewVar.varName());
                    // FIXME For some reason ast.copy() does not do what we would like
                    thenStatements.add(ast.newExpressionStatement(ast.newAssignment(fieldAccess, Assignment.Operator.ASSIGN, ast.copySubtree(item.findViewByIdExpression))));
                    // Replace previous findViewById with accesses to viewHolderItem
                    rewrite.replace(item.findViewByIdExpression, ast.createCopyTarget(fieldAccess), group);
                }
                MethodInvocation setTagMethod = ast.newMethodInvocation();
                // $NON-NLS-1$
                setTagMethod.setExpression(ASTNodeFactory.newName(ast, "convertView"));
                // $NON-NLS-1$
                setTagMethod.setName(ast.newSimpleName("setTag"));
                setTagMethod.arguments().add(viewHolderItemVar.varName());
                // Store viewHolderItem in convertView
                thenStatements.add(ast.newExpressionStatement(setTagMethod));
                Block newBlock = ast.newBlock();
                MethodInvocation getTagMethod = ast.newMethodInvocation();
                // $NON-NLS-1$
                getTagMethod.setExpression(ASTNodeFactory.newName(ast, "convertView"));
                // $NON-NLS-1$
                getTagMethod.setName(ast.newSimpleName("getTag"));
                newBlock.statements().add(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN, ast.newCastExpression(viewHolderItemVar.type(), getTagMethod))));
                // Retrieve viewHolderItem from convertView
                newIfStatement.setElseStatement(newBlock);
            }
            rewrite.remove(visitor.viewAssignmentStatement, group);
            return false;
        }
    }
    return true;
}
Also used : Variable(org.autorefactor.jdt.internal.corext.dom.Variable) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 63 with InfixExpression

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

the class AssignRatherThanTernaryFilterThenAssignAnywayCleanUp method visit.

@Override
public boolean visit(final ConditionalExpression visited) {
    InfixExpression condition = ASTNodes.as(visited.getExpression(), InfixExpression.class);
    if (condition != null && ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS) && !condition.hasExtendedOperands()) {
        Expression hardCodedExpression;
        Expression valuedExpression;
        if (ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS)) {
            hardCodedExpression = visited.getThenExpression();
            valuedExpression = visited.getElseExpression();
        } else {
            hardCodedExpression = visited.getElseExpression();
            valuedExpression = visited.getThenExpression();
        }
        if (ASTNodes.isHardCoded(hardCodedExpression) && ASTNodes.isPassive(valuedExpression)) {
            return maybeReplaceWithValue(visited, hardCodedExpression, valuedExpression, condition.getRightOperand(), condition.getLeftOperand()) && maybeReplaceWithValue(visited, hardCodedExpression, valuedExpression, condition.getLeftOperand(), condition.getRightOperand());
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 64 with InfixExpression

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

the class BooleanCleanUp method newExpressionOrNull.

private Expression newExpressionOrNull(final ITypeBinding typeBinding, final Expression condition, final Expression thenExpression, final Expression elseExpression) {
    Boolean thenLiteral = ASTNodes.getBooleanLiteral(thenExpression);
    Boolean elseLiteral = ASTNodes.getBooleanLiteral(elseExpression);
    if (areNegatedBooleanValues(thenExpression, elseExpression)) {
        return getExpression(signExpression(condition, thenLiteral), typeBinding.getQualifiedName(), condition);
    }
    if ((ASTNodes.isPrimitive(thenExpression) || ASTNodes.isPrimitive(elseExpression)) && ASTNodes.hasType(typeBinding, boolean.class.getCanonicalName(), Boolean.class.getCanonicalName())) {
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        // care
        if (thenLiteral != null && elseLiteral == null) {
            InfixExpression newInfixExpression = ast.newInfixExpression();
            newInfixExpression.setLeftOperand(signExpression(condition, thenLiteral));
            newInfixExpression.setOperator(getConditionalOperator(thenLiteral));
            newInfixExpression.setRightOperand(ast.createCopyTarget(elseExpression));
            return newInfixExpression;
        }
        if (thenLiteral == null && elseLiteral != null) {
            InfixExpression newInfixExpression = ast.newInfixExpression();
            newInfixExpression.setLeftOperand(signExpression(condition, !elseLiteral));
            newInfixExpression.setOperator(getConditionalOperator(elseLiteral));
            newInfixExpression.setRightOperand(ast.createCopyTarget(thenExpression));
            return newInfixExpression;
        }
    }
    return null;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 65 with InfixExpression

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

the class BooleanEqualsRatherThanNullCheckCleanUp method replaceNullCheck.

private void replaceNullCheck(final InfixExpression visited, final Expression firstExpression, final boolean isNullCheck, final boolean isAndExpression, final boolean isPositiveExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.BooleanEqualsRatherThanNullCheckCleanUp_description);
    // $NON-NLS-1$ //$NON-NLS-2$
    Name booleanConstant = ASTNodeFactory.newName(ast, Boolean.class.getSimpleName(), isAndExpression == isPositiveExpression ? "TRUE" : "FALSE");
    MethodInvocation equalsMethod = ast.newMethodInvocation();
    equalsMethod.setExpression(booleanConstant);
    // $NON-NLS-1$
    equalsMethod.setName(ast.newSimpleName("equals"));
    equalsMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(firstExpression)));
    Expression newExpression;
    if (!isNullCheck || isAndExpression) {
        newExpression = equalsMethod;
    } else {
        newExpression = ast.not(equalsMethod);
    }
    rewrite.replace(visited, newExpression, group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Name(org.eclipse.jdt.core.dom.Name)

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