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

net.jbock.context.MultilineConverter Maven / Gradle / Ivy

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

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import net.jbock.convert.Mapping;
import net.jbock.util.StringConverter;

import javax.inject.Inject;

import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PROTECTED;
import static javax.lang.model.element.Modifier.STATIC;
import static net.jbock.common.Constants.STRING;

@ContextScope
public class MultilineConverter {

    private final GeneratedTypes generatedTypes;

    @Inject
    MultilineConverter(GeneratedTypes generatedTypes) {
        this.generatedTypes = generatedTypes;
    }

    TypeSpec define(Mapping m) {
        return TypeSpec.classBuilder(generatedTypes.multilineConverterType(m))
                .addMethod(convertMethod(m))
                .superclass(ParameterizedTypeName.get(
                        ClassName.get(StringConverter.class),
                        TypeName.get(m.baseType())))
                .addModifiers(PRIVATE, STATIC)
                .build();
    }

    private MethodSpec convertMethod(Mapping m) {
        MethodSpec.Builder spec = MethodSpec.methodBuilder("convert");
        spec.addAnnotation(Override.class);
        spec.addCode(m.mapExpr());
        spec.addParameter(ParameterSpec.builder(STRING, "token").build());
        spec.addModifiers(PROTECTED);
        spec.returns(TypeName.get(m.baseType()));
        return spec.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy