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);
}
}
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);
}
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;
}
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);
}
}
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);
}
Aggregations