All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.babyfish.jimmer.apt.dto.DtoProcessor Maven / Gradle / Ivy

There is a newer version: 0.8.177
Show newest version
package org.babyfish.jimmer.apt.dto;

import org.babyfish.jimmer.Immutable;
import org.babyfish.jimmer.apt.Context;
import org.babyfish.jimmer.apt.immutable.meta.ImmutableProp;
import org.babyfish.jimmer.apt.immutable.meta.ImmutableType;
import org.babyfish.jimmer.dto.compiler.DtoAstException;
import org.babyfish.jimmer.dto.compiler.DtoFile;
import org.babyfish.jimmer.dto.compiler.DtoModifier;
import org.babyfish.jimmer.dto.compiler.DtoType;
import org.babyfish.jimmer.sql.Embeddable;
import org.babyfish.jimmer.sql.Entity;

import javax.annotation.processing.Filer;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import java.util.*;

public class DtoProcessor {

    private final Context context;

    private final Elements elements;

    private final Filer filer;

    private final Collection dtoDirs;

    private final DtoModifier defaultNullableInputModifier;

    public DtoProcessor(
            Context context,
            Elements elements,
            Filer filer,
            Collection dtoDirs, DtoModifier defaultNullableInputModifier
    ) {
        this.context = context;
        this.elements = elements;
        this.filer = filer;
        this.dtoDirs = dtoDirs;
        this.defaultNullableInputModifier = defaultNullableInputModifier;
    }

    public boolean process() {
        Map>> dtoTypeMap = parseDtoTypes();
        return generateDtoTypes(dtoTypeMap);
    }

    private Map>> parseDtoTypes() {
        Map>> dtoTypeMap = new LinkedHashMap<>();
        DtoContext dtoContext = new DtoContext(filer, dtoDirs);
        AptDtoCompiler compiler;

        for (DtoFile dtoFile : dtoContext.getDtoFiles()) {
            try {
                compiler = new AptDtoCompiler(dtoFile, elements, defaultNullableInputModifier);
            } catch (DtoAstException ex) {
                throw new DtoException(
                        "Failed to parse \"" +
                                dtoFile.getAbsolutePath() +
                                "\": " +
                                ex.getMessage(),
                        ex
                );
            } catch (Throwable ex) {
                throw new DtoException(
                        "Failed to read \"" +
                                dtoFile.getAbsolutePath() +
                                "\": " +
                                ex.getMessage(),
                        ex
                );
            }
            TypeElement typeElement = elements.getTypeElement(compiler.getSourceTypeName());
            if (typeElement == null) {
                throw new DtoException(
                        "Failed to parse \"" +
                                dtoFile.getAbsolutePath() +
                                "\": No immutable type \"" +
                                compiler.getSourceTypeName() +
                                "\""
                );
            }
            if (!context.include(typeElement)) {
                continue;
            }
            if (typeElement.getAnnotation(Entity.class) == null &&
            typeElement.getAnnotation(Embeddable.class) == null &&
            typeElement.getAnnotation(Immutable.class) == null) {
                throw new DtoException(
                        "Failed to parse \"" +
                                dtoFile.getAbsolutePath() +
                                "\": the \"" +
                                compiler.getSourceTypeName() +
                                "\" is not decorated by \"@" +
                                Entity.class.getName() +
                                "\", \"" +
                                Embeddable.class.getName() +
                                "\" or \"" +
                                Immutable.class.getName() +
                                "\""
                );
            }
            ImmutableType immutableType = context.getImmutableType(typeElement);
            dtoTypeMap
                    .computeIfAbsent(immutableType, it -> new ArrayList<>())
                    .addAll(compiler.compile(immutableType));
        }
        return dtoTypeMap;
    }

    private boolean generateDtoTypes(Map>> dtoTypeMap) {
        boolean result = false;
        for (List> dtoTypes : dtoTypeMap.values()) {
            for (DtoType dtoType : dtoTypes) {
                new DtoGenerator(context, dtoType, filer).generate();
                result = true;
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy