Search in sources :

Example 1 with TypeParamDefBuilder

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

the class ClassToTypeDef method apply.

@Override
public TypeDef apply(Class item) {
    if (Object.class.equals(item)) {
        return TypeDef.OBJECT;
    }
    Kind kind = classToKind.apply(item);
    List<ClassRef> extendsList = new ArrayList<>();
    List<ClassRef> implementsList = new ArrayList<>();
    List<Property> properties = new ArrayList<>();
    List<Method> methods = new ArrayList<>();
    List<Method> constructors = new ArrayList<>();
    List<TypeParamDef> parameters = new ArrayList<>();
    if (item.getSuperclass() != null) {
        extendsList.add((ClassRef) typeToTypeRef.apply(item.getGenericSuperclass()));
        references.add(item.getSuperclass());
    }
    for (Class interfaceClass : item.getInterfaces()) {
        references.add(interfaceClass);
    }
    for (Type interfaceClass : item.getGenericInterfaces()) {
        TypeRef ref = typeToTypeRef.apply(interfaceClass);
        if (ref instanceof ClassRef) {
            implementsList.add((ClassRef) ref);
        }
    }
    constructors.addAll(getConstructors(item, references));
    methods.addAll(getMethods(item, references));
    properties.addAll(getProperties(item, references));
    for (TypeVariable typeVariable : item.getTypeParameters()) {
        List<ClassRef> bounds = new ArrayList<>();
        for (Type boundType : typeVariable.getBounds()) {
            TypeRef typeRef = typeToTypeRef.apply(boundType);
            if (typeRef instanceof ClassRef) {
                bounds.add((ClassRef) typeRef);
            }
        }
        parameters.add(new TypeParamDefBuilder().withName(typeVariable.getName()).withBounds(bounds).build());
    }
    String outerFQCN = item.getDeclaringClass() != null ? item.getDeclaringClass().getName() : null;
    TypeDef result = context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withOuterTypeName(outerFQCN).withName(item.getSimpleName()).withPackageName(item.getPackage() != null ? item.getPackage().getName() : null).withModifiers(item.getModifiers()).withParameters(parameters).withConstructors(constructors).withMethods(methods).withProperties(properties).withExtendsList(extendsList).withImplementsList(implementsList).build());
    Set<Class> copy = new HashSet<>(references);
    copy.stream().peek(c -> references.remove(c)).filter(c -> !c.equals(item)).filter(c -> !c.getName().startsWith("sun.") && !c.getName().toString().startsWith("com.sun.")).forEach(c -> {
        String referenceFQCN = c.getName().replaceAll(Pattern.quote("$"), ".");
        context.getDefinitionRepository().registerIfAbsent(referenceFQCN, () -> apply(c));
    });
    return result;
}
Also used : TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) AnnotationRefBuilder(io.sundr.model.AnnotationRefBuilder) HashMap(java.util.HashMap) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) AdapterContext(io.sundr.adapter.api.AdapterContext) Attributeable(io.sundr.model.Attributeable) ArrayList(java.util.ArrayList) AttributeKey(io.sundr.model.AttributeKey) ClassRef(io.sundr.model.ClassRef) HashSet(java.util.HashSet) Map(java.util.Map) TypeVariable(java.lang.reflect.TypeVariable) Set(java.util.Set) Method(io.sundr.model.Method) TypeRef(io.sundr.model.TypeRef) Field(java.lang.reflect.Field) AnnotationRef(io.sundr.model.AnnotationRef) Kind(io.sundr.model.Kind) Collectors(java.util.stream.Collectors) Property(io.sundr.model.Property) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Stream(java.util.stream.Stream) MethodBuilder(io.sundr.model.MethodBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) TypeDef(io.sundr.model.TypeDef) PropertyBuilder(io.sundr.model.PropertyBuilder) Pattern(java.util.regex.Pattern) TypeParamDef(io.sundr.model.TypeParamDef) AnnotatedElement(java.lang.reflect.AnnotatedElement) TypeParamDef(io.sundr.model.TypeParamDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeDef(io.sundr.model.TypeDef) TypeVariable(java.lang.reflect.TypeVariable) Kind(io.sundr.model.Kind) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) Property(io.sundr.model.Property) HashSet(java.util.HashSet)

Example 2 with TypeParamDefBuilder

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

the class Combine method extractParameters.

private static final Set<TypeParamDef> extractParameters(ClassRef classRef) {
    final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
    final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
    ClassRef ignored = new ClassRefBuilder(classRef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
    for (TypeParamRef typeParamRef : refs) {
        result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeParamRef(io.sundr.model.TypeParamRef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamRefColletor(io.sundr.dsl.internal.visitors.TypeParamRefColletor) ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) TypeParamDefColletor(io.sundr.dsl.internal.visitors.TypeParamDefColletor)

Example 3 with TypeParamDefBuilder

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

the class Combine method extractParameters.

private static final Set<TypeParamDef> extractParameters(TypeDef typeDef) {
    final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
    final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
    TypeDef ignored = new TypeDefBuilder(typeDef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
    for (TypeParamRef typeParamRef : refs) {
        result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeParamRef(io.sundr.model.TypeParamRef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamRefColletor(io.sundr.dsl.internal.visitors.TypeParamRefColletor) TypeDef(io.sundr.model.TypeDef) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) TypeParamDefColletor(io.sundr.dsl.internal.visitors.TypeParamDefColletor) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 4 with TypeParamDefBuilder

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

the class TypeToTypeParamDef method apply.

@Override
public TypeParamDef apply(Type item) {
    if (item instanceof TypeVariable) {
        TypeVariable typeVariable = (TypeVariable) item;
        String name = typeVariable.getName();
        List<ClassRef> bounds = new ArrayList<ClassRef>();
        for (Type b : typeVariable.getBounds()) {
            if (b instanceof Class) {
                Class c = (Class) b;
                bounds.add((ClassRef) typeToTypeRef.apply(c));
            }
        }
        return new TypeParamDefBuilder().withName(name).withBounds(bounds).build();
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) ClassRef(io.sundr.model.ClassRef) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder)

Aggregations

TypeParamDefBuilder (io.sundr.model.TypeParamDefBuilder)4 ClassRef (io.sundr.model.ClassRef)3 TypeParamDef (io.sundr.model.TypeParamDef)3 TypeParamDefColletor (io.sundr.dsl.internal.visitors.TypeParamDefColletor)2 TypeParamRefColletor (io.sundr.dsl.internal.visitors.TypeParamRefColletor)2 TypeDef (io.sundr.model.TypeDef)2 TypeDefBuilder (io.sundr.model.TypeDefBuilder)2 TypeParamRef (io.sundr.model.TypeParamRef)2 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 AdapterContext (io.sundr.adapter.api.AdapterContext)1 AnnotationRef (io.sundr.model.AnnotationRef)1 AnnotationRefBuilder (io.sundr.model.AnnotationRefBuilder)1 AttributeKey (io.sundr.model.AttributeKey)1 Attributeable (io.sundr.model.Attributeable)1 ClassRefBuilder (io.sundr.model.ClassRefBuilder)1 Kind (io.sundr.model.Kind)1 Method (io.sundr.model.Method)1