Search in sources :

Example 21 with TypeDef

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

the class BuildableProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    Elements elements = processingEnv.getElementUtils();
    Types types = processingEnv.getTypeUtils();
    Filer filer = processingEnv.getFiler();
    BuilderContext ctx = null;
    // First pass register all buildables
    Set<TypeDef> buildables = new HashSet<>();
    for (TypeElement typeElement : annotations) {
        for (Element element : env.getElementsAnnotatedWith(typeElement)) {
            Buildable buildable = element.getAnnotation(Buildable.class);
            if (buildable == null) {
                continue;
            }
            AptContext aptContext = AptContext.create(elements, types, DefinitionRepository.getRepository());
            ctx = BuilderContextManager.create(elements, types, buildable.validationEnabled(), buildable.generateBuilderPackage(), buildable.builderPackage());
            TypeDef b = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(element), aptContext)).addToAttributes(BUILDABLE, buildable).addToAttributes(EDITABLE_ENABLED, buildable.editableEnabled()).addToAttributes(VALIDATION_ENABLED, buildable.validationEnabled()).accept(new Visitor<PropertyBuilder>() {

                @Override
                public void visit(PropertyBuilder builder) {
                    builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, buildable.lazyCollectionInitEnabled());
                    builder.addToAttributes(LAZY_MAP_INIT_ENABLED, buildable.lazyMapInitEnabled());
                }
            }).build();
            ctx.getDefinitionRepository().register(b);
            ctx.getBuildableRepository().register(b);
            buildables.add(b);
            for (TypeElement ref : BuilderUtils.getBuildableReferences(ctx, buildable)) {
                TypeDef r = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(ref), aptContext)).addToAttributes(BUILDABLE, buildable).addToAttributes(EDITABLE_ENABLED, buildable.editableEnabled()).addToAttributes(VALIDATION_ENABLED, buildable.validationEnabled()).accept(new Visitor<PropertyBuilder>() {

                    @Override
                    public void visit(PropertyBuilder builder) {
                        builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, buildable.lazyCollectionInitEnabled());
                        builder.addToAttributes(LAZY_MAP_INIT_ENABLED, buildable.lazyMapInitEnabled());
                    }
                }).build();
                ctx.getDefinitionRepository().register(r);
                ctx.getBuildableRepository().register(r);
                buildables.add(r);
            }
        }
    }
    if (ctx == null) {
        return true;
    }
    generateLocalDependenciesIfNeeded();
    ctx.getDefinitionRepository().updateReferenceMap();
    generateBuildables(ctx, buildables);
    generatePojos(ctx, buildables);
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format("%-120s", "100%: Builder generation complete."));
    return false;
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) AptContext(io.sundr.adapter.apt.AptContext) Visitor(io.sundr.builder.Visitor) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Elements(javax.lang.model.util.Elements) TypeDefBuilder(io.sundr.model.TypeDefBuilder) TypeDef(io.sundr.model.TypeDef) BuilderContext(io.sundr.builder.internal.BuilderContext) Filer(javax.annotation.processing.Filer) Buildable(io.sundr.builder.annotations.Buildable) HashSet(java.util.HashSet) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 22 with TypeDef

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

the class ExternalBuildableProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    Elements elements = processingEnv.getElementUtils();
    Types types = processingEnv.getTypeUtils();
    Filer filer = processingEnv.getFiler();
    BuilderContext ctx = null;
    Set<TypeDef> buildables = new HashSet<>();
    // First pass register all externals
    for (TypeElement annotation : annotations) {
        for (Element element : env.getElementsAnnotatedWith(annotation)) {
            final ExternalBuildables generated = element.getAnnotation(ExternalBuildables.class);
            if (generated == null) {
                continue;
            }
            ctx = BuilderContextManager.create(elements, types, generated.validationEnabled(), generated.generateBuilderPackage(), generated.builderPackage());
            for (String name : generated.value()) {
                PackageElement packageElement = elements.getPackageElement(name);
                List<TypeElement> typeElements = new ArrayList<>();
                if (packageElement != null) {
                    for (Element e : packageElement.getEnclosedElements()) {
                        if (e instanceof TypeElement) {
                            typeElements.add((TypeElement) e);
                        }
                    }
                } else {
                    TypeElement e = elements.getTypeElement(name);
                    if (e != null) {
                        typeElements.add(e);
                    }
                }
                for (TypeElement typeElement : typeElements) {
                    final boolean isLazyCollectionInitEnabled = generated.lazyCollectionInitEnabled();
                    final boolean isLazyMapInitEnabled = generated.lazyMapInitEnabled();
                    final boolean includeInterfaces = generated.includeInterfaces();
                    final boolean includeAbstractClasses = generated.includeAbstractClasses();
                    AptContext aptContext = AptContext.create(ctx.getElements(), ctx.getTypes(), ctx.getDefinitionRepository());
                    TypeDef original = Adapters.adaptType(typeElement, aptContext);
                    String fqcn = original.getFullyQualifiedName();
                    boolean isBuildable = original.getKind() != Kind.ENUM && (includeAbstractClasses || !original.isAbstract()) && (includeInterfaces || original.getKind() != Kind.INTERFACE) && isIncluded(fqcn, generated.includes()) && !isExcluded(fqcn, generated.excludes());
                    TypeDef b = new TypeDefBuilder(original).accept(new Visitor<PropertyBuilder>() {

                        @Override
                        public void visit(PropertyBuilder builder) {
                            if (isBuildable) {
                                builder.addToAttributes(EXTERNAL_BUILDABLE, generated);
                                builder.addToAttributes(EDITABLE_ENABLED, generated.editableEnabled());
                                builder.addToAttributes(VALIDATION_ENABLED, generated.validationEnabled());
                                builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, isLazyCollectionInitEnabled);
                                builder.addToAttributes(LAZY_MAP_INIT_ENABLED, isLazyMapInitEnabled);
                            }
                        }
                    }).build();
                    if (b.getKind() == Kind.ENUM) {
                        continue;
                    }
                    if (b.isAbstract() && !includeAbstractClasses) {
                        continue;
                    }
                    if (b.getKind() == Kind.INTERFACE && !includeInterfaces) {
                        continue;
                    }
                    if (!isIncluded(b.getFullyQualifiedName(), generated.includes())) {
                        continue;
                    }
                    if (isExcluded(b.getFullyQualifiedName(), generated.excludes())) {
                        continue;
                    }
                    ctx.getDefinitionRepository().register(b);
                    ctx.getBuildableRepository().register(b);
                    buildables.add(b);
                }
            }
            for (TypeElement ref : BuilderUtils.getBuildableReferences(ctx, generated)) {
                final boolean isLazyCollectionInitEnabled = generated.lazyCollectionInitEnabled();
                final boolean isLazyMapInitEnabled = generated.lazyMapInitEnabled();
                final boolean includeInterfaces = generated.includeInterfaces();
                final boolean includeAbstractClasses = generated.includeAbstractClasses();
                AptContext aptContext = AptContext.create(ctx.getElements(), ctx.getTypes(), ctx.getDefinitionRepository());
                TypeDef original = Adapters.adaptType(Apt.getClassElement(ref), aptContext);
                String fqcn = original.getFullyQualifiedName();
                boolean isBuildable = original.getKind() != Kind.ENUM && !original.isAbstract() && isIncluded(fqcn, generated.includes()) && !isExcluded(fqcn, generated.excludes());
                TypeDef r = new TypeDefBuilder(original).accept(new Visitor<PropertyBuilder>() {

                    @Override
                    public void visit(PropertyBuilder builder) {
                        if (isBuildable) {
                            builder.addToAttributes(EXTERNAL_BUILDABLE, generated);
                            builder.addToAttributes(EDITABLE_ENABLED, generated.editableEnabled());
                            builder.addToAttributes(VALIDATION_ENABLED, generated.validationEnabled());
                            builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, isLazyCollectionInitEnabled);
                            builder.addToAttributes(LAZY_MAP_INIT_ENABLED, isLazyMapInitEnabled);
                        }
                    }
                }).build();
                if (r.getKind() == Kind.ENUM || r.isAbstract()) {
                    continue;
                }
                if (r.getKind() == Kind.ENUM) {
                    continue;
                }
                if (r.isAbstract() && !includeAbstractClasses) {
                    continue;
                }
                if (r.getKind() == Kind.INTERFACE && !includeInterfaces) {
                    continue;
                }
                if (!isIncluded(r.getFullyQualifiedName(), generated.includes())) {
                    continue;
                }
                if (isExcluded(r.getFullyQualifiedName(), generated.excludes())) {
                    continue;
                }
                ctx.getDefinitionRepository().register(r);
                ctx.getBuildableRepository().register(r);
                buildables.add(r);
            }
        }
    }
    if (ctx == null) {
        return true;
    }
    generateLocalDependenciesIfNeeded();
    ctx.getDefinitionRepository().updateReferenceMap();
    generateBuildables(ctx, buildables);
    generatePojos(ctx, buildables);
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format("%-120s", "100%: Builder generation complete."));
    return true;
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) AptContext(io.sundr.adapter.apt.AptContext) Visitor(io.sundr.builder.Visitor) ExternalBuildables(io.sundr.builder.annotations.ExternalBuildables) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) Elements(javax.lang.model.util.Elements) TypeDefBuilder(io.sundr.model.TypeDefBuilder) TypeDef(io.sundr.model.TypeDef) BuilderContext(io.sundr.builder.internal.BuilderContext) PackageElement(javax.lang.model.element.PackageElement) Filer(javax.annotation.processing.Filer) HashSet(java.util.HashSet) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 23 with TypeDef

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

