Search in sources :

Example 26 with TypeDefBuilder

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

the class BuilderUtils method buildableField.

public static Property buildableField(Property property) {
    TypeRef typeRef = property.getTypeRef();
    ClassRef targetType = (ClassRef) TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(typeRef);
    boolean isArray = Types.isArray(typeRef);
    boolean isSet = Types.isSet(typeRef);
    boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
    boolean isList = Types.isList(typeRef);
    boolean isAbstractList = isList && Types.isAbstract(typeRef);
    boolean isMap = Types.isMap(typeRef);
    boolean isOptional = Types.isOptional(typeRef);
    boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
    boolean isOptionalLong = Types.isOptionalLong(typeRef);
    // For fields that are concrete we can possibly create an instance of a VisitableBuilder.
    // For everything else we can have a builder e.g. Builder<Foo> = () -> fooInstance but it won't be visitable
    ClassRef builderType = Types.isConcrete(targetType) ? TypeAs.BUILDER.apply(GetDefinition.of(targetType)).toInternalReference() : TypeAs.VISITABLE_BUILDER.apply(targetType);
    if (isArray || isList) {
        ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).withDimensions(0).build();
        TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(builderType)).withName("l").endArgument().endConstructor().build();
        return new PropertyBuilder(property).withTypeRef(listRef).addToAttributes(LAZY_INIT, " new " + listRef + "()").addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, listRef, builderType)).build();
    }
    if (isSet) {
        ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).build();
        TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(builderType)).withName("s").endArgument().endConstructor().build();
        return new PropertyBuilder(property).withTypeRef(setRef).addToAttributes(LAZY_INIT, " new " + setRef + "()").addToAttributes(INIT_FUNCTION, new Construct(setDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, setRef, builderType)).build();
    }
    if (isOptionalLong) {
        ClassRef optionalRef = Optionals.OPTIONAL_LONG.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalLong.empty()").build();
    }
    if (isOptionalDouble) {
        ClassRef optionalRef = Optionals.OPTIONAL_DOUBLE.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalDouble.empty()").build();
    }
    if (Types.isOptionalInt(targetType)) {
        ClassRef optionalRef = Optionals.OPTIONAL_INT.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalInt.empty()").build();
    }
    if (isOptional) {
        ClassRef optionalRef = Optionals.OPTIONAL.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " Optional.empty()").addToAttributes(ALSO_IMPORT, alsoImport(property, optionalRef, builderType)).build();
    }
    if (Types.isConcrete(builderType) && BuilderUtils.hasDefaultConstructor(builderType) && property.hasAttribute(DEFAULT_VALUE)) {
        return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).addToAttributes(INIT, "new " + builderType + "()").build();
    }
    return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).removeFromAttributes(INIT).build();
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Construct(io.sundr.builder.internal.functions.Construct) TypeDefBuilder(io.sundr.model.TypeDefBuilder) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 27 with TypeDefBuilder

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

the class InitEnricher method visit.

