Search in sources :

Example 91 with TypeDef

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

the class ClazzAsTest method testToFluent.

@Test
public void testToFluent() {
    TypeDef type = new TypeDefBuilder().withName("MyClass").withPackageName(getClass().getPackage().getName()).withParameters().build();
    Method constructor = new MethodBuilder().withReturnType(type.toReference()).withAnnotations(Constants.BUILDABLE_ANNOTATION).build();
    type = new TypeDefBuilder(type).withConstructors(constructor).build();
    TypeDef result = ClazzAs.FLUENT_IMPL.apply(TypeArguments.apply(type));
    System.out.println(result);
}
Also used : TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) MethodBuilder(io.sundr.model.MethodBuilder) AbstractProcessorTest(io.sundr.builder.internal.processor.AbstractProcessorTest) Test(org.junit.Test)

Example 92 with TypeDef

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

the class TypeElementToTypeDef method apply.

@Override
public TypeDef apply(TypeElement classElement) {
    // Check SuperClass
    Kind kind = Kind.CLASS;
    Element enclosing = classElement.getEnclosingElement();
    if (enclosing != null && ANY.equals(enclosing.getSimpleName().toString())) {
        throw new SundrException("Failed to read class element:" + classElement.getQualifiedName().toString() + ". " + Messages.POTENTIAL_UNRESOLVED_SYMBOL);
    }
    TypeMirror superClass = classElement.getSuperclass();
    TypeRef superClassType = TypeDef.OBJECT_REF;
    if (superClass == null) {
    // ignore
    } else if (superClass instanceof NoType) {
    // ignore
    } else if (superClass.toString().equals(TypeDef.OBJECT.getFullyQualifiedName())) {
    // ignore
    } else {
        superClassType = referenceAdapterFunction.apply(superClass);
    }
    List<TypeParamDef> genericTypes = new ArrayList<TypeParamDef>();
    List<ClassRef> interfaces = new ArrayList<ClassRef>();
    if (classElement.getKind() == ElementKind.INTERFACE) {
        kind = Kind.INTERFACE;
    } else if (classElement.getKind() == ElementKind.CLASS) {
        kind = Kind.CLASS;
    } else if (classElement.getKind() == ElementKind.ANNOTATION_TYPE) {
        kind = Kind.ANNOTATION;
    } else if (classElement.getKind() == ElementKind.ENUM) {
        kind = Kind.ENUM;
    }
    String comments = AptContext.getContext().getElements().getDocComment(classElement);
    List<String> commentList = Strings.isNullOrEmpty(comments) ? new ArrayList<>() : Arrays.stream(comments.split(NEWLINE_PATTERN)).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    for (TypeMirror interfaceTypeMirrror : classElement.getInterfaces()) {
        TypeRef interfaceType = referenceAdapterFunction.apply(interfaceTypeMirrror);
        if (interfaceType instanceof ClassRef) {
            interfaces.add((ClassRef) interfaceType);
        } else {
            throw new IllegalStateException("Interface: [" + interfaceType + "] not mapped to a class ref.");
        }
    }
    for (TypeParameterElement typeParameter : classElement.getTypeParameters()) {
        TypeParamDef genericType = typeParamAdapterFunction.apply(typeParameter);
        genericTypes.add(genericType);
    }
    TypeDef baseType = new TypeDefBuilder().withComments(commentList).withKind(kind).withModifiers(Types.modifiersToInt(classElement.getModifiers())).withPackageName(getPackageName(classElement)).withName(getClassName(classElement)).withParameters(genericTypes).withExtendsList(superClassType instanceof ClassRef ? (ClassRef) superClassType : null).withImplementsList(interfaces).withOuterTypeName(classElement.getEnclosingElement() instanceof TypeElement ? classElement.getEnclosingElement().toString() : null).build();
    // We will register the base type first and will replace it with the full blown version later.
    context.getDefinitionRepository().registerIfAbsent(baseType);
    List<TypeDef> innerTypes = new ArrayList<TypeDef>();
    for (TypeElement innerElement : ElementFilter.typesIn(classElement.getEnclosedElements())) {
        TypeDef innerType = context.getDefinitionRepository().register(apply(innerElement));
        if (innerType == null) {
            throw new IllegalStateException("Inner type for:" + innerElement + " is null");
        }
        innerType = new TypeDefBuilder(innerType).withOuterTypeName(baseType.getFullyQualifiedName()).build();
        context.getDefinitionRepository().register(innerType);
        innerTypes.add(innerType);
    }
    TypeDefBuilder builder = new TypeDefBuilder(baseType).withInnerTypes(innerTypes);
    for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
        builder.addToConstructors(methodAdapterFunction.apply(constructor));
    }
    // Populate Fields
    for (VariableElement variableElement : ElementFilter.fieldsIn(classElement.getEnclosedElements())) {
        builder.addToProperties(propertyAdapterFunction.apply(variableElement));
    }
    Set<ExecutableElement> allMethods = new LinkedHashSet<ExecutableElement>();
    allMethods.addAll(ElementFilter.methodsIn(classElement.getEnclosedElements()));
    allMethods.addAll(getInheritedMethods(classElement));
    for (ExecutableElement method : allMethods) {
        builder.addToMethods(methodAdapterFunction.apply(method));
    }
    for (AnnotationMirror annotationMirror : classElement.getAnnotationMirrors()) {
        builder.addToAnnotations(annotationAdapterFunction.apply(annotationMirror));
    }
    // Let's register the full blown definition
    TypeDef result = context.getDefinitionRepository().register(builder.build());
    // Also register other types
    if (context.isDeep()) {
        Set<TypeElement> references = new HashSet<>(context.getReferences());
        references.stream().filter(t -> !t.equals(classElement)).filter(t -> !t.toString().startsWith("sun.") && !t.toString().startsWith("com.sun.")).forEach(t -> {
            String fqcn = t.toString();
            TypeDef existing = context.getDefinitionRepository().getDefinition(fqcn);
            if (existing == null) {
                context.getDefinitionRepository().registerIfAbsent(fqcn, () -> apply(t));
            }
            context.getReferences().remove(t);
        });
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) Modifier(javax.lang.model.element.Modifier) Apt.getPackageName(io.sundr.adapter.apt.utils.Apt.getPackageName) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ClassRef(io.sundr.model.ClassRef) HashSet(java.util.HashSet) ElementFilter(javax.lang.model.util.ElementFilter) Types(io.sundr.model.utils.Types) LinkedHashSet(java.util.LinkedHashSet) Strings(io.sundr.utils.Strings) ElementKind(javax.lang.model.element.ElementKind) NoType(javax.lang.model.type.NoType) ExecutableElement(javax.lang.model.element.ExecutableElement) SundrException(io.sundr.SundrException) Set(java.util.Set) Element(javax.lang.model.element.Element) Method(io.sundr.model.Method) TypeRef(io.sundr.model.TypeRef) AnnotationRef(io.sundr.model.AnnotationRef) Kind(io.sundr.model.Kind) Collectors(java.util.stream.Collectors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Property(io.sundr.model.Property) TypeParameterElement(javax.lang.model.element.TypeParameterElement) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Apt.getClassName(io.sundr.adapter.apt.utils.Apt.getClassName) TypeDef(io.sundr.model.TypeDef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamDef(io.sundr.model.TypeParamDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder) TypeDef(io.sundr.model.TypeDef) TypeMirror(javax.lang.model.type.TypeMirror) ElementKind(javax.lang.model.element.ElementKind) Kind(io.sundr.model.Kind) SundrException(io.sundr.SundrException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NoType(javax.lang.model.type.NoType) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 93 with TypeDef

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

the class SourcesTest method shouldAdaptList.

@Test
public void shouldAdaptList() throws Exception {
    TypeDef typeDef = Sources.readTypeDefFromResource("java/util/List.java", context);
    assertNotNull(typeDef);
}
Also used : TypeDef(io.sundr.model.TypeDef) Test(org.junit.Test)

Example 94 with TypeDef

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

the class AbstractAdapterTest method testWithSimpleClass.

@Test
public void testWithSimpleClass() {
    T input = getInput(SimpleClass.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    assertNotNull(typeDef);
    assertEquals("SimpleClass", typeDef.getName());
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Test(org.junit.Test)

Example 95 with TypeDef

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

the class AbstractAdapterTest method testClassWithAnnotationParams.

@Test
public void testClassWithAnnotationParams() {
    T input = getInput(ClassWithAnnotation.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    final List<Property> properties = typeDef.getProperties();
    final Property foo = properties.stream().filter(p -> p.getName().equals("foo")).findFirst().orElseThrow(RuntimeException::new);
    List<AnnotationRef> annotations = foo.getAnnotations();
    assertEquals(1, annotations.size());
    AnnotationRef annotationRef = annotations.get(0);
    assertTrue(annotationRef.toString().contains("SimpleAnnotation"));
    assertEquals("foo", annotationRef.getParameters().get("name"));
    final Method bar = typeDef.getMethods().stream().filter(p -> p.getName().equals("bar")).findFirst().orElseThrow(RuntimeException::new);
    annotations = bar.getAnnotations();
    assertEquals(1, annotations.size());
    annotationRef = annotations.get(0);
    assertEquals("bar", annotationRef.getParameters().get("name"));
    Object params = annotationRef.getParameters().get("values");
    assertArrayEquals(new int[] { 1, 2, 3, 5, 7 }, (int[]) annotationRef.getParameters().get("values"));
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) 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