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

org.babyfish.jimmer.apt.immutable.ImmutableProcessor Maven / Gradle / Ivy

There is a newer version: 0.9.17
Show newest version
package org.babyfish.jimmer.apt.immutable;

import org.babyfish.jimmer.apt.Context;
import org.babyfish.jimmer.apt.entry.EntryProcessor;
import org.babyfish.jimmer.apt.immutable.generator.*;
import org.babyfish.jimmer.apt.immutable.meta.ImmutableType;

import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.HashMap;
import java.util.Map;

public class ImmutableProcessor {

    private final Context context;

    private final Filer filer;

    private final Messager messager;

    public ImmutableProcessor(Context context, Filer filer, Messager messager) {
        this.context = context;
        this.filer = filer;
        this.messager = messager;
    }

    public Map process(RoundEnvironment roundEnv) {
        Map immutableTypeMap = parseImmutableTypes(roundEnv);
        generateJimmerTypes(immutableTypeMap);
        return immutableTypeMap;
    }

    private Map parseImmutableTypes(RoundEnvironment roundEnv) {
        Map map = new HashMap<>();
        for (Element element : roundEnv.getRootElements()) {
            if (element instanceof TypeElement) {
                TypeElement typeElement = (TypeElement) element;
                if (context.isImmutable(typeElement) && context.include(typeElement)) {
                    ImmutableType immutableType = context.getImmutableType(typeElement);
                    map.put(typeElement, immutableType);
                }
            }
        }
        return map;
    }

    private void generateJimmerTypes(Map immutableTypeMap) {
        for (ImmutableType immutableType : immutableTypeMap.values()) {
            new DraftGenerator(
                    immutableType,
                    filer
            ).generate();
            new PropsGenerator(
                    context,
                    immutableType,
                    filer
            ).generate();
            messager.printMessage(Diagnostic.Kind.NOTE, "Immutable: " + immutableType.getQualifiedName());
            if (immutableType.isEntity()) {
                messager.printMessage(Diagnostic.Kind.NOTE, "Entity: " + immutableType.getQualifiedName());
                new TableGenerator(
                        context,
                        immutableType,
                        false,
                        filer
                ).generate();
                new TableGenerator(
                        context,
                        immutableType,
                        true,
                        filer
                ).generate();
                new FetcherGenerator(
                        context,
                        immutableType,
                        filer
                ).generate();
            } else if (immutableType.isEmbeddable()) {
                messager.printMessage(Diagnostic.Kind.NOTE, "Embeddable: " + immutableType.getQualifiedName());
                new PropExpressionGenerator(
                        context,
                        immutableType,
                        filer
                ).generate();
                new FetcherGenerator(
                        context,
                        immutableType,
                        filer
                ).generate();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy