Search in sources :

Example 41 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 42 with ASTBuilder

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

the class MergeConditionalBlocksRefactoring method maybeMergeBlocks.

private boolean maybeMergeBlocks(final Expression firstCondition, final List<Statement> ifCode, final IfStatement subNode, final Expression secondCondition, final Statement doubleStmts, final Statement remainingStmts, final boolean isPositive) {
    if (isSameCode(ifCode, asList(doubleStmts))) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Refactorings r = this.ctx.getRefactorings();
        final Expression additionalCondition;
        if (isPositive) {
            additionalCondition = b.copy(secondCondition);
        } else {
            additionalCondition = b.negate(secondCondition, Copy.COPY);
        }
        r.replace(firstCondition, b.infixExpr(b.parenthesizeIfNeeded(b.copy(firstCondition)), InfixExpression.Operator.CONDITIONAL_OR, b.parenthesizeIfNeeded(additionalCondition)));
        if (remainingStmts != null) {
            r.replace(subNode, b.copy(remainingStmts));
        } else {
            r.remove(subNode);
        }
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 43 with ASTBuilder

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

the class StringValueOfRatherThanConcatRefactoring method maybeReplaceStringConcatenation.

private boolean maybeReplaceStringConcatenation(final InfixExpression node, final Expression expr, final Expression variable) {
    if (expr instanceof StringLiteral && ((StringLiteral) expr).getLiteralValue().matches("") && !hasType(variable, "java.lang.String", "char[]")) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.invoke("String", "valueOf", b.copy(variable)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 44 with ASTBuilder

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

the class UseStringContainsRefactoring method replaceWithStringContains.

private boolean replaceWithStringContains(InfixExpression ie, MethodInvocation node, boolean negate) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    r.set(node, MethodInvocation.NAME_PROPERTY, b.simpleName("contains"));
    if (negate) {
        r.replace(ie, b.not(b.move(node)));
    } else {
        r.replace(ie, b.move(node));
    }
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) 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