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

net.sf.andromedaioc.bean.FactoryMethodBeanWrapperCreator Maven / Gradle / Ivy

The newest version!
package net.sf.andromedaioc.bean;

import net.sf.andromedaioc.bean.instantiation.FactoryMethodInstantiatorWrapper;
import net.sf.andromedaioc.bean.instantiation.InstantiatorWrapper;
import net.sf.andromedaioc.bean.wrapper.BeanWrapper;
import net.sf.andromedaioc.exception.BeanNotFoundException;
import net.sf.andromedaioc.exception.NoFactoryMethodFoundException;
import net.sf.andromedaioc.model.beans.BeanModel;
import net.sf.andromedaioc.model.beans.ReferenceKey;
import net.sf.andromedaioc.resource.ResourceProvider;

import java.lang.reflect.Method;
import java.util.*;

public class FactoryMethodBeanWrapperCreator extends AbstractBeanWrapperCreator {

    public FactoryMethodBeanWrapperCreator(BeanModel beanModel, Map beans, ResourceProvider resourceProvider) {
        super(beanModel, beans, resourceProvider);
    }

    @Override
    protected List getInstantiatorWrappers() {
        List instantiatorWrappers = new LinkedList();
        Method[] methods = getBeanModel().getBeanClass().getMethods();
        ReferenceKey factoryBeanReference = getBeanModel().getFactoryBean();
        BeanWrapper factoryBean = null;
        if(factoryBeanReference != null) {
            factoryBean = getBeans().get(factoryBeanReference);
            if(factoryBean == null) {
                throw new BeanNotFoundException(String.format("Bean with id = %s not found", factoryBeanReference.getId()));
            }
        }
        for(Method method : methods) {
            if(method.getName().equals(getBeanModel().getFactoryMethod())) {
                instantiatorWrappers.add(new FactoryMethodInstantiatorWrapper(factoryBean, method));
            }
        }
        return instantiatorWrappers;
    }

    @Override
    protected Exception getNoInstantiatorFoundException() {
        return new NoFactoryMethodFoundException(String.format("No factory method %s found for bean with id = %s", getBeanModel().getFactoryMethod(), getBeanModel().getKey().getId()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy