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

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

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

import io.jbock.javapoet.ClassName;
import io.jbock.javapoet.CodeBlock;
import io.jbock.javapoet.MethodSpec;
import io.jbock.javapoet.ParameterSpec;
import io.jbock.javapoet.ParameterizedTypeName;
import io.jbock.javapoet.TypeName;
import io.jbock.javapoet.TypeSpec;
import io.jbock.simple.Inject;
import net.jbock.annotated.Item;
import net.jbock.annotated.Option;
import net.jbock.annotated.Parameter;
import net.jbock.annotated.VarargsParameter;
import net.jbock.common.Suppliers;
import net.jbock.convert.Mapping;
import net.jbock.model.ItemType;
import net.jbock.parse.ParseResult;
import net.jbock.util.ExConvert;
import net.jbock.util.ExFailure;
import net.jbock.util.ExMissingItem;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Supplier;

import static java.util.stream.Collectors.toList;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
import static net.jbock.common.Constants.EITHERS;
import static net.jbock.common.Constants.STRING;
import static net.jbock.writing.CodeBlocks.joinByNewline;

/**
 * Implementation of the command class.
 */
final class ImplClass extends HasCommandRepresentation {

    private final GeneratedTypes generatedTypes;

    @Inject
    ImplClass(GeneratedTypes generatedTypes,
              CommandRepresentation commandRepresentation) {
        super(commandRepresentation);
        this.generatedTypes = generatedTypes;
    }

    TypeSpec define() {
        TypeSpec.Builder spec = TypeSpec.classBuilder(generatedTypes.implType());
        if (sourceElement().isInterface()) {
            spec.addSuperinterface(sourceElement().typeName());
        } else {
            spec.superclass(sourceElement().typeName());
        }
        return spec.addModifiers(PRIVATE, STATIC, FINAL)
                .addMethod(constructor())
                .addMethod(generateToString())
                .addFields(allMappings().stream()
                        .map(Mapping::field)
                        .collect(toList()))
                .addMethods(allMappings().stream()
                        .map(this::parameterMethodOverride)
                        .collect(toList()))
                .build();
    }

    private MethodSpec parameterMethodOverride(Mapping m) {
        Item sourceMethod = m.item();
        return MethodSpec.methodBuilder(sourceMethod.methodName())
                .returns(TypeName.get(sourceMethod.returnType()))
                .addModifiers(sourceMethod.accessModifiers())
                .addStatement("return $N", m.field())
                .addAnnotation(Override.class)
                .build();
    }

    private final Supplier resultSupplier = Suppliers.memoize(() -> {
        ParameterizedTypeName resultType = ParameterizedTypeName.get(ClassName.get(ParseResult.class),
                optType());
        return ParameterSpec.builder(resultType, "result").build();
    });

    private ParameterSpec result() {
        return resultSupplier.get();
    }

    private MethodSpec constructor() {
        MethodSpec.Builder spec = MethodSpec.constructorBuilder();
        for (int i = 0; i < namedOptions().size(); i++) {
            Mapping




© 2015 - 2024 Weber Informatics LLC | Privacy Policy