Search in sources :

Example 1 with ExternalBuildables

use of io.sundr.builder.annotations.ExternalBuildables 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 2 with ExternalBuildables

use of io.sundr.builder.annotations.ExternalBuildables in project sundrio by sundrio.

the class AbstractBuilderProcessor method generateBuildables.

public void generateBuildables(BuilderContext ctx, Set<TypeDef> buildables) {
    int total = ctx.getBuildableRepository().getBuildables().size();
    int count = 0;
    for (TypeDef typeDef : buildables) {
        RichTypeDef richTypeDef = TypeArguments.apply(typeDef);
        double percentage = 100d * (count++) / total;
        if (typeDef.isInterface() || typeDef.isAnnotation()) {
            continue;
        }
        System.err.printf("\033[2K%3d%% Generating: %s\r", Math.round(percentage), typeDef.getFullyQualifiedName());
        generate(ClazzAs.FLUENT_INTERFACE.apply(richTypeDef));
        generate(ClazzAs.FLUENT_IMPL.apply(richTypeDef));
        if (typeDef.isAbstract()) {
            continue;
        }
        if (!typeDef.isFinal() && typeDef.getAttributes().containsKey(EDITABLE_ENABLED) && (Boolean) typeDef.getAttributes().get(EDITABLE_ENABLED)) {
            generate(ClazzAs.EDITABLE_BUILDER.apply(richTypeDef));
            generate(ClazzAs.EDITABLE.apply(richTypeDef));
        } else {
            generate(ClazzAs.BUILDER.apply(richTypeDef));
        }
        Buildable buildable = typeDef.getAttribute(BUILDABLE);
        ExternalBuildables externalBuildables = typeDef.getAttribute(EXTERNAL_BUILDABLE);
        if (buildable != null) {
            for (final Inline inline : buildable.inline()) {
                generate(inlineableOf(ctx, typeDef, inline));
            }
        } else if (externalBuildables != null) {
            for (final Inline inline : externalBuildables.inline()) {
                generate(inlineableOf(ctx, typeDef, inline));
            }
        }
    }
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) ExternalBuildables(io.sundr.builder.annotations.ExternalBuildables) Inline(io.sundr.builder.annotations.Inline) Buildable(io.sundr.builder.annotations.Buildable) RichTypeDef(io.sundr.model.RichTypeDef)

Aggregations

ExternalBuildables (io.sundr.builder.annotations.ExternalBuildables)2 TypeDef (io.sundr.model.TypeDef)2 AptContext (io.sundr.adapter.apt.AptContext)1 Visitor (io.sundr.builder.Visitor)1 Buildable (io.sundr.builder.annotations.Buildable)1 Inline (io.sundr.builder.annotations.Inline)1 BuilderContext (io.sundr.builder.internal.BuilderContext)1 PropertyBuilder (io.sundr.model.PropertyBuilder)1 RichTypeDef (io.sundr.model.RichTypeDef)1 TypeDefBuilder (io.sundr.model.TypeDefBuilder)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Filer (javax.annotation.processing.Filer)1 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)1 Element (javax.lang.model.element.Element)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1 Elements (javax.lang.model.util.Elements)1 Types (javax.lang.model.util.Types)1