Search in sources :

Example 26 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class DoWhileRatherThanWhileCleanUp method visit.

@Override
public boolean visit(final WhileStatement visited) {
    if (ASTNodes.isPassiveWithoutFallingThrough(visited.getExpression()) && Boolean.TRUE.equals(peremptoryValue(visited, visited.getExpression()))) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.DoWhileRatherThanWhileCleanUp_description);
        ASTNodes.replaceButKeepComment(rewrite, visited, ast.newDoStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())), ASTNodes.createMoveTarget(rewrite, visited.getBody())), group);
        return false;
    }
    return true;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 27 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class EntrySetRatherThanKeySetAndValueSearchCleanUp method replaceEntryIterationByKeyIteration.

private void replaceEntryIterationByKeyIteration(final EnhancedForStatement enhancedFor, final Expression mapExpression, final SingleVariableDeclaration parameter, final List<MethodInvocation> getValueMis) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.EntrySetRatherThanKeySetAndValueSearchCleanUp_description);
    VarDefinitionsUsesVisitor keyUseVisitor = new VarDefinitionsUsesVisitor(parameter.resolveBinding(), enhancedFor.getBody(), true);
    int keyUses = keyUseVisitor.getReads().size();
    int insertionPoint = ASTNodes.asList(enhancedFor.getBody()).get(0).getStartPosition() - 1;
    // $NON-NLS-1$ //$NON-NLS-2$
    String entryVar = new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("entry", "mapEntry");
    TypeNameDecider typeNameDecider = new TypeNameDecider(parameter);
    MethodInvocation getValueMi0 = getValueMis.get(0);
    ITypeBinding typeBinding = getValueMi0.getExpression().resolveTypeBinding();
    MethodInvocation entrySetMethod = ast.newMethodInvocation();
    entrySetMethod.setExpression(ASTNodes.createMoveTarget(rewrite, mapExpression));
    // $NON-NLS-1$
    entrySetMethod.setName(ast.newSimpleName("entrySet"));
    rewrite.set(enhancedFor, EnhancedForStatement.EXPRESSION_PROPERTY, entrySetMethod, group);
    MethodInvocation getKeyMethod = ast.newMethodInvocation();
    getKeyMethod.setExpression(ast.newSimpleName(entryVar));
    // $NON-NLS-1$
    getKeyMethod.setName(ast.newSimpleName("getKey"));
    if (typeBinding != null && typeBinding.isRawType()) {
        // for (Object key : map.keySet()) => for (Object key : map.entrySet())
        refactorRawMap(enhancedFor, parameter, getValueMis, rewrite, ast, group, keyUses, insertionPoint, entryVar, typeNameDecider, getKeyMethod);
    } else {
        // for (K key : map.keySet()) => for (K key : map.entrySet())
        // for (K key : map.entrySet()) => for (Map.Entry<K, V> mapEntry :
        // map.entrySet())
        refactorGenericMap(enhancedFor, parameter, getValueMis, rewrite, ast, group, keyUses, entryVar, typeNameDecider, getValueMi0, getKeyMethod);
    }
    // Replace all occurrences of map.get(key) => mapEntry.getValue()
    for (MethodInvocation getValueMi : getValueMis) {
        MethodInvocation getValueMethod = ast.newMethodInvocation();
        getValueMethod.setExpression(ast.newSimpleName(entryVar));
        // $NON-NLS-1$
        getValueMethod.setName(ast.newSimpleName("getValue"));
        MethodInvocation newMethodInvocation = getValueMethod;
        rewrite.replace(getValueMi, newMethodInvocation, group);
    }
}
Also used : VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 28 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class EnumMapRatherThanHashMapCleanUp method replace.

private void replace(final ClassInstanceCreation classInstanceCreation, final Set<String> alreadyImportedClasses, final Type keyType, final Type valueType, final List<Expression> arguments) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    Expression newParam = resolveParameter(keyType, arguments);
    Type newType = ast.newParameterizedType(alreadyImportedClasses.contains(EnumMap.class.getCanonicalName()) ? EnumMap.class.getSimpleName() : EnumMap.class.getCanonicalName(), ast.createCopyTarget(keyType), ast.createCopyTarget(valueType));
    // remove them from replacement
    if (typeArgs(classInstanceCreation.getType()).isEmpty()) {
        typeArgs(newType).clear();
    }
    TextEditGroup group = new TextEditGroup(MultiFixMessages.EnumMapRatherThanHashMapCleanUp_description);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    rewrite.replace(classInstanceCreation, ast.newClassInstanceCreation(newType, newParam), group);
}
Also used : Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) EnumMap(java.util.EnumMap) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 29 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class EnumSetRatherThanHashSetCleanUp method maybeReplace.

/**
 * Cleanup 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>
 *
 * @param classInstanceCreation  - class instance creation node to be replaced
 * @param type - type argument of the declaration
 * @see java.util.EnumSet#noneOf(Class) <br>
 */
@Override
boolean maybeReplace(final ClassInstanceCreation classInstanceCreation, final Set<String> alreadyImportedClasses, final Set<String> importsToAdd, final Type... types) {
    if (types == null || types.length == 0) {
        return true;
    }
    Type type = types[0];
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    List<Expression> arguments = classInstanceCreation.arguments();
    MethodInvocation invocation;
    Name newClassName = ASTNodeFactory.newName(ast, alreadyImportedClasses.contains(EnumSet.class.getCanonicalName()) ? EnumSet.class.getSimpleName() : EnumSet.class.getCanonicalName());
    if (!arguments.isEmpty() && ASTNodes.instanceOf(arguments.get(0), Collection.class.getCanonicalName())) {
        Expression typeArg = arguments.get(0);
        if (!ASTNodes.instanceOf(typeArg, EnumSet.class.getCanonicalName())) {
            return true;
        }
        invocation = ast.newMethodInvocation();
        invocation.setExpression(newClassName);
        // $NON-NLS-1$
        invocation.setName(ast.newSimpleName("copyOf"));
        invocation.arguments().add(ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(typeArg)));
    } else {
        TypeLiteral newTypeLiteral = cuRewrite.getAST().newTypeLiteral();
        newTypeLiteral.setType(ast.createCopyTarget(type));
        invocation = ast.newMethodInvocation();
        invocation.setExpression(newClassName);
        // $NON-NLS-1$
        invocation.setName(ast.newSimpleName("noneOf"));
        invocation.arguments().add(newTypeLiteral);
    }
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.EnumSetRatherThanHashSetCleanUp_description);
    rewrite.replace(classInstanceCreation, invocation, group);
    importsToAdd.add(EnumSet.class.getCanonicalName());
    return false;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) EnumSet(java.util.EnumSet) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Name(org.eclipse.jdt.core.dom.Name)

Example 30 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteBracketsRatherThanArrayInstantiationCleanUp method refactorWithInitializer.

private void refactorWithInitializer(final ArrayCreation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteBracketsRatherThanArrayInstantiationCleanUp_description);
    if (visited.getInitializer() != null) {
        ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, visited.getInitializer()), group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, visited, ast.newArrayInitializer(), group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)152 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)133 TextEditGroup (org.eclipse.text.edits.TextEditGroup)115 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 Expression (org.eclipse.jdt.core.dom.Expression)44 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)37 ArrayList (java.util.ArrayList)19 Block (org.eclipse.jdt.core.dom.Block)17 Statement (org.eclipse.jdt.core.dom.Statement)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)16 Type (org.eclipse.jdt.core.dom.Type)14 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 List (java.util.List)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Name (org.eclipse.jdt.core.dom.Name)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6