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

ftl.cdi.$$BeanSupplierTemplateftl Maven / Gradle / Ivy

There is a newer version: 0.11
Show newest version
<#include "../common-lib.javaftl">
<#-- ------------------------------------------------------------------------------------------------------------ -->
public final class ${JAVA_MODEL_CLASS.beanSupplierImplSimpleClassName} extends BeanSupplier<${JAVA_MODEL_CLASS.javaSimpleClassName}> {

    <#-- -------------------------------------------------------------------------------------------------------- -->
    <#if JAVA_MODEL_CLASS.constructorInjection>
    <#assign INSTANCE_NAME>builder
    <#else>
    <#assign INSTANCE_NAME>bean
    
    <#-- -------------------------------------------------------------------------------------------------------- -->
    <#-- -------------------------------------------------------------------------------------------------------- -->
    <#-- -------------------------------------------------------------------------------------------------------- -->
    @Override
    public ${JAVA_MODEL_CLASS.javaSimpleClassName} get() {
        <#if JAVA_MODEL_CLASS.injectionPoints?has_content || JAVA_MODEL_CLASS.postConstructMethodPresent>
            <#if JAVA_MODEL_CLASS.constructorInjection>
        final Builder builder = new Builder();
            <#elseif JAVA_MODEL_CLASS.factoryMethodPresent>
                <#if JAVA_MODEL_CLASS.factoryMethod.privateMethod>
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} bean = (${JAVA_MODEL_CLASS.javaSimpleClassName}) invoke(${JAVA_MODEL_CLASS.javaSimpleClassName}.class, "${JAVA_MODEL_CLASS.factoryMethod.methodName.simpleName}");
                <#else>
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} bean = ${JAVA_MODEL_CLASS.javaSimpleClassName}.${JAVA_MODEL_CLASS.factoryMethod.methodName.simpleName}();
                
            <#else>
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} bean = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
        
        <@generateInjectionsAndReturnBean />
        <#-- ---------------------------------------------------------------------------------------------------- -->
        <#else>
            <#if JAVA_MODEL_CLASS.factoryMethodPresent>
                <#if JAVA_MODEL_CLASS.factoryMethod.privateMethod>
        return (${JAVA_MODEL_CLASS.javaSimpleClassName}) invoke(${JAVA_MODEL_CLASS.javaSimpleClassName}.class, "${JAVA_MODEL_CLASS.factoryMethod.methodName.simpleName}");
                <#else>
        return ${JAVA_MODEL_CLASS.javaSimpleClassName}.${JAVA_MODEL_CLASS.factoryMethod.methodName.simpleName}();
                
            <#else>
        return new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
            
        
    }
    <#if JAVA_MODEL_CLASS.factoryClass>

    public static final class ${JAVA_MODEL_CLASS.beanFactoryTypeSupplierImplSimpleClassName} extends BeanSupplier<${JAVA_MODEL_CLASS.factoryTypeSimpleClassName}> {

        @Override
        public ${JAVA_MODEL_CLASS.factoryTypeSimpleClassName} get() {
            return getBean(${JAVA_MODEL_CLASS.javaSimpleClassName}.class).get();
        }

    }
    
    <#if JAVA_MODEL_CLASS.constructorInjection>

    private static final class Builder {

        <#list JAVA_MODEL_CLASS.injectionPoints as injectionPoint>
        private ${injectionPoint.modelField.fieldSimpleType} ${injectionPoint.modelField.fieldName};

        
        private ${JAVA_MODEL_CLASS.javaSimpleClassName} build() {
            return new ${JAVA_MODEL_CLASS.javaSimpleClassName}(<#list JAVA_MODEL_CLASS.injectionPoints as p>${p.modelField.fieldName}<#if p?has_next>, );
        }

    }
    
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro generateInjectionsAndReturnBean>
        <#list JAVA_MODEL_CLASS.injectionPoints as point>
        <@inject injectionPoint=point/>
        
        <#-- ------------------------------------------------------------------------------------------------ -->
        <#if JAVA_MODEL_CLASS.constructorInjection && JAVA_MODEL_CLASS.postConstructMethodPresent>
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} bean = builder.build();
        <#if JAVA_MODEL_CLASS.postConstructMethod.privateMethod>
        invoke(bean, "${JAVA_MODEL_CLASS.postConstructMethod.methodName.simpleName}");
        <#else>
        bean.${JAVA_MODEL_CLASS.postConstructMethod.methodName.simpleName}();
        
        return bean;
        <#-- ------------------------------------------------------------------------------------------------ -->
        <#elseif JAVA_MODEL_CLASS.postConstructMethodPresent>
        <#if JAVA_MODEL_CLASS.postConstructMethod.privateMethod>
        invoke(bean, "${JAVA_MODEL_CLASS.postConstructMethod.methodName.simpleName}");
        <#else>
        bean.${JAVA_MODEL_CLASS.postConstructMethod.methodName.simpleName}();
        
        return bean;
        <#-- ------------------------------------------------------------------------------------------------ -->
        <#elseif JAVA_MODEL_CLASS.constructorInjection>
        return builder.build();
        <#else>
        return bean;
        

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro inject injectionPoint>
    <#if injectionPoint.type.name() == 'REPOSITORY'>
        <@injectRepository injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'CONFIG'>
        <@injectConfig injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'MONGO_CLIENT'>
        <@injectMongoClient injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'POSTGRE_SQL_CONNECTION_FACTORY'>
        <@injectConnectionFactory injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'POSTGRE_SQL_CONNECTION_POOL'>
        <@injectConnectionPool injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'REST_CLIENT'>
        <@injectRestClient injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'BEAN'>
        <@injectBean injectionPoint=injectionPoint/>
    <#elseif injectionPoint.type.name() == 'MULTI_BINDER'>
        <@injectMultiBinder injectionPoint=injectionPoint/>
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectRepository injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getRepository(${injectionPoint.modelField.fieldSimpleType}.class));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getRepository(${injectionPoint.modelField.fieldSimpleType}.class));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getRepository(${injectionPoint.modelField.fieldSimpleType}.class);
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectConfig injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getConfig("${injectionPoint.modelField.modelName}", ${injectionPoint.modelField.fieldSimpleType}.class));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getConfig("${injectionPoint.modelField.modelName}", ${injectionPoint.modelField.fieldSimpleType}.class));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getConfig("${injectionPoint.modelField.modelName}", ${injectionPoint.modelField.fieldSimpleType}.class);
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectMongoClient injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getMongoClient("${injectionPoint.modelField.modelName}"));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getMongoClient("${injectionPoint.modelField.modelName}"));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getMongoClient("${injectionPoint.modelField.modelName}");
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectConnectionFactory injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getPostgreSQLConnectionFactory("${injectionPoint.modelField.modelName}"));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getPostgreSQLConnectionFactory("${injectionPoint.modelField.modelName}"));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getPostgreSQLConnectionFactory("${injectionPoint.modelField.modelName}");
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectConnectionPool injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getPostgreSQLConnectionPool("${injectionPoint.modelField.modelName}"));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getPostgreSQLConnectionPool("${injectionPoint.modelField.modelName}"));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getPostgreSQLConnectionPool("${injectionPoint.modelField.modelName}");
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectRestClient injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getRestClient(${injectionPoint.modelField.fieldSimpleType}.class));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getRestClient(${injectionPoint.modelField.fieldSimpleType}.class));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getRestClient(${injectionPoint.modelField.fieldSimpleType}.class);
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectBean injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
      <#if injectionPoint.required>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getRequiredBean(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                <#list injectionPoint.qualifierRules as cdi>
                ${cdi.javaCodeFragment}<#if cdi?has_next>,
                
        ));
      <#else>
        getOptionalBean(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                <#list injectionPoint.qualifierRules as cdi>
                ${cdi.javaCodeFragment}<#if cdi?has_next>,
                
        ).ifPresent(${INSTANCE_NAME}::${injectionPoint.injectionMethodSimpleName});
      
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
      <#if injectionPoint.required>
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getRequiredBean(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                <#list injectionPoint.qualifierRules as cdi>
                ${cdi.javaCodeFragment}<#if cdi?has_next>,
                
        ));
      <#else>
        getOptionalBean(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                <#list injectionPoint.qualifierRules as cdi>
                ${cdi.javaCodeFragment}<#if cdi?has_next>,
                
        ).ifPresent(value -> setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", value));
      
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
      <#if injectionPoint.required>
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getRequiredBean(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                <#list injectionPoint.qualifierRules as cdi>
                ${cdi.javaCodeFragment}<#if cdi?has_next>,
                
        );
      <#else>
        getOptionalBean(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                <#list injectionPoint.qualifierRules as cdi>
                ${cdi.javaCodeFragment}<#if cdi?has_next>,
                
        ).ifPresent(value -> ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = value);
      
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro injectMultiBinder injectionPoint>
    <#if injectionPoint.injectionMethodPresent>
        ${INSTANCE_NAME}.${injectionPoint.injectionMethodSimpleName}(getBeansByType(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                ${injectionPoint.modelField.genericListItemSimpleClassName}.class
        ));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(${INSTANCE_NAME}, "${injectionPoint.modelField.fieldName}", getBeansByType(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                ${injectionPoint.modelField.genericListItemSimpleClassName}.class
        ));
    <#elseif injectionPoint.modelField.modelWriteAccessorType.name() == "DIRECT">
        ${INSTANCE_NAME}.${injectionPoint.modelField.fieldName} = getBeansByType(
                ${INSTANCE_NAME},
                "${injectionPoint.modelField.fieldName}",
                ${injectionPoint.modelField.genericListItemSimpleClassName}.class
        );
    

<#-- -------------------------------------------------------------------------------------------------------- -->




© 2015 - 2025 Weber Informatics LLC | Privacy Policy