Search in sources :

Example 1 with TypeLiteral

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;
}
Also used : TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral)

Example 2 with TypeLiteral

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;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 3 with TypeLiteral

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;
}
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 4 with TypeLiteral

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;
}
Also used : TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral)

Example 5 with 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;
}
Also used : TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral)

Aggregations

TypeLiteral (org.eclipse.jdt.core.dom.TypeLiteral)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)4 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)4 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)2 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)2 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)2 BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)2 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)2 Expression (org.eclipse.jdt.core.dom.Expression)2 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)2 Type (org.eclipse.jdt.core.dom.Type)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 EnumSet (java.util.EnumSet)1 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)1 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)1 Name (org.eclipse.jdt.core.dom.Name)1 TextEditGroup (org.eclipse.text.edits.TextEditGroup)1