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

net.jbock.writing.CreateModelMethod Maven / Gradle / Ivy

There is a newer version: 5.18
Show newest version
package net.jbock.writing;

import io.jbock.javapoet.CodeBlock;
import io.jbock.javapoet.MethodSpec;
import io.jbock.simple.Inject;
import net.jbock.annotated.Option;
import net.jbock.convert.Mapping;
import net.jbock.model.CommandModel;
import net.jbock.model.Multiplicity;
import net.jbock.model.Parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static io.jbock.javapoet.MethodSpec.methodBuilder;
import static net.jbock.common.Suppliers.memoize;
import static net.jbock.writing.CodeBlocks.joinByComma;
import static net.jbock.writing.CodeBlocks.joinByNewline;

final class CreateModelMethod extends HasCommandRepresentation {

    @Inject
    CreateModelMethod(CommandRepresentation commandRepresentation) {
        super(commandRepresentation);
    }

    private final Supplier define = memoize(() -> {
        List code = new ArrayList<>();
        code.add(CodeBlock.of("return $T.builder()", CommandModel.class));
        sourceElement().descriptionKey().ifPresent(key ->
                code.add(CodeBlock.of(".withDescriptionKey($S)", key)));
        for (String descriptionLine : sourceElement().description()) {
            code.add(CodeBlock.of(".addDescriptionLine($S)", descriptionLine));
        }
        code.add(CodeBlock.of(".withProgramName($S)", sourceElement().programName()));
        if (isSuperCommand()) {
            code.add(CodeBlock.of(".withSuperCommand($L)", true));
        }
        for (Mapping




© 2015 - 2024 Weber Informatics LLC | Privacy Policy