Search in sources :

Example 1 with TypeParamRefBuilder

use of io.sundr.model.TypeParamRefBuilder in project sundrio by sundrio.

the class TypeToTypeRef method apply.

@Override
public TypeRef apply(Type item) {
    if (item == null) {
        return new VoidRefBuilder().build();
    } else if (item instanceof WildcardType) {
        return new WildcardRefBuilder().withBounds(Arrays.asList(((WildcardType) item).getLowerBounds()).stream().map(t -> apply(t)).collect(Collectors.toList())).build();
    } else if (item instanceof TypeVariable) {
        return new TypeParamRefBuilder().withName(((TypeVariable) item).getName()).build();
    } else if (item instanceof GenericArrayType) {
        Type target = item;
        int dimensions = 0;
        while (target instanceof GenericArrayType) {
            target = ((GenericArrayType) target).getGenericComponentType();
            dimensions++;
        }
        if (target instanceof Class) {
            references.add((Class) target);
        }
        TypeRef targetRef = apply(target);
        return targetRef.withDimensions(dimensions + targetRef.getDimensions());
    } else if (item instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) item;
        Type rawType = parameterizedType.getRawType();
        List<TypeRef> arguments = new ArrayList<TypeRef>();
        for (Type arg : parameterizedType.getActualTypeArguments()) {
            arguments.add(apply(arg));
            if (arg instanceof Class) {
                references.add((Class) arg);
            }
        }
        if (rawType instanceof Class) {
            references.add((Class) rawType);
        }
        return new ClassRefBuilder((ClassRef) apply(rawType)).withArguments(arguments).build();
    } else if (Object.class.equals(item)) {
        return ClassRef.OBJECT;
    } else if (item instanceof Class) {
        Class c = (Class) item;
        if (c.isArray()) {
            Class target = c;
            int dimensions = 0;
            while (target.isArray()) {
                target = ((Class) target).getComponentType();
                dimensions++;
            }
            TypeRef targetRef = apply(target);
            references.add(target);
            return targetRef.withDimensions(dimensions + targetRef.getDimensions());
        }
        if (c.isPrimitive()) {
            return new PrimitiveRefBuilder().withName(c.getName()).withDimensions(0).build();
        } else {
            List<TypeRef> arguments = new ArrayList<TypeRef>();
            for (TypeVariable v : c.getTypeParameters()) {
                arguments.add(apply(v));
            }
            references.add((Class) item);
            String fqcn = c.getName().replaceAll(Pattern.quote("$"), ".");
            return new ClassRefBuilder().withFullyQualifiedName(fqcn).withArguments(arguments).build();
        }
    }
    throw new IllegalArgumentException("Can't convert type:" + item + " to a TypeRef");
}
Also used : PrimitiveRefBuilder(io.sundr.model.PrimitiveRefBuilder) VoidRefBuilder(io.sundr.model.VoidRefBuilder) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) WildcardRefBuilder(io.sundr.model.WildcardRefBuilder)

Example 2 with TypeParamRefBuilder

use of io.sundr.model.TypeParamRefBuilder in project sundrio by sundrio.

the class TypeToTypeRef method apply.

