Search in sources :

Example 66 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class PushNegationDownRefactoring method visit.

@Override
public boolean visit(PrefixExpression node) {
    if (!hasOperator(node, NOT)) {
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    final Expression operand = removeParentheses(node.getOperand());
    if (operand instanceof PrefixExpression) {
        final PrefixExpression pe = (PrefixExpression) operand;
        if (hasOperator(pe, NOT)) {
            r.replace(node, b.move(pe.getOperand()));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (operand instanceof InfixExpression) {
        final InfixExpression ie = (InfixExpression) operand;
        final Operator reverseOp = (Operator) OperatorEnum.getOperator(ie).getReverseBooleanOperator();
        if (reverseOp != null) {
            List<Expression> allOperands = new ArrayList<Expression>(allOperands(ie));
            if (Arrays.<Operator>asList(CONDITIONAL_AND, CONDITIONAL_OR, AND, OR).contains(ie.getOperator())) {
                for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
                    it.set(b.negate(it.next()));
                }
            } else {
                allOperands = b.move(allOperands);
            }
            r.replace(node, b.parenthesize(b.infixExpr(reverseOp, allOperands)));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else {
        final Boolean constant = getBooleanLiteral(operand);
        if (constant != null) {
            r.replace(node, b.boolean0(!constant));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : ASTHelper.hasOperator(org.autorefactor.refactoring.ASTHelper.hasOperator) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) 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) Refactorings(org.autorefactor.refactoring.Refactorings) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ArrayList(java.util.ArrayList) List(java.util.List) ListIterator(java.util.ListIterator) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 67 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class ReduceVariableScopeRefactoring method getVariableDeclarationFragment.

private VariableDeclarationFragment getVariableDeclarationFragment(Expression exprToReplace, Name varName) {
    if (exprToReplace instanceof Assignment) {
        final Assignment a = (Assignment) exprToReplace;
        if (a.getLeftHandSide() instanceof SimpleName) {
            final SimpleName sn = (SimpleName) a.getLeftHandSide();
            if (sn.getFullyQualifiedName().equals(varName.getFullyQualifiedName())) {
                final ASTBuilder b = this.ctx.getASTBuilder();
                final VariableDeclarationFragment vdf = b.getAST().newVariableDeclarationFragment();
                vdf.setInitializer(b.copy(a.getRightHandSide()));
                vdf.setName(b.copy(sn));
                return vdf;
            }
        }
        throw new NotImplementedException(a.getLeftHandSide());
    }
    throw new NotImplementedException(exprToReplace);
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 68 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class RemoveEmptyIfRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Refactorings r = this.ctx.getRefactorings();
    final Statement thenStmt = node.getThenStatement();
    final Statement elseStmt = node.getElseStatement();
    if (elseStmt != null && asList(elseStmt).isEmpty()) {
        r.remove(elseStmt);
        return DO_NOT_VISIT_SUBTREE;
    } else if (thenStmt != null && asList(thenStmt).isEmpty()) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Expression condition = node.getExpression();
        if (elseStmt != null) {
            r.replace(node, b.if0(b.negate(condition), b.move(elseStmt)));
        } else if (isPassive(condition)) {
            removeBlock(node, r, b);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 69 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class ReplaceQualifiedNamesBySimpleNamesRefactoring method maybeReplaceFqnWithSimpleName.

private boolean maybeReplaceFqnWithSimpleName(final QualifiedName node, final Set<String> localIdentifiers) {
    final ASTNode ancestor = getFirstAncestorOrNull(node, PackageDeclaration.class, ImportDeclaration.class);
    final QName qname = getFullyQualifiedNameOrNull(node);
    if (ancestor != null || qname == null) {
        return VISIT_SUBTREE;
    }
    if (types.canReplaceFqnWithSimpleName(node, qname, FqnType.TYPE) || (fields.canReplaceFqnWithSimpleName(node, qname, FqnType.FIELD) && !localIdentifiers.contains(qname.simpleName))) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.copy(node.getName()));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 70 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class SimplifyExpressionRefactoring method replace.

private boolean replace(final InfixExpression node, final boolean isTrue, final Expression exprToCopy) {
    checkNoExtendedOperands(node);
    if (!isPrimitive(node.getLeftOperand(), "boolean") && !isPrimitive(node.getRightOperand(), "boolean")) {
        return VISIT_SUBTREE;
    }
    // Either:
    // - Two boolean primitives: no possible NPE
    // - One boolean primitive and one Boolean object, this code already run
    // the risk of an NPE, so we can replace the infix expression without
    // fearing we would introduce a previously non existing NPE.
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Expression operand;
    if (isTrue == hasOperator(node, EQUALS)) {
        operand = b.copy(exprToCopy);
    } else {
        operand = b.negate(exprToCopy);
    }
    this.ctx.getRefactorings().replace(node, operand);
    return DO_NOT_VISIT_SUBTREE;
}
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) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

ASTBuilder (org.autorefactor.refactoring.ASTBuilder)79 Expression (org.eclipse.jdt.core.dom.Expression)25 Refactorings (org.autorefactor.refactoring.Refactorings)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 Statement (org.eclipse.jdt.core.dom.Statement)9 Type (org.eclipse.jdt.core.dom.Type)9 Block (org.eclipse.jdt.core.dom.Block)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 ArrayList (java.util.ArrayList)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ASTHelper.hasType (org.autorefactor.refactoring.ASTHelper.hasType)4 NotImplementedException (org.autorefactor.util.NotImplementedException)4 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)4 ForStatement (org.eclipse.jdt.core.dom.ForStatement)4 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)4