Search in sources :

Example 11 with TypeDef

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

the class GetDefinition method apply.

@Override
public TypeDef apply(ClassRef t) {
    String fullyQualifiedName = t.getFullyQualifiedName();
    TypeDef def = DefinitionRepository.getRepository().getDefinition(fullyQualifiedName);
    if (def != null) {
        return def;
    }
    Predicate<String> isLowerCase = w -> Character.isUpperCase(w.charAt(0));
    Predicate<String> inPackage = until(isLowerCase);
    Predicate<String> outOfPackage = after(isLowerCase);
    String packageName = Arrays.stream(fullyQualifiedName.split("\\.")).filter(inPackage).collect(Collectors.joining("."));
    String className = Arrays.stream(fullyQualifiedName.split("\\.")).filter(outOfPackage).collect(Collectors.joining("."));
    String ownerClassName = className.contains(".") ? className.substring(0, className.indexOf(".")) : null;
    if (ownerClassName != null) {
        className = className.substring(ownerClassName.length() + 1);
        return new TypeDefBuilder().withName(className).withPackageName(packageName).withOuterTypeName(packageName + "." + ownerClassName).build();
    }
    return new TypeDefBuilder().withName(className).withPackageName(packageName).build();
}
Also used : Predicates.until(io.sundr.utils.Predicates.until) Arrays(java.util.Arrays) Predicates.after(io.sundr.utils.Predicates.after) Predicate(java.util.function.Predicate) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) TypeDef(io.sundr.model.TypeDef) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 12 with TypeDef

use of io.sundr.model.TypeDef 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 13 with TypeDef

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

the class AbstractAdapterTest method testWithSelfRefParam.

@Test
public void testWithSelfRefParam() {
    T input = getInput(ClassWithSelfRefParam.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    assertNotNull(typeDef);
    assertEquals("ClassWithSelfRefParam", typeDef.getName());
    assertEquals(1, typeDef.getProperties().size());
    assertEquals(1, typeDef.getParameters().size());
    assertEquals("T", typeDef.getParameters().get(0).getName());
    assertEquals(1, typeDef.getParameters().get(0).getBounds().size());
    assertEquals(1, typeDef.getParameters().get(0).getBounds().get(0).getArguments().size());
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Test(org.junit.Test)

Example 14 with TypeDef

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

the class AbstractAdapterTest method testWithParam.

@Test
public void testWithParam() {
    T input = getInput(ClassWithParam.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    assertNotNull(typeDef);
    assertEquals("ClassWithParam", typeDef.getName());
    assertEquals(1, typeDef.getProperties().size());
    assertEquals(1, typeDef.getParameters().size());
    assertEquals("T", typeDef.getParameters().get(0).getName());
    assertEquals(0, typeDef.getParameters().get(0).getBounds().size());
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Test(org.junit.Test)

Example 15 with TypeDef

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

the class AbstractAdapterTest method testWithPrimitiveArray.

@Test
public void testWithPrimitiveArray() {
    T input = getInput(ClassWithPrimitiveArray.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    assertNotNull(typeDef);
    assertEquals("ClassWithPrimitiveArray", typeDef.getName());
    assertEquals(1, typeDef.getProperties().size());
    TypeRef typeRef = typeDef.getProperties().iterator().next().getTypeRef();
    assertTrue(typeRef instanceof PrimitiveRef);
    assertEquals(1, ((PrimitiveRef) typeRef).getDimensions());
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) PrimitiveRef(io.sundr.model.PrimitiveRef) Test(org.junit.Test)

Aggregations

TypeDef (io.sundr.model.TypeDef)99 ClassRef (io.sundr.model.ClassRef)43 Test (org.junit.Test)40 RichTypeDef (io.sundr.model.RichTypeDef)38 TypeDefBuilder (io.sundr.model.TypeDefBuilder)35 TypeRef (io.sundr.model.TypeRef)26 Method (io.sundr.model.Method)22 Property (io.sundr.model.Property)22 ArrayList (java.util.ArrayList)20 List (java.util.List)15 TypeElement (javax.lang.model.element.TypeElement)15 Collectors (java.util.stream.Collectors)14 Element (javax.lang.model.element.Element)14 Set (java.util.Set)12 Test (org.junit.jupiter.api.Test)12 AnnotationRef (io.sundr.model.AnnotationRef)11 DefinitionRepository (io.sundr.model.repo.DefinitionRepository)11 HashMap (java.util.HashMap)11 AptContext (io.sundr.adapter.apt.AptContext)10 ClassRefBuilder (io.sundr.model.ClassRefBuilder)10