use of org.eclipse.jdt.core.dom.TypeLiteral in project AutoRefactor by JnRouvignac.
the class EnumMapRatherThanHashMapRefactoring method resolveParameter.
/**
* Map parameter for HashMap constructor to EnumMap constructor. HashMap(Map) ->
* EnumMap(EnumMap) <br/>
* other HashMap constructors -> EnumMap(Class) <br>
*
* @return correct parameter for EnumMap constructor
*/
private Expression resolveParameter(Type keyType, List<Expression> originalArgs) {
if (!originalArgs.isEmpty() && instanceOf(originalArgs.get(0), "java.util.EnumMap")) {
return ctx.getASTBuilder().copy(originalArgs.get(0));
}
TypeLiteral keyTypeLiteral = keyType.getAST().newTypeLiteral();
keyTypeLiteral.setType(ctx.getASTBuilder().copy(keyType));
return keyTypeLiteral;
}
use of org.eclipse.jdt.core.dom.TypeLiteral in project AutoRefactor by JnRouvignac.
the class EnumSetRatherThanHashSetRefactoring method replace.
/**
* Refactoring 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>
*
* @see {@link java.util.EnumSet#copyOf(Collection)}
* @see {@link java.util.EnumSet#copyOf(EnumSet)}
* @see {@link java.util.EnumSet#noneOf(Class)} <br>
* @param cic
* - class instance creation node to be replaced
* @param type
* - type argument of the declaration
*/
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
if (types == null || types.length < 1) {
return VISIT_SUBTREE;
}
Type type = types[0];
ASTBuilder b = ctx.getASTBuilder();
List<Expression> arguments = arguments(cic);
final MethodInvocation invocation;
if (!arguments.isEmpty() && instanceOf(arguments.get(0), "java.util.Collection")) {
Expression typeArg = arguments.get(0);
if (!instanceOf(typeArg, "java.util.EnumSet")) {
return VISIT_SUBTREE;
}
invocation = b.invoke(b.name("java", "util", "EnumSet"), "copyOf", b.copy(typeArg));
} else {
TypeLiteral newTypeLiteral = ctx.getAST().newTypeLiteral();
newTypeLiteral.setType(b.copy(type));
invocation = b.invoke(b.name("java", "util", "EnumSet"), "noneOf", newTypeLiteral);
}
ctx.getRefactorings().replace(cic, invocation);
return DO_NOT_VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.TypeLiteral 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.eclipse.jdt.core.dom.TypeLiteral in project whole by wholeplatform.
the class CompilationUnitBuilder method newTypeLiteral.
public TypeLiteral newTypeLiteral(String type) {
TypeLiteral typeLiteral = ast.newTypeLiteral();
typeLiteral.setType(newType(type));
return typeLiteral;
}
use of org.eclipse.jdt.core.dom.TypeLiteral in project whole by wholeplatform.
the class CompilationUnitBuilder method newQualifiedTypeLiteral.
public TypeLiteral newQualifiedTypeLiteral(String type) {
TypeLiteral typeLiteral = ast.newTypeLiteral();
typeLiteral.setType(newQualifiedType(type));
return typeLiteral;
}
Aggregations