Search in sources :

Example 11 with ASTBuilder

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

the class VectorOldToNewAPIRefactoring method replaceWithSpecial.

private void replaceWithSpecial(final MethodInvocation node, final String newMethodName) {
    final List<Expression> args = arguments(node);
    assertSize(args, 1);
    final Expression arg0 = args.get(0);
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    r.set(node, NAME_PROPERTY, b.simpleName(newMethodName));
    if (hasType(arg0, "int", "short", "byte")) {
        r.replace(arg0, b.cast(b.type("Object"), b.move(arg0)));
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 12 with ASTBuilder

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

the class DoWhileRatherThanWhileRefactoring method visit.

@Override
public boolean visit(WhileStatement node) {
    final Object constantCondition = node.getExpression().resolveConstantExpressionValue();
    if (Boolean.TRUE.equals(constantCondition)) {
        ASTBuilder b = this.ctx.getASTBuilder();
        this.ctx.getRefactorings().replace(node, b.doWhile(b.copy(node.getExpression()), b.copy(node.getBody())));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 13 with ASTBuilder

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

the class EnumSetRatherThanHashSetRefactoring method replace.

/**
     * Refactoring is not correct if argument for HashSet constructor is a Collection, but other
     * than EnumSet. <br>
     * In case of empty collection <code>EnumSet.copyOf</code> will throw an
     * <code>IllegalArgumentException</code>, <br>
     * and HashSet(Collection) will not. <br>
     * <br>
     * Other constructors can be replaced with <code>EnumSet.noneOf(Class)</code> method. <br>
     * <br>
     *
     * @see {@link java.util.EnumSet#copyOf(Collection)}
     * @see {@link java.util.EnumSet#copyOf(EnumSet)}
     * @see {@link java.util.EnumSet#noneOf(Class)} <br>
     * @param cic
     *            - class instance creation node to be replaced
     * @param type
     *            - type argument of the declaration
     */
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
    if (types == null || types.length < 1) {
        return VISIT_SUBTREE;
    }
    Type type = types[0];
    ASTBuilder b = ctx.getASTBuilder();
    List<Expression> arguments = arguments(cic);
    final MethodInvocation invocation;
    if (!arguments.isEmpty() && instanceOf(arguments.get(0), "java.util.Collection")) {
        Expression typeArg = arguments.get(0);
        if (!instanceOf(typeArg, "java.util.EnumSet")) {
            return VISIT_SUBTREE;
        }
        invocation = b.invoke(b.name("java", "util", "EnumSet"), "copyOf", b.copy(typeArg));
    } else {
        TypeLiteral newTypeLiteral = ctx.getAST().newTypeLiteral();
        newTypeLiteral.setType(b.copy(type));
        invocation = b.invoke(b.name("java", "util", "EnumSet"), "noneOf", newTypeLiteral);
    }
    ctx.getRefactorings().replace(cic, invocation);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 14 with ASTBuilder

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

the class RemoveUnnecessaryCastRefactoring method visit.

@Override
public boolean visit(CastExpression node) {
    if (canRemoveCast(node)) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.move(node.getExpression()));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 15 with ASTBuilder

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

the class CollapseIfStatementRefactoring method replaceIfNoElseStatement.

private boolean replaceIfNoElseStatement(IfStatement outerIf, IfStatement innerIf) {
    if (innerIf.getElseStatement() != null) {
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final InfixExpression ie = b.infixExpr(parenthesizeOrExpr(b, outerIf.getExpression()), CONDITIONAL_AND, parenthesizeOrExpr(b, innerIf.getExpression()));
    this.ctx.getRefactorings().replace(outerIf.getExpression(), ie);
    this.ctx.getRefactorings().replace(outerIf.getThenStatement(), b.copy(innerIf.getThenStatement()));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

ASTBuilder (org.autorefactor.refactoring.ASTBuilder)44 Expression (org.eclipse.jdt.core.dom.Expression)16 Refactorings (org.autorefactor.refactoring.Refactorings)14 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)4 Statement (org.eclipse.jdt.core.dom.Statement)4 List (java.util.List)3 Block (org.eclipse.jdt.core.dom.Block)3 Type (org.eclipse.jdt.core.dom.Type)3 ArrayList (java.util.ArrayList)2 ASTHelper.hasType (org.autorefactor.refactoring.ASTHelper.hasType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Name (org.eclipse.jdt.core.dom.Name)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1