the class BuilderUtils method fullyQualifiedNameDiff.

public static String fullyQualifiedNameDiff(TypeRef typeRef, TypeDef originType) {
    Map<String, String> map = DefinitionRepository.getRepository().getReferenceMap();
    String currentPackage = originType != null ? originType.getPackageName() : null;
    if (typeRef instanceof ClassRef) {
        TypeRef unwrapped = TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF, UNWRAP_MAP_VALUE_OF).apply(typeRef);
        if (unwrapped instanceof ClassRef) {
            ClassRef classRef = (ClassRef) unwrapped;
            String candidateFqn = classRef.getFullyQualifiedName().replace(classRef.getPackageName(), currentPackage);
            // If classRef is inside the current package.
            if (candidateFqn.equals(classRef.getFullyQualifiedName())) {
                return "";
            }
            // If candidate is imported and different that the actual name, do a diff
            if (originType.getImports().contains(candidateFqn) && !classRef.getFullyQualifiedName().equals(candidateFqn)) {
                return capitalizeFirst(Types.fullyQualifiedNameDiff(candidateFqn, classRef.getFullyQualifiedName()));
            }
            // If not then we compare against what has been found in the map.
            String fqcn = map.get(classRef.getName());
            TypeDef mainDef = fqcn != null ? DefinitionRepository.getRepository().getDefinition(fqcn) : null;
            boolean mainBuildable = mainDef != null ? isBuildable(mainDef) : false;
            if (fqcn == null) {
                System.out.println("Warning: Expected to find class with name:" + classRef.getName());
            } else if (!classRef.getFullyQualifiedName().equals(fqcn) && mainBuildable) {
                return capitalizeFirst(Types.fullyQualifiedNameDiff(fqcn, classRef.getFullyQualifiedName()));
            }
        }
    }
    return "";
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef)

Example 24 with TypeDef

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

the class BuilderUtils method hasDefaultConstructor.

/**
 * Checks if there is a default constructor available.
 *
 * @param item The clazz to check.
 * @return True if default constructor is found, false otherwise.
 */
public static boolean hasDefaultConstructor(TypeRef item) {
    DefinitionRepository repository = DefinitionRepository.getRepository();
    TypeDef def = repository.getDefinition(item);
    if (def == null && item instanceof ClassRef) {
        def = GetDefinition.of((ClassRef) item);
    }
    return hasDefaultConstructor(def);
}
Also used : DefinitionRepository(io.sundr.model.repo.DefinitionRepository) TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef)

Example 25 with TypeDef

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

the class SimpleAnnotationTest method testPojo.

@Test
public void testPojo() {
    System.out.println(simpleClassDef);
    TypeDef pojo = new ToPojo().apply(TypeArguments.apply(simpleClassDef));
    Assert.assertNotNull(pojo);
}
Also used : TypeDef(io.sundr.model.TypeDef) ToPojo(io.sundr.builder.internal.functions.ToPojo) 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