Search in sources :

Example 36 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);
        if (!concretization.containsKey(name))
            throw new SynthesisException(SynthesisException.GenericTypeVariableMismatch);
        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);
            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);
}
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 37 with ParameterizedType

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

the class Type method releaseBinding.

// make a DOM type independent of its bindings, because when copying subtrees bindings don't copy over
org.eclipse.jdt.core.dom.Type releaseBinding(org.eclipse.jdt.core.dom.Type type, AST ast) {
    ITypeBinding binding = type.resolveBinding();
    if (type.isPrimitiveType())
        return ast.newPrimitiveType(((PrimitiveType) type).getPrimitiveTypeCode());
    else if (type.isSimpleType())
        return ast.newSimpleType(ast.newName(binding.getQualifiedName()));
    else if (binding.isParameterizedType()) {
        ITypeBinding erasure = binding.getErasure();
        SimpleType baseType = ast.newSimpleType(ast.newName(erasure.getQualifiedName()));
        ParameterizedType retType = ast.newParameterizedType(baseType);
        for (Object o : ((ParameterizedType) type).typeArguments()) {
            org.eclipse.jdt.core.dom.Type arg = (org.eclipse.jdt.core.dom.Type) o;
            org.eclipse.jdt.core.dom.Type argType = releaseBinding(arg, ast);
            retType.typeArguments().add(argType);
        }
        return retType;
    } else if (type.isArrayType()) {
        ArrayType arrayType = (ArrayType) type;
        org.eclipse.jdt.core.dom.Type elementType = releaseBinding(arrayType.getElementType(), ast);
        int dimensions = arrayType.getDimensions();
        return ast.newArrayType(elementType, dimensions);
    } else {
        throw new SynthesisException(SynthesisException.InvalidKindOfType);
    }
}
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)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)37 Type (org.eclipse.jdt.core.dom.Type)23 SimpleType (org.eclipse.jdt.core.dom.SimpleType)15 ArrayType (org.eclipse.jdt.core.dom.ArrayType)14 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)12 IType (org.eclipse.jdt.core.IType)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 WildcardType (java.lang.reflect.WildcardType)8 org.eclipse.jdt.core.dom (org.eclipse.jdt.core.dom)8 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 Name (org.eclipse.jdt.core.dom.Name)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4 UnionType (org.eclipse.jdt.core.dom.UnionType)4 WildcardType (org.eclipse.jdt.core.dom.WildcardType)4