Example usage for org.eclipse.jdt.core.dom InfixExpression getParent

List of usage examples for org.eclipse.jdt.core.dom InfixExpression getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom InfixExpression getParent.

Prototype

public final ASTNode getParent() 

Source Link

Document

Returns this node's parent node, or null if this is the root node.

Usage

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java

License:Open Source License

@Override
public boolean visit(InfixExpression node) {
    Integer operatorPrecedence = OPERATOR_PRECEDENCE.get(node.getOperator());
    if (operatorPrecedence == null)
        return true;
    ASTNode parent = node.getParent();
    if ((parent instanceof InfixExpression) && samePrecedence(node, (InfixExpression) parent))
        return true; // this node has been handled higher in the AST

    findTokensToWrap(node, 0);//from ww  w. j  a  va2 s. c o  m
    this.wrapParentIndex = this.wrapIndexes.remove(0);
    this.wrapGroupEnd = this.tm.lastIndexIn(node, -1);
    if ((this.options.alignment_for_binary_expression & Alignment.M_INDENT_ON_COLUMN) != 0
            && this.wrapParentIndex > 0)
        this.wrapParentIndex--;
    for (int i = this.wrapParentIndex; i >= 0; i--) {
        if (!this.tm.get(i).isComment()) {
            this.wrapParentIndex = i;
            break;
        }
    }
    handleWrap(this.options.alignment_for_binary_expression, node);
    return true;
}

From source file:net.sf.eclipsecs.ui.quickfixes.coding.StringLiteralEqualityQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}/*from w w w .  ja  va2 s. c  o  m*/
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartPosition) {

    return new ASTVisitor() {

        public boolean visit(InfixExpression node) {

            if (containsPosition(lineInfo, node.getStartPosition())) {

                StringLiteral literal = null;
                Expression otherOperand = null;

                if (node.getLeftOperand() instanceof StringLiteral) {
                    literal = (StringLiteral) node.getLeftOperand();
                    otherOperand = node.getRightOperand();
                } else if (node.getRightOperand() instanceof StringLiteral) {
                    literal = (StringLiteral) node.getRightOperand();
                    otherOperand = node.getLeftOperand();
                } else {
                    return true;
                }

                Expression replacementNode = null;

                MethodInvocation equalsInvocation = node.getAST().newMethodInvocation();
                equalsInvocation.setName(node.getAST().newSimpleName("equals")); //$NON-NLS-1$
                equalsInvocation.setExpression((Expression) ASTNode.copySubtree(node.getAST(), literal));
                equalsInvocation.arguments().add(ASTNode.copySubtree(node.getAST(), otherOperand));

                // if the string was compared with != create a not
                // expression
                if (node.getOperator().equals(InfixExpression.Operator.NOT_EQUALS)) {
                    PrefixExpression prefixExpression = node.getAST().newPrefixExpression();
                    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
                    prefixExpression.setOperand(equalsInvocation);
                    replacementNode = prefixExpression;
                } else {
                    replacementNode = equalsInvocation;
                }

                replaceNode(node, replacementNode);
            }
            return true;
        }

        /**
         * Replaces the given node with the replacement node (using
         * reflection since I am not aware of a proper API to do this).
         * 
         * @param node
         *            the node to replace
         * @param replacementNode
         *            the replacement
         */
        private void replaceNode(ASTNode node, ASTNode replacementNode) {

            try {
                if (node.getLocationInParent().isChildProperty()) {

                    String property = node.getLocationInParent().getId();

                    String setterMethodName = "set" + StringUtils.capitalize(property);

                    Class testClass = node.getClass();

                    while (testClass != null) {

                        try {
                            Method setterMethod = node.getParent().getClass().getMethod(setterMethodName,
                                    testClass);
                            setterMethod.invoke(node.getParent(), replacementNode);
                            break;
                        } catch (NoSuchMethodException e) {
                            testClass = testClass.getSuperclass();
                        }
                    }

                } else if (node.getLocationInParent().isChildListProperty()) {
                    Method listMethod = node.getParent().getClass()
                            .getMethod(node.getLocationInParent().getId(), (Class<?>[]) null);
                    List list = (List) listMethod.invoke(node.getParent(), (Object[]) null);
                    list.set(list.indexOf(node), replacementNode);
                }
            } catch (InvocationTargetException e) {
                CheckstyleLog.log(e);
            } catch (IllegalAccessException e) {
                CheckstyleLog.log(e);
            } catch (NoSuchMethodException e) {
                CheckstyleLog.log(e);
            }
        }
    };
}

From source file:org.autorefactor.refactoring.rules.SimplifyExpressionRefactoring.java

License:Open Source License

private boolean shouldHaveParentheses(InfixExpression node) {
    final Operator childOp = node.getOperator();
    if (node.getParent() instanceof InfixExpression) {
        final InfixExpression ie = (InfixExpression) node.getParent();
        return shouldHaveParentheses(childOp, ie.getOperator());
    }/*  w w  w  .j a  v a 2 s. c o m*/
    return false;
}

From source file:org.eclipse.xtend.core.javaconverter.JavaASTFlattener.java

License:Open Source License

@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("\'\'\'");
        }//from   w ww  .  ja  v  a2 s. c  o m
        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;
}