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

io.github.butkoprojects.bots.preprocess.factory.DefaultControllerFactory Maven / Gradle / Ivy

package io.github.butkoprojects.bots.preprocess.factory;

import io.github.butkoprojects.bots.preprocess.container.BotMethodContainer;
import io.github.butkoprojects.bots.preprocess.controller.builder.ControllerBuilder;
import io.github.butkoprojects.bots.preprocess.annotation.processor.AnnotationProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.ParameterizedType;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

@Component
public class DefaultControllerFactory implements ControllerFactory {

    @Autowired
    private BotMethodContainer container;

    @Autowired
    private ControllerBuilder builder;

    @Autowired
    private List processors;

    @Override
    public void generateController( Object bean, Method method ) {
        ControllerBuilder newBuilder = builder.instance();
        newBuilder.setMethod( method ).setBean( bean );

        processors.forEach( annotationProcessor -> {
            Class requiredAnnotation = getRequiredAnnotation( annotationProcessor );
            if ( method.isAnnotationPresent( requiredAnnotation ) ) {
                annotationProcessor.process(
                        method.getAnnotation( requiredAnnotation ),
                        newBuilder
                );
            }
        });

        container.addBotController( newBuilder.getPath(), newBuilder.getControllerType(), newBuilder.build() );
    }

    private Class getRequiredAnnotation( final AnnotationProcessor processor ) {
        Class result = ( Class )
                ((ParameterizedType) processor.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy