Search in sources :

Example 31 with ASTBuilder

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

the class RemoveUnnecessaryCastRefactoring method createPrimitive.

private void createPrimitive(final CastExpression node, final NumberLiteral literal, final char postfix) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final NumberLiteral numberLiteral = b.numberLiteral();
    numberLiteral.setToken(literal.getToken() + postfix);
    ctx.getRefactorings().replace(node, numberLiteral);
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 32 with ASTBuilder

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

the class RemoveUselessBlockRefactoring method replaceBlock.

@SuppressWarnings("unchecked")
private void replaceBlock(final Block node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    r.replace(node, b.copyRange(node.statements()));
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 33 with ASTBuilder

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

the class SimplifyExpressionRefactoring method replaceBy.

private boolean replaceBy(ASTNode node, Expression expr) {
    final ASTBuilder b = ctx.getASTBuilder();
    ctx.getRefactorings().replace(node, b.move(expr));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 34 with ASTBuilder

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

the class SimplifyExpressionRefactoring method addParentheses.

private void addParentheses(Expression e) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    this.ctx.getRefactorings().replace(e, b.parenthesize(b.copy(e)));
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 35 with ASTBuilder

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

the class SimplifyExpressionRefactoring method maybeReduceBooleanExpression.

private boolean maybeReduceBooleanExpression(final InfixExpression node, final Expression leftExpr, final Expression rightExpr) {
    final Boolean leftBoolean = getBooleanLiteral(leftExpr);
    final Boolean rightBoolean = getBooleanLiteral(rightExpr);
    if (leftBoolean != null) {
        return replace(node, leftBoolean.booleanValue(), rightExpr);
    } else if (rightBoolean != null) {
        return replace(node, rightBoolean.booleanValue(), leftExpr);
    }
    Expression leftOppositeExpr = null;
    final PrefixExpression leftPrefix = as(leftExpr, PrefixExpression.class);
    if (leftPrefix != null && hasOperator(leftPrefix, NOT)) {
        leftOppositeExpr = leftPrefix.getOperand();
    }
    Expression rightOppositeExpr = null;
    final PrefixExpression rightPrefix = as(rightExpr, PrefixExpression.class);
    if (rightPrefix != null && hasOperator(rightPrefix, NOT)) {
        rightOppositeExpr = rightPrefix.getOperand();
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    if (leftOppositeExpr != null && rightOppositeExpr != null) {
        r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), getAppropriateOperator(node), b.copy(rightOppositeExpr)));
        return DO_NOT_VISIT_SUBTREE;
    } else if (leftOppositeExpr != null) {
        final Operator reverseOp = getReverseOperator(node);
        r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), reverseOp, b.copy(rightExpr)));
        return DO_NOT_VISIT_SUBTREE;
    } else if (rightOppositeExpr != null) {
        final Operator reverseOp = getReverseOperator(node);
        r.replace(node, b.infixExpr(b.copy(leftExpr), reverseOp, b.copy(rightOppositeExpr)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) 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