Search in sources :

Example 61 with ASTBuilder

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

the class AndroidViewHolderRefactoring method createViewHolderItemClass.

private TypeDeclaration createViewHolderItemClass(FindViewByIdVisitor findViewByIdVisitor, SimpleName typeName, TypeNameDecider typeNameDecider) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    TypeDeclaration result = b.getAST().newTypeDeclaration();
    modifiers(result).addAll(asList(b.private0(), b.static0()));
    result.setName(typeName);
    List<BodyDeclaration> viewItemsFieldDecls = bodyDeclarations(result);
    for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
        viewItemsFieldDecls.add(item.toFieldDecl(b, typeNameDecider));
    }
    return result;
}
Also used : BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 62 with ASTBuilder

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

the class CommonCodeInIfElseStatementRefactoring method visit.

// TODO handle switch statements
// TODO also handle ternary operator, ConditionalExpression
@Override
public boolean visit(IfStatement node) {
    if (node.getElseStatement() == null) {
        return VISIT_SUBTREE;
    }
    if (!(node.getParent() instanceof Block)) {
        // when not inside curly braces
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final List<List<Statement>> allCasesStmts = new ArrayList<List<Statement>>();
    final List<List<ASTNode>> removedCaseStmts = new LinkedList<List<ASTNode>>();
    // Collect all the if / else if / else if / ... / else cases
    if (collectAllCases(allCasesStmts, node)) {
        // initialize removedCaseStmts list
        for (int i = 0; i < allCasesStmts.size(); i++) {
            removedCaseStmts.add(new LinkedList<ASTNode>());
        }
        // If all cases exist
        final ASTMatcher matcher = new ASTMatcherSameVariablesAndMethods();
        final int minSize = minSize(allCasesStmts);
        final List<Statement> caseStmts = allCasesStmts.get(0);
        boolean result = VISIT_SUBTREE;
        // Identify matching statements starting from the beginning of each case
        for (int stmtIndex = 0; stmtIndex < minSize; stmtIndex++) {
            if (!match(matcher, allCasesStmts, true, stmtIndex, 0, allCasesStmts.size())) {
                break;
            }
            r.insertBefore(b.copy(caseStmts.get(stmtIndex)), node);
            removeStmts(allCasesStmts, true, stmtIndex, removedCaseStmts);
            result = DO_NOT_VISIT_SUBTREE;
        }
        // Identify matching statements starting from the end of each case
        for (int stmtIndex = 1; 0 <= minSize - stmtIndex; stmtIndex++) {
            if (!match(matcher, allCasesStmts, false, stmtIndex, 0, allCasesStmts.size()) || anyContains(removedCaseStmts, allCasesStmts, stmtIndex)) {
                break;
            }
            r.insertAfter(b.copy(caseStmts.get(caseStmts.size() - stmtIndex)), node);
            removeStmts(allCasesStmts, false, stmtIndex, removedCaseStmts);
            result = DO_NOT_VISIT_SUBTREE;
        }
        // Remove the nodes common to all cases
        final boolean[] areCasesEmpty = new boolean[allCasesStmts.size()];
        for (int i = 0; i < allCasesStmts.size(); i++) {
            areCasesEmpty[i] = false;
        }
        removeStmtsFromCases(allCasesStmts, removedCaseStmts, areCasesEmpty);
        if (allEmpty(areCasesEmpty)) {
            r.removeButKeepComment(node);
            return DO_NOT_VISIT_SUBTREE;
        }
        // Remove empty cases
        if (areCasesEmpty[0]) {
            if (areCasesEmpty.length == 2 && !areCasesEmpty[1]) {
                // Then clause is empty and there is only one else clause
                // => revert if statement
                r.replace(node, b.if0(b.not(b.parenthesizeIfNeeded(b.move(node.getExpression()))), b.move(node.getElseStatement())));
            } else {
                r.replace(node.getThenStatement(), b.block());
            }
            result = DO_NOT_VISIT_SUBTREE;
        }
        for (int i = 1; i < areCasesEmpty.length; i++) {
            if (areCasesEmpty[i]) {
                final Statement firstStmt = allCasesStmts.get(i).get(0);
                r.remove(findNodeToRemove(firstStmt, firstStmt.getParent()));
                result = DO_NOT_VISIT_SUBTREE;
            }
        }
        return result;
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) ArrayList(java.util.ArrayList) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) LinkedList(java.util.LinkedList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ASTMatcherSameVariablesAndMethods(org.autorefactor.refactoring.ASTMatcherSameVariablesAndMethods) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 63 with ASTBuilder

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

the class EnumMapRatherThanHashMapRefactoring method replace.

/**
 * Replace given class instance creation with suitable EnumMap constructor. <br>
 * <br>
 * Replacement is not correct if HashMap constructor accepts map <br>
 * other than EnumMap, because it throws <code>IllegalArgumentException</code> if map is empty,
 * <br>
 * and HashMap(Map) does not. Therefore, for correctness reasons, it should not be refactored.
 * <br>
 *
 * @see {@link java.util.EnumMap#EnumMap(java.util.Map)}
 * @see {@link java.util.HashMap#HashMap(java.util.Map)}
 */
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
    if (types == null || types.length < 2) {
        return VISIT_SUBTREE;
    }
    Type keyType = types[0];
    Type valueType = types[1];
    ASTBuilder b = ctx.getASTBuilder();
    List<Expression> arguments = arguments(cic);
    if (!arguments.isEmpty() && isTargetType(arguments.get(0).resolveTypeBinding()) && !hasType(arguments.get(0).resolveTypeBinding(), "java.util.EnumMap")) {
        return VISIT_SUBTREE;
    }
    Expression newParam = resolveParameter(keyType, arguments);
    Type newType = b.genericType("java.util.EnumMap", b.copy(keyType), b.copy(valueType));
    // remove them from replacement
    if (typeArgs(cic.getType()).isEmpty()) {
        typeArgs(newType).clear();
    }
    ctx.getRefactorings().replace(cic, b.new0(newType, newParam));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ASTHelper.hasType(org.autorefactor.refactoring.ASTHelper.hasType) Expression(org.eclipse.jdt.core.dom.Expression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 64 with ASTBuilder

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

the class IfRatherThanWhileAndFallsThroughRefactoring method visit.

@Override
public boolean visit(WhileStatement node) {
    if (isEndingWithExit(node.getBody())) {
        final BreakVisitor breakVisitor = new BreakVisitor(node);
        breakVisitor.visitNode(node);
        if (breakVisitor.canBeRefactored()) {
            final ASTBuilder b = ctx.getASTBuilder();
            for (final BreakStatement breakStmt : breakVisitor.getBreaks()) {
                ctx.getRefactorings().remove(breakStmt);
            }
            ctx.getRefactorings().replace(node, b.if0(b.copy(node.getExpression()), b.copy(node.getBody())));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 65 with ASTBuilder

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

the class InlineCodeRatherThanPeremptoryConditionRefactoring method replaceBlockByPlainCode.

@SuppressWarnings("unchecked")
private void replaceBlockByPlainCode(final Statement sourceNode, final Statement unconditionnalStatement) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    if (unconditionnalStatement instanceof Block && sourceNode.getParent() instanceof Block) {
        r.replace(sourceNode, b.copyRange(((Block) unconditionnalStatement).statements()));
    } else {
        r.replace(sourceNode, b.copy(unconditionnalStatement));
    }
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) Block(org.eclipse.jdt.core.dom.Block) 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