Search in sources :

Example 31 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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 32 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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 33 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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 34 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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)

Example 35 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class IsEmptyRatherThanSizeCleanUp method replaceMethod.

private void replaceMethod(final InfixExpression visited, MethodInvocation miToReplace, boolean isEmpty) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.IsEmptyRatherThanSizeCleanUp_description);
    MethodInvocation newMethodInvocation = ast.newMethodInvocation();
    newMethodInvocation.setExpression(ast.copyExpression(miToReplace));
    newMethodInvocation.setName(ast.newSimpleName(IS_EMPTY_METHOD));
    rewrite.replace(visited, isEmpty ? newMethodInvocation : ast.not(newMethodInvocation), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)195 TextEditGroup (org.eclipse.text.edits.TextEditGroup)167 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)133 Expression (org.eclipse.jdt.core.dom.Expression)56 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 Statement (org.eclipse.jdt.core.dom.Statement)24 ArrayList (java.util.ArrayList)22 IfStatement (org.eclipse.jdt.core.dom.IfStatement)20 Block (org.eclipse.jdt.core.dom.Block)15 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 Modifier (org.eclipse.jdt.core.dom.Modifier)11 Type (org.eclipse.jdt.core.dom.Type)11 List (java.util.List)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7