net.jbock.writing.CreateModelMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbock-compiler Show documentation
Show all versions of jbock-compiler Show documentation
jbock annotation processor
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