@Override
public TypeRef apply(Type type) {
    if (type instanceof VoidType) {
        return new VoidRef();
    } else if (type instanceof WildcardType) {
        return new WildcardRef();
    } else if (type instanceof ReferenceType) {
        ReferenceType referenceType = (ReferenceType) type;
        int dimensions = referenceType.getArrayCount();
        TypeRef typeRef = apply(referenceType.getType());
        if (dimensions == 0) {
            return typeRef;
        } else if (typeRef instanceof ClassRef) {
            return new ClassRefBuilder((ClassRef) typeRef).withDimensions(dimensions).build();
        } else if (typeRef instanceof PrimitiveRef) {
            return new PrimitiveRefBuilder((PrimitiveRef) typeRef).withDimensions(dimensions).build();
        } else if (typeRef instanceof TypeParamRef) {
            return new TypeParamRefBuilder((TypeParamRef) typeRef).withDimensions(dimensions).build();
        }
    } else if (type instanceof PrimitiveType) {
        PrimitiveType primitiveType = (PrimitiveType) type;
        return new PrimitiveRefBuilder().withName(primitiveType.getType().name()).build();
    } else if (type instanceof ClassOrInterfaceType) {
        return classOrInterfaceToTypeRef.apply((ClassOrInterfaceType) type);
    }
    throw new IllegalArgumentException("Can't handle type:[" + type + "].");
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) PrimitiveRefBuilder(io.sundr.model.PrimitiveRefBuilder) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) VoidRef(io.sundr.model.VoidRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) WildcardRef(io.sundr.model.WildcardRef) PrimitiveRef(io.sundr.model.PrimitiveRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ReferenceType(com.github.javaparser.ast.type.ReferenceType) TypeParamRef(io.sundr.model.TypeParamRef) WildcardType(com.github.javaparser.ast.type.WildcardType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType)

Example 3 with TypeParamRefBuilder

use of io.sundr.model.TypeParamRefBuilder in project sundrio by sundrio.

the class ClassOrInterfaceToTypeRef method apply.

@Override
public TypeRef apply(ClassOrInterfaceType classOrInterfaceType) {
    String boundPackage = PACKAGENAME.apply(classOrInterfaceType);
    String boundName = classOrInterfaceType.getName();
    List<TypeRef> arguments = new ArrayList<TypeRef>();
    for (Type arg : classOrInterfaceType.getTypeArgs()) {
        if (arg instanceof ReferenceType) {
            // TODO: Need to check if this is valid for all cases...
            ReferenceType referenceType = (ReferenceType) arg;
            Type type = referenceType.getType();
            int dimensions = referenceType.getArrayCount();
            if (type instanceof ClassOrInterfaceType) {
                TypeRef intermediateRef = apply((ClassOrInterfaceType) type);
                if (intermediateRef instanceof ClassRef) {
                    arguments.add(new ClassRefBuilder((ClassRef) intermediateRef).withDimensions(dimensions).build());
                } else if (intermediateRef instanceof TypeParamRef) {
                    arguments.add(new TypeParamRefBuilder((TypeParamRef) intermediateRef).withDimensions(dimensions).build());
                } else {
                    throw new IllegalStateException("Expected class or type param reference");
                }
            } else {
                String name = referenceType.toString();
                arguments.add(new TypeParamRefBuilder().withName(name).withDimensions(dimensions).build());
            }
        } else if (arg instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType) arg;
            if (wildcardType.getExtends() != null) {
                TypeRef bound = typeToTypeRef.apply(wildcardType.getExtends());
                arguments.add(new WildcardRefBuilder().addToBounds(bound).build());
            } else if (wildcardType.getSuper() != null) {
                TypeRef bound = typeToTypeRef.apply(wildcardType.getSuper());
                arguments.add(new WildcardRefBuilder().addToBounds(bound).withBoundKind(WildcardRef.BoundKind.SUPER).build());
            } else {
                arguments.add(new WildcardRef());
            }
        }
    }
    if (classOrInterfaceType.getParentNode() == classOrInterfaceType) {
        return new TypeParamRefBuilder().withName(boundName).build();
    }
    String fqcn = boundPackage + "." + boundName;
    // : knownDefinition.toReference(arguments);
    if (classOrInterfaceType.getTypeArgs().isEmpty() && boundName.length() == 1) {
        // parameter refs.
        return new TypeParamRefBuilder().withName(boundName).build();
    } else {
        return new ClassRefBuilder().withFullyQualifiedName(fqcn).withArguments(arguments).build();
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) WildcardRef(io.sundr.model.WildcardRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ReferenceType(com.github.javaparser.ast.type.ReferenceType) TypeParamRef(io.sundr.model.TypeParamRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReferenceType(com.github.javaparser.ast.type.ReferenceType) Type(com.github.javaparser.ast.type.Type) WildcardType(com.github.javaparser.ast.type.WildcardType) WildcardType(com.github.javaparser.ast.type.WildcardType) WildcardRefBuilder(io.sundr.model.WildcardRefBuilder)

Aggregations

ClassRefBuilder (io.sundr.model.ClassRefBuilder)3 TypeParamRefBuilder (io.sundr.model.TypeParamRefBuilder)3 TypeRef (io.sundr.model.TypeRef)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 ClassRef (io.sundr.model.ClassRef)2 PrimitiveRefBuilder (io.sundr.model.PrimitiveRefBuilder)2 TypeParamRef (io.sundr.model.TypeParamRef)2 WildcardRef (io.sundr.model.WildcardRef)2 WildcardRefBuilder (io.sundr.model.WildcardRefBuilder)2 ArrayList (java.util.ArrayList)2 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 Type (com.github.javaparser.ast.type.Type)1 VoidType (com.github.javaparser.ast.type.VoidType)1 PrimitiveRef (io.sundr.model.PrimitiveRef)1 VoidRef (io.sundr.model.VoidRef)1 VoidRefBuilder (io.sundr.model.VoidRefBuilder)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1