Search in sources :

Example 81 with Expression

use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.

the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.

private static boolean isReferringToLocalVariableFromFor(Expression expression) {
    ASTNode current = expression;
    ASTNode parent = current.getParent();
    while (parent != null && !(parent instanceof BodyDeclaration)) {
        if (parent instanceof ForStatement) {
            ForStatement forStmt = (ForStatement) parent;
            if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
                List<Expression> initializers = forStmt.initializers();
                if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
                    List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
                    ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
                    expression.accept(checker);
                    if (checker.isReferringToForVariable())
                        return true;
                }
            }
        }
        current = parent;
        parent = current.getParent();
    }
    return false;
}
Also used : 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) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 82 with Expression

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

the class PrimitiveWrapperCreationRefactoring method replaceFloatInstanceWithValueOf.

private boolean replaceFloatInstanceWithValueOf(ClassInstanceCreation node, final ITypeBinding typeBinding, final List<Expression> args) {
    final Expression arg0 = args.get(0);
    if (isPrimitive(arg0, "double")) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.invoke(typeBinding.getName(), "valueOf", b.cast(b.type("float"), b.copy(arg0))));
        return DO_NOT_VISIT_SUBTREE;
    } else if (hasType(arg0, "java.lang.Double")) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.invoke(b.copy(arg0), "floatValue"));
        return DO_NOT_VISIT_SUBTREE;
    } else {
        replaceWithValueOf(node, typeBinding);
        return DO_NOT_VISIT_SUBTREE;
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 83 with Expression

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

the class BooleanEqualsRatherThanNullCheckRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
        final Expression leftOperand = node.getLeftOperand();
        final Expression rightOperand = node.getRightOperand();
        final InfixExpression condition = as(leftOperand, InfixExpression.class);
        final boolean isNullCheck = hasOperator(condition, EQUALS);
        final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
        if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
            Expression firstExpr = null;
            if (isNullLiteral(condition.getLeftOperand())) {
                firstExpr = condition.getRightOperand();
            } else if (isNullLiteral(condition.getRightOperand())) {
                firstExpr = condition.getLeftOperand();
            }
            Expression secondExpr = null;
            final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
            final boolean isPositiveExpr;
            if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
                secondExpr = negateSecondExpr.getOperand();
                isPositiveExpr = false;
            } else {
                secondExpr = rightOperand;
                isPositiveExpr = true;
            }
            if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
                replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 84 with Expression

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

the class BooleanEqualsRatherThanNullCheckRefactoring method replaceNullCheck.

private void replaceNullCheck(final InfixExpression node, final Expression firstExpr, final boolean isNullCheck, final boolean isAndExpr, final boolean isPositiveExpr) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Name booleanConstant = b.name("Boolean", isAndExpr == isPositiveExpr ? "TRUE" : "FALSE");
    final MethodInvocation equalsMethod = b.invoke(booleanConstant, "equals", b.copy(firstExpr));
    Expression newExpr = null;
    if (!isNullCheck || isAndExpr) {
        newExpr = equalsMethod;
    } else {
        newExpr = b.not(equalsMethod);
    }
    ctx.getRefactorings().replace(node, newExpr);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) Name(org.eclipse.jdt.core.dom.Name)

Example 85 with Expression

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

the class BigDecimalRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    if (node.getExpression() == null) {
        return VISIT_SUBTREE;
    }
    if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigDecimal", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "double"))) {
        final ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
        final Expression arg0 = arg0(node);
        if (arg0 instanceof NumberLiteral) {
            final NumberLiteral nb = (NumberLiteral) arg0;
            if (nb.getToken().contains(".")) {
                this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), nb.getToken()));
            } else if (ZERO_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ZERO");
            } else if (ONE_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ONE");
            } else if (TEN_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "TEN");
            } else {
                return VISIT_SUBTREE;
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (!(node.getParent() instanceof PrefixExpression) || !hasOperator((PrefixExpression) node.getParent(), NOT)) {
        return maybeReplaceEquals(true, node, node);
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

Expression (org.eclipse.jdt.core.dom.Expression)552 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)263 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)213 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)187 CastExpression (org.eclipse.jdt.core.dom.CastExpression)185 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)135 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)126 ASTNode (org.eclipse.jdt.core.dom.ASTNode)125 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)122 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)121 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)112 AST (org.eclipse.jdt.core.dom.AST)101 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)95 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)88 SimpleName (org.eclipse.jdt.core.dom.SimpleName)83 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)76 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)73 Type (org.eclipse.jdt.core.dom.Type)70 ArrayList (java.util.ArrayList)69 Block (org.eclipse.jdt.core.dom.Block)63