@Override
public void visit(PropertyBuilder builder) {
    TypeRef typeRef = builder.buildTypeRef();
    TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    boolean isBuildable = isBuildable(unwrapped);
    boolean hasDescendants = false;
    if (!(typeRef instanceof ClassRef)) {
        return;
    }
    Property parent = (Property) builder.getAttributes().get(DESCENDANT_OF);
    if (parent != null) {
        typeRef = parent.getTypeRef();
        unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    } else if (builder.getAttributes().containsKey(DESCENDANTS) && !((Collection) builder.getAttributes().get(DESCENDANTS)).isEmpty()) {
        hasDescendants = true;
    }
    List<TypeRef> arguments = ((ClassRef) typeRef).getArguments();
    TypeRef targetType = unwrapped;
    if (isBuildable || hasDescendants) {
        ClassRef unwarppedClassRef = (unwrapped instanceof ClassRef) ? (ClassRef) unwrapped : null;
        targetType = isAbstract(unwarppedClassRef) || GetDefinition.of(unwarppedClassRef).getKind() == Kind.INTERFACE ? TypeAs.VISITABLE_BUILDER.apply(unwarppedClassRef) : TypeAs.BUILDER.apply(GetDefinition.of(unwarppedClassRef)).toInternalReference();
    }
    boolean isArray = Types.isArray(typeRef);
    boolean isSet = Types.isSet(typeRef);
    boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
    boolean isList = Types.isList(typeRef);
    boolean isAbstractList = isList && Types.isAbstract(typeRef);
    boolean isMap = Types.isMap(typeRef);
    boolean isAbstractMap = isMap && Types.isAbstract(typeRef);
    boolean isOptional = Types.isOptional(typeRef);
    boolean isOptionalInt = Types.isOptionalInt(typeRef);
    boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
    boolean isOptionalLong = Types.isOptionalLong(typeRef);
    if (isArray || isList) {
        ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).withDimensions(0).build();
        TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(targetType)).withName("l").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + listRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, listRef));
    } else if (isSet) {
        ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).build();
        TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(targetType)).withName("s").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + setRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(setDef, unwrapped)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, setRef));
    } else if (isMap) {
        ClassRef mapRef = isAbstractMap ? Collections.LINKED_HASH_MAP.toReference(arguments) : new ClassRefBuilder((ClassRef) typeRef).withArguments(arguments).build();
        TypeDef mapDef = new TypeDefBuilder(TypeDef.forName(mapRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.MAP.toReference(arguments)).withName("m").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + mapRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_MAP_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_MAP_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(mapDef, arguments)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, mapRef));
    } else if (isOptional) {
        final ClassRef ref = new ClassRefBuilder(Optionals.OPTIONAL.toReference()).withArguments(java.util.Collections.EMPTY_LIST).build();
        builder.addToAttributes(INIT, "Optional.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalDouble) {
        final ClassRef ref = Optionals.OPTIONAL_DOUBLE.toReference();
        builder.addToAttributes(INIT, "OptionalDouble.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_DOUBLE, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalInt) {
        final ClassRef ref = Optionals.OPTIONAL_INT.toReference();
        builder.addToAttributes(INIT, "OptionalInt.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_INT, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalLong) {
        final ClassRef ref = Optionals.OPTIONAL_LONG.toReference();
        builder.addToAttributes(INIT, "OptionalLong.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_LONG, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Construct(io.sundr.builder.internal.functions.Construct) Property(io.sundr.model.Property) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 28 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder 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 29 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder 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 30 with TypeDefBuilder

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

the class TypeDeclarationToTypeDef method apply.

@Override
public TypeDef apply(TypeDeclaration type) {
    if (type instanceof ClassOrInterfaceDeclaration) {
        ClassOrInterfaceDeclaration decl = (ClassOrInterfaceDeclaration) type;
        Kind kind = decl.isInterface() ? Kind.INTERFACE : Kind.CLASS;
        List<TypeParamDef> parameters = new ArrayList<TypeParamDef>();
        List<ClassRef> extendsList = new ArrayList<ClassRef>();
        List<ClassRef> implementsList = new ArrayList<ClassRef>();
        List<Property> properties = new ArrayList<Property>();
        List<Method> methods = new ArrayList<Method>();
        List<Method> constructors = new ArrayList<Method>();
        List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
        for (AnnotationExpr annotationExpr : decl.getAnnotations()) {
            annotations.add(ANNOTATIONREF.apply(annotationExpr));
        }
        for (TypeParameter typeParameter : decl.getTypeParameters()) {
            parameters.add(typeParameterToTypeParamDef.apply(typeParameter));
        }
        for (ClassOrInterfaceType classOrInterfaceType : decl.getExtends()) {
            extendsList.add((ClassRef) classOrInterfaceToTypeRef.apply(classOrInterfaceType));
        }
        for (ClassOrInterfaceType classOrInterfaceType : decl.getImplements()) {
            implementsList.add((ClassRef) classOrInterfaceToTypeRef.apply(classOrInterfaceType));
        }
        for (BodyDeclaration bodyDeclaration : decl.getMembers()) {
            if (bodyDeclaration instanceof FieldDeclaration) {
                FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
                for (VariableDeclarator var : fieldDeclaration.getVariables()) {
                    TypeRef fieldDeclRef = typeToTypeRef.apply(fieldDeclaration.getType());
                    TypeRef typeRef = checkAgainstTypeParamRef(fieldDeclRef, parameters);
                    properties.add(new PropertyBuilder().withName(var.getId().getName()).withTypeRef(typeRef).withModifiers(fieldDeclaration.getModifiers()).addToAttributes(Attributeable.INIT, var.getInit() != null ? var.getInit().toStringWithoutComments() : null).build());
                }
            } else if (bodyDeclaration instanceof MethodDeclaration) {
                MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
                List<Property> arguments = new ArrayList<Property>();
                List<ClassRef> exceptions = new ArrayList<ClassRef>();
                List<AnnotationRef> methodAnnotations = new ArrayList<AnnotationRef>();
                for (AnnotationExpr annotationExpr : methodDeclaration.getAnnotations()) {
                    methodAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                }
                for (ReferenceType referenceType : methodDeclaration.getThrows()) {
                    TypeRef exceptionRef = typeToTypeRef.apply(referenceType.getType());
                    if (exceptionRef instanceof ClassRef) {
                        exceptions.add((ClassRef) exceptionRef);
                    }
                }
                Boolean preferVarArg = false;
                for (Parameter parameter : methodDeclaration.getParameters()) {
                    List<AnnotationRef> paramAnnotations = new ArrayList<AnnotationRef>();
                    for (AnnotationExpr annotationExpr : parameter.getAnnotations()) {
                        paramAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                    }
                    TypeRef typeRef = typeToTypeRef.apply(parameter.getType());
                    if (parameter.isVarArgs()) {
                        preferVarArg = true;
                        typeRef = typeRef.withDimensions(typeRef.getDimensions() + 1);
                    }
                    arguments.add(new PropertyBuilder().withName(parameter.getId().getName()).withTypeRef(typeRef).withModifiers(parameter.getModifiers()).withAnnotations(paramAnnotations).build());
                }
                List<TypeParamDef> typeParamDefs = new ArrayList<TypeParamDef>();
                for (TypeParameter typeParameter : methodDeclaration.getTypeParameters()) {
                    typeParamDefs.add(typeParameterToTypeParamDef.apply(typeParameter));
                }
                TypeRef returnType = checkAgainstTypeParamRef(typeToTypeRef.apply(methodDeclaration.getType()), parameters);
                methods.add(new MethodBuilder().withName(methodDeclaration.getName()).withDefaultMethod(methodDeclaration.isDefault()).withModifiers(methodDeclaration.getModifiers()).withParameters(typeParamDefs).withVarArgPreferred(preferVarArg).withReturnType(returnType).withExceptions(exceptions).withArguments(arguments).withAnnotations(methodAnnotations).withBlock(BLOCK.apply(methodDeclaration.getBody())).build());
            } else if (bodyDeclaration instanceof ConstructorDeclaration) {
                ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) bodyDeclaration;
                List<Property> arguments = new ArrayList<Property>();
                List<ClassRef> exceptions = new ArrayList<ClassRef>();
                List<AnnotationRef> ctorAnnotations = new ArrayList<AnnotationRef>();
                for (AnnotationExpr annotationExpr : constructorDeclaration.getAnnotations()) {
                    ctorAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                }
                for (ReferenceType referenceType : constructorDeclaration.getThrows()) {
                    TypeRef exceptionRef = typeToTypeRef.apply(referenceType.getType());
                    exceptions.add((ClassRef) exceptionRef);
                }
                for (Parameter parameter : constructorDeclaration.getParameters()) {
                    List<AnnotationRef> ctorParamAnnotations = new ArrayList<AnnotationRef>();
                    for (AnnotationExpr annotationExpr : parameter.getAnnotations()) {
                        ctorParamAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                    }
                    TypeRef typeRef = checkAgainstTypeParamRef(typeToTypeRef.apply(parameter.getType()), parameters);
                    arguments.add(new PropertyBuilder().withName(parameter.getId().getName()).withTypeRef(typeRef).withModifiers(parameter.getModifiers()).withAnnotations(ctorParamAnnotations).build());
                }
                constructors.add(new MethodBuilder().withModifiers(constructorDeclaration.getModifiers()).withExceptions(exceptions).withArguments(arguments).withAnnotations(ctorAnnotations).withBlock(BLOCK.apply(constructorDeclaration.getBlock())).build());
            }
        }
        return context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withPackageName(PACKAGENAME.apply(type)).withName(decl.getName()).withModifiers(type.getModifiers()).withParameters(parameters).withExtendsList(extendsList).withImplementsList(implementsList).withProperties(properties).withMethods(methods).withConstructors(constructors).withAnnotations(annotations).addToAttributes(TypeDef.ALSO_IMPORT, IMPORTS.apply(type)).build());
    }
    if (type instanceof AnnotationDeclaration) {
        AnnotationDeclaration decl = (AnnotationDeclaration) type;
        Kind kind = Kind.ANNOTATION;
        List<Method> methods = new ArrayList<Method>();
        for (BodyDeclaration bodyDeclaration : decl.getMembers()) {
            if (bodyDeclaration instanceof AnnotationMemberDeclaration) {
                Map<AttributeKey, Object> attributes = new HashMap<>();
                AnnotationMemberDeclaration annotationMemberDeclaration = (AnnotationMemberDeclaration) bodyDeclaration;
                if (annotationMemberDeclaration.getDefaultValue() != null) {
                    attributes.put(Attributeable.DEFAULT_VALUE, annotationMemberDeclaration.getDefaultValue().toString());
                }
                TypeRef returnType = typeToTypeRef.apply(annotationMemberDeclaration.getType());
                methods.add(new MethodBuilder().withName(annotationMemberDeclaration.getName()).withModifiers(annotationMemberDeclaration.getModifiers()).withReturnType(returnType).withAttributes(attributes).build());
            }
        }
        List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
        for (AnnotationExpr annotationExpr : decl.getAnnotations()) {
            annotations.add(ANNOTATIONREF.apply(annotationExpr));
        }
        return context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withPackageName(PACKAGENAME.apply(type)).withName(decl.getName()).withModifiers(type.getModifiers()).withMethods(methods).withAnnotations(annotations).addToAttributes(TypeDef.ALSO_IMPORT, IMPORTS.apply(type)).build());
    }
    throw new IllegalArgumentException("Unsupported TypeDeclaration:[" + type + "].");
}
Also used : TypeParamDef(io.sundr.model.TypeParamDef) TypeParameter(com.github.javaparser.ast.TypeParameter) ClassRef(io.sundr.model.ClassRef) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeDefBuilder(io.sundr.model.TypeDefBuilder) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ReferenceType(com.github.javaparser.ast.type.ReferenceType) MethodBuilder(io.sundr.model.MethodBuilder) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Kind(io.sundr.model.Kind) ArrayList(java.util.ArrayList) List(java.util.List) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) PropertyBuilder(io.sundr.model.PropertyBuilder) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Method(io.sundr.model.Method) AttributeKey(io.sundr.model.AttributeKey) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.TypeParameter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration)

Aggregations

TypeDefBuilder (io.sundr.model.TypeDefBuilder)30 TypeDef (io.sundr.model.TypeDef)25 ClassRef (io.sundr.model.ClassRef)14 ArrayList (java.util.ArrayList)14 TypeRef (io.sundr.model.TypeRef)11 Property (io.sundr.model.Property)10 Method (io.sundr.model.Method)9 HashMap (java.util.HashMap)8 Collectors (java.util.stream.Collectors)8 Element (javax.lang.model.element.Element)8 TypeElement (javax.lang.model.element.TypeElement)8 MethodBuilder (io.sundr.model.MethodBuilder)7 PropertyBuilder (io.sundr.model.PropertyBuilder)7 TypeParamDef (io.sundr.model.TypeParamDef)7 List (java.util.List)7 AnnotationRef (io.sundr.model.AnnotationRef)6 HashSet (java.util.HashSet)6 TypedVisitor (io.sundr.builder.TypedVisitor)5 Map (java.util.Map)5 Set (java.util.Set)5