Search in sources :

Example 1 with TypeRef

use of io.sundr.model.TypeRef in project kubernetes-client by fabric8io.

the class TypesTest method projectSuperClassesShouldProduceProperlyTypedClasses.

@Test
void projectSuperClassesShouldProduceProperlyTypedClasses() {
    List<ClassRef> superClasses = Types.typeDefFrom(Basic.class).getExtendsList();
    assertEquals(2, superClasses.size());
    Optional<ClassRef> crOpt = superClasses.stream().filter(c -> c.getName().contains("CustomResource")).findFirst();
    assertTrue(crOpt.isPresent());
    ClassRef crDef = crOpt.get();
    List<TypeRef> arguments = crDef.getArguments();
    assertEquals(2, arguments.size());
    assertTrue(arguments.get(0).toString().contains(BasicSpec.class.getSimpleName()));
    assertTrue(arguments.get(1).toString().contains(BasicStatus.class.getSimpleName()));
}
Also used : BasicSpec(io.fabric8.crd.example.basic.BasicSpec) SpecAndStatus(io.fabric8.crd.generator.utils.Types.SpecAndStatus) TypeRef(io.sundr.model.TypeRef) Property(io.sundr.model.Property) Joke(io.fabric8.crd.example.joke.Joke) Person(io.fabric8.crd.example.person.Person) ClassRef(io.sundr.model.ClassRef) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BasicStatus(io.fabric8.crd.example.basic.BasicStatus) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Basic(io.fabric8.crd.example.basic.Basic) TypeDef(io.sundr.model.TypeDef) Optional(java.util.Optional) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) WebServerWithStatusProperty(io.fabric8.crd.example.webserver.WebServerWithStatusProperty) Child(io.fabric8.crd.example.inherited.Child) Basic(io.fabric8.crd.example.basic.Basic) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Test(org.junit.jupiter.api.Test)

Example 2 with TypeRef

use of io.sundr.model.TypeRef in project kubernetes-client by fabric8io.

the class TypesTest method shouldFindInheritedStatusProperty.

@Test
void shouldFindInheritedStatusProperty() {
    final TypeDef def = Types.typeDefFrom(Child.class);
    final Optional<Property> p = Types.findStatusProperty(def);
    assertTrue(p.isPresent());
    final Property property = p.get();
    final TypeRef typeRef = property.getTypeRef();
    assertTrue(typeRef instanceof ClassRef);
    final ClassRef classRef = (ClassRef) typeRef;
    final SpecAndStatus specAndStatus = Types.resolveSpecAndStatusTypes(def);
    assertEquals(specAndStatus.getStatusClassName(), classRef.getFullyQualifiedName());
}
Also used : TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) SpecAndStatus(io.fabric8.crd.generator.utils.Types.SpecAndStatus) Property(io.sundr.model.Property) WebServerWithStatusProperty(io.fabric8.crd.example.webserver.WebServerWithStatusProperty) Test(org.junit.jupiter.api.Test)

Example 3 with TypeRef

use of io.sundr.model.TypeRef 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 4 with TypeRef

use of io.sundr.model.TypeRef 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 5 with TypeRef

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

the class VariableElementToProperty method apply.

public Property apply(final VariableElement variableElement) {
    String name = variableElement.getSimpleName().toString();
    TypeRef type = referenceAdapterFunction.apply(variableElement.asType());
    List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
    for (AnnotationMirror annotationMirror : variableElement.getAnnotationMirrors()) {
        annotations.add(annotationAdapterFunction.apply(annotationMirror));
    }
    String comments = context.getElements().getDocComment(variableElement);
    List<String> commentList = Strings.isNullOrEmpty(comments) ? new ArrayList<>() : Arrays.stream(comments.split(NEWLINE_PATTERN)).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    return new PropertyBuilder().withComments(commentList).withName(name).withTypeRef(type).withAnnotations(annotations).withModifiers(Types.modifiersToInt(variableElement.getModifiers())).build();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) AnnotationRef(io.sundr.model.AnnotationRef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Aggregations

TypeRef (io.sundr.model.TypeRef)45 ClassRef (io.sundr.model.ClassRef)33 TypeDef (io.sundr.model.TypeDef)23 ArrayList (java.util.ArrayList)16 ClassRefBuilder (io.sundr.model.ClassRefBuilder)14 Method (io.sundr.model.Method)13 RichTypeDef (io.sundr.model.RichTypeDef)13 TypeDefBuilder (io.sundr.model.TypeDefBuilder)12 Property (io.sundr.model.Property)10 MethodBuilder (io.sundr.model.MethodBuilder)9 PropertyBuilder (io.sundr.model.PropertyBuilder)9 TypeParamRef (io.sundr.model.TypeParamRef)9 TypedVisitor (io.sundr.builder.TypedVisitor)8 AnnotationRef (io.sundr.model.AnnotationRef)8 List (java.util.List)8 TypeParamDef (io.sundr.model.TypeParamDef)7 HashMap (java.util.HashMap)7 AttributeKey (io.sundr.model.AttributeKey)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6