Example usage for org.eclipse.jdt.core.dom Expression subtreeMatch

List of usage examples for org.eclipse.jdt.core.dom Expression subtreeMatch

Introduction

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

Prototype

public final boolean subtreeMatch(ASTMatcher matcher, Object other) 

Source Link

Document

Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.

Usage

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

License:Open Source License

/**
 * The previous null check is redundant if:
 * <ul>/*from   w  w  w . j  a  va 2  s. c o  m*/
 * <li>the null checked expression is reused in an instanceof expression</li>
 * <li>the null checked expression is reused in an expression checking for
 * object equality against an expression that resolves to a non null
 * constant</li>
 * </ul>
 */
private boolean isNullCheckRedundant(Expression e, Expression nullCheckedExpression) {
    if (nullCheckedExpression == null) {
        return false;
    } else if (e instanceof InstanceofExpression) {
        final Expression expr = ((InstanceofExpression) e).getLeftOperand();
        return expr.subtreeMatch(new ASTMatcher(), nullCheckedExpression);
    } else if (e instanceof MethodInvocation) {
        final MethodInvocation expr = (MethodInvocation) e;
        if (expr.getExpression() != null && expr.getExpression().resolveConstantExpressionValue() != null
                && arguments(expr).size() == 1
                && arguments(expr).get(0).subtreeMatch(new ASTMatcher(), nullCheckedExpression)) {
            // Did we invoke java.lang.Object.equals() or java.lang.String.equalsIgnoreCase()?
            return isMethod(expr, "java.lang.Object", "equals", "java.lang.Object")
                    || isMethod(expr, "java.lang.String", "equalsIgnoreCase", "java.lang.String");
        }
    }
    return false;
}

From source file:org.decojer.cavaj.transformers.TrExpressions.java

License:Open Source License

/**
 * Handle pre increments / decrements via LOAD & LOAD/DUP -> ADD/SUB -> STORE.
 *
 * @param bb//from ww  w  . j a  va 2  s .co  m
 *            BB
 * @param assignment
 *            assignment
 * @return {@code true} - is rewritten pre increment / decrement
 */
private boolean rewriteStorePreIncDec(@Nonnull final BB bb, @Nonnull final Assignment assignment) {
    if (bb.isStackEmpty()) {
        return false;
    }
    final Expression leftHandSide = assignment.getLeftHandSide();
    final Expression peek = bb.peek();
    if (!leftHandSide.subtreeMatch(new ASTMatcher(), peek)) {
        return false;
    }
    Expression expression = assignment.getRightHandSide();
    if (expression instanceof CastExpression) {
        expression = ((CastExpression) expression).getExpression();
    }
    if (expression instanceof ParenthesizedExpression) {
        expression = ((ParenthesizedExpression) expression).getExpression();
    }
    if (!(expression instanceof InfixExpression)) {
        return false;
    }
    final InfixExpression infixExpression = (InfixExpression) expression;
    if (!infixExpression.getLeftOperand().subtreeMatch(new ASTMatcher(), peek)) {
        return false;
    }
    if (!(infixExpression.getRightOperand() instanceof NumberLiteral)) {
        return false;
    }
    if (!((NumberLiteral) infixExpression.getRightOperand()).getToken().equals("1")) {
        return false;
    }
    final InfixExpression.Operator operator = infixExpression.getOperator();
    if (operator == InfixExpression.Operator.MINUS || operator == InfixExpression.Operator.PLUS) {
        final PrefixExpression prefixExpression = getAst().newPrefixExpression();
        prefixExpression
                .setOperator(operator == InfixExpression.Operator.MINUS ? PrefixExpression.Operator.DECREMENT
                        : PrefixExpression.Operator.INCREMENT);
        prefixExpression.setOperand(wrap(bb.pop(), Priority.PREFIX_OR_POSTFIX));
        bb.push(prefixExpression);
        return true;
    }
    return false;
}