Search in sources :

Example 46 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.

the class Type method getConcretization.

public Type getConcretization(java.lang.reflect.Type type) {
    AST ast = t.getAST();
    if (type instanceof java.lang.reflect.ParameterizedType) {
        // substitute generic names with their types (recursively)
        java.lang.reflect.ParameterizedType pType = (java.lang.reflect.ParameterizedType) type;
        java.lang.reflect.Type rawType_ = pType.getRawType();
        org.eclipse.jdt.core.dom.Type rawType = ast.newSimpleType(ast.newName(((Class) rawType_).getCanonicalName()));
        ParameterizedType retType = ast.newParameterizedType(rawType);
        for (java.lang.reflect.Type arg : pType.getActualTypeArguments()) {
            try {
                org.eclipse.jdt.core.dom.Type argType = getConcretization(arg).T();
                retType.typeArguments().add(ASTNode.copySubtree(ast, argType));
            } catch (SynthesisException e) {
                return new Type(rawType, (Class) rawType_);
            }
        }
        return new Type(retType, (Class) rawType_);
    } else if (type instanceof TypeVariable) {
        // return the type the generic name was concretized to
        String name = ((TypeVariable) type).getName();
        // FIXME: Add support for wildcard types and concretizing without a base parameterized type (e.g., Collections)
        if (concretization == null)
            throw new SynthesisException(SynthesisException.InvalidKindOfType, C().getName());
        if (!concretization.containsKey(name))
            throw new SynthesisException(SynthesisException.GenericTypeVariableMismatch, C().getName());
        return concretization.get(name);
    } else if (type instanceof Class) {
        Class cls = (Class) type;
        if (cls.isArray()) {
            if (// no support for multidim arrays
            cls.getComponentType().isArray())
                throw new SynthesisException(SynthesisException.InvalidKindOfType, C().getName());
            Type componentType = getConcretization(cls.getComponentType());
            return new Type(ast.newArrayType(componentType.T(), 1), cls);
        } else if (cls.isPrimitive()) {
            return new Type(ast.newPrimitiveType(PrimitiveType.toCode(cls.getSimpleName())), cls);
        } else {
            // no generics, just return a simple type with the class
            org.eclipse.jdt.core.dom.Type retType = ast.newSimpleType(ast.newName(cls.getCanonicalName()));
            return new Type(retType, cls);
        }
    } else
        throw new SynthesisException(SynthesisException.InvalidKindOfType, C().getName());
}
Also used : org.eclipse.jdt.core.dom(org.eclipse.jdt.core.dom) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) java.lang.reflect(java.lang.reflect) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType)

Example 47 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.

the class Type method simpleT.

/**
 * Returns a simple representation of the current type (i.e., without fully qualified names)
 *
 * @param ast the AST node that should own the simple type
 * @param env if provided (not null), then add imports of classes in generic type
 * @return a DOM type representing the simple type
 */
public org.eclipse.jdt.core.dom.Type simpleT(AST ast, Environment env) {
    if (t.isPrimitiveType())
        return ast.newPrimitiveType(((PrimitiveType) t).getPrimitiveTypeCode());
    if (t.isSimpleType() || t.isQualifiedType()) {
        Name name = t.isSimpleType() ? ((SimpleType) t).getName() : ((QualifiedType) t).getName();
        SimpleName simple;
        if (name.isSimpleName())
            simple = ast.newSimpleName(((SimpleName) name).getIdentifier());
        else
            simple = ast.newSimpleName(((QualifiedName) name).getName().getIdentifier());
        return ast.newSimpleType(simple);
    }
    if (t.isParameterizedType()) {
        org.eclipse.jdt.core.dom.Type baseType = ((ParameterizedType) t).getType();
        Name name = baseType.isSimpleType() ? ((SimpleType) baseType).getName() : ((QualifiedType) baseType).getName();
        SimpleName simple;
        if (name.isSimpleName())
            simple = ast.newSimpleName(((SimpleName) name).getIdentifier());
        else
            simple = ast.newSimpleName(((QualifiedName) name).getName().getIdentifier());
        ParameterizedType pType = ast.newParameterizedType(ast.newSimpleType(simple));
        for (Object o : ((ParameterizedType) t).typeArguments()) {
            Type p = new Type((org.eclipse.jdt.core.dom.Type) o);
            if (env != null)
                env.addImport(p.C());
            pType.typeArguments().add(p.simpleT(ast, env));
        }
        return pType;
    }
    if (t.isArrayType()) {
        org.eclipse.jdt.core.dom.Type elementType = ((ArrayType) t).getElementType();
        org.eclipse.jdt.core.dom.Type simpleElementType = new Type(elementType).simpleT(ast, env);
        return ast.newArrayType((org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(ast, simpleElementType), ((ArrayType) t).getDimensions());
    }
    throw new SynthesisException(SynthesisException.InvalidKindOfType, C().getName());
}
Also used : org.eclipse.jdt.core.dom(org.eclipse.jdt.core.dom) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType)

Example 48 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method toType.

/**
 * Converts a type binding into a type.
 *
 * @param typeBinding     the type binding to convert
 * @param typeNameDecider decides on how the type should be referenced (simple
 *                        name or qualified name)
 * @return a new type
 */
public Type toType(final ITypeBinding typeBinding, final TypeNameDecider typeNameDecider) {
    if (typeBinding == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException(null, "typeBinding cannot be null");
    }
    if (typeBinding.isParameterizedType()) {
        ParameterizedType type = ast.newParameterizedType(toType(typeBinding.getErasure(), typeNameDecider));
        List<Type> typeArgs = type.typeArguments();
        for (ITypeBinding typeArg : typeBinding.getTypeArguments()) {
            typeArgs.add(toType(typeArg, typeNameDecider));
        }
        return type;
    }
    if (typeBinding.isPrimitive()) {
        return type(typeBinding.getName());
    }
    if (typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isEnum() || typeBinding.isAnnotation() || typeBinding.isNullType() || typeBinding.isRawType()) {
        return type(typeNameDecider.useSimplestPossibleName(typeBinding));
    }
    if (typeBinding.isArray()) {
        return ast.newArrayType(toType(typeBinding.getElementType(), typeNameDecider), typeBinding.getDimensions());
    }
    if (typeBinding.isWildcardType()) {
        WildcardType type = ast.newWildcardType();
        if (typeBinding.getBound() != null) {
            type.setBound(toType(typeBinding.getBound(), typeNameDecider), typeBinding.isUpperbound());
        }
        return type;
    }
    if (typeBinding.isTypeVariable()) {
        return type(typeBinding.getName());
    }
    if (typeBinding.isCapture()) {
        if (typeBinding.getTypeBounds().length > 1) {
            throw new NotImplementedException(null, // $NON-NLS-1$
            "because it violates the javadoc of `ITypeBinding.getTypeBounds()`: " + // $NON-NLS-1$
            "\"Note that per construction, it can only contain one class or array type, " + // $NON-NLS-1$
            "at most, and then it is located in first position.\"");
        }
        return toType(typeBinding.getWildcard(), typeNameDecider);
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    throw new NotImplementedException(null, " for the type binding '" + typeBinding + "'");
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) IllegalArgumentException(org.autorefactor.util.IllegalArgumentException)

Example 49 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method copyType.

private Type copyType(final Type type) {
    switch(type.getNodeType()) {
        case ASTNode.ARRAY_TYPE:
            ArrayType arrayType = (ArrayType) type;
            return ast.newArrayType(copyType(arrayType.getElementType()), arrayType.getDimensions());
        case ASTNode.PRIMITIVE_TYPE:
            Code code = ((PrimitiveType) type).getPrimitiveTypeCode();
            return ast.newPrimitiveType(code);
        case ASTNode.QUALIFIED_TYPE:
            ITypeBinding typeBinding = type.resolveBinding();
            if (typeBinding == null) {
                return null;
            }
            return type(typeBinding.getQualifiedName());
        case ASTNode.SIMPLE_TYPE:
            SimpleType sType = (SimpleType) type;
            return ast.newSimpleType(createCopyTarget(sType.getName()));
        case ASTNode.PARAMETERIZED_TYPE:
            ParameterizedType pType = (ParameterizedType) type;
            ParameterizedType copyOfType = ast.newParameterizedType(createCopyTarget(pType.getType()));
            List<Type> newTypeArgs = copyOfType.typeArguments();
            for (Object typeArg : pType.typeArguments()) {
                if (((Type) typeArg).isWildcardType()) {
                    newTypeArgs.add(ast.newWildcardType());
                } else {
                    newTypeArgs.add(createCopyTarget((Type) typeArg));
                }
            }
            return copyOfType;
    }
    // $NON-NLS-1$
    throw new NotImplementedException(null, "Unknown type for type " + type);
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Code(org.eclipse.jdt.core.dom.PrimitiveType.Code)

Example 50 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method newParameterizedType.

/**
 * Returns a parameterized type with the provided type name and type arguments.
 *
 * @param typeName      the type name (simple or qualified name)
 * @param typeArguments the type arguments
 * @return a new parameterized type
 */
public Type newParameterizedType(final String typeName, final Type... typeArguments) {
    Type type = type(typeName);
    ParameterizedType parameterizedType = ast.newParameterizedType(type);
    Collections.addAll(parameterizedType.typeArguments(), typeArguments);
    return parameterizedType;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)57 Type (org.eclipse.jdt.core.dom.Type)38 SimpleType (org.eclipse.jdt.core.dom.SimpleType)23 ArrayType (org.eclipse.jdt.core.dom.ArrayType)22 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 IType (org.eclipse.jdt.core.IType)12 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)10 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)10 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 WildcardType (java.lang.reflect.WildcardType)8 org.eclipse.jdt.core.dom (org.eclipse.jdt.core.dom)8 WildcardType (org.eclipse.jdt.core.dom.WildcardType)8 Name (org.eclipse.jdt.core.dom.Name)7 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)7 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)6 UnionType (org.eclipse.jdt.core.dom.UnionType)6 AST (org.eclipse.jdt.core.dom.AST)5