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

it.espr.injector.BeanFactory Maven / Gradle / Ivy

There is a newer version: 0.2.6
Show newest version
package it.espr.injector;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.espr.injector.exception.BeanCreationExpection;

public class BeanFactory {

	private static final Logger log = LoggerFactory.getLogger(BeanFactory.class);

	private Map cache = new HashMap<>();

	@SuppressWarnings("unchecked")
	public  Type create(Bean bean) throws BeanCreationExpection {
		if (bean.instance != null) {
			this.handleConfiguratedCollections(bean.instance);
			return bean.instance;
		}

		Type instance = null;
		if (bean.singleton) {
			instance = (Type) this.cache.get(bean.key);
		}

		if (instance == null) {
			try {
				if (bean.constructorParameters == null || bean.constructorParameters.size() == 0) {
					instance = bean.constructor.newInstance();
				} else {
					Object[] constructorParameterInstances = new Object[bean.constructorParameters.size()];
					for (int i = 0; i < bean.constructorParameters.size(); i++) {
						constructorParameterInstances[i] = this.create(bean.constructorParameters.get(i));
					}
					instance = bean.constructor.newInstance(constructorParameterInstances);
				}

				// instantiate fields
				if (bean.fields != null) {
					for (Entry> entry : bean.fields.entrySet()) {
						Field f = entry.getKey();
						Bean b = entry.getValue();

						boolean resetAccessible = false;
						if (!f.isAccessible()) {
							f.setAccessible(true);
							resetAccessible = true;
						}
						f.set(instance, this.create(b));

						if (resetAccessible) {
							f.setAccessible(false);
						}
					}
				}

				if (bean.singleton) {
					this.cache.put(bean.key, instance);
				}
			} catch (Exception e) {
				log.error("Problem when creating bean {}", bean, e);
				throw new BeanCreationExpection("Problem when creating bean '" + bean + "'", e);
			}
		}

		return instance;
	}

	private  void handleConfiguratedCollections(Type configuredBean) throws BeanCreationExpection {
		if (configuredBean instanceof List) {
			this.handleConfiguredList(configuredBean);
		} else if (configuredBean instanceof Map) {
			this.handleConfiguredMap(configuredBean);
		}
	}

	@SuppressWarnings("unchecked")
	private  void handleConfiguredList(Type configuredList) throws BeanCreationExpection {
		@SuppressWarnings("rawtypes")
		List configuredBeanList = (List) configuredList;
		for (int i = 0; i < configuredBeanList.size(); i++) {
			if (configuredBeanList.get(i) instanceof Bean) {
				configuredBeanList.set(i, this.create((Bean) configuredBeanList.get(i)));
			}
		}
	}

	@SuppressWarnings("unchecked")
	private  void handleConfiguredMap(Type configuredMap) throws BeanCreationExpection {
		@SuppressWarnings("rawtypes")
		Map configuredBeanMap = (Map) configuredMap;
		for (Entry entry : configuredBeanMap.entrySet()) {
			if (entry.getValue() instanceof Bean) {
				entry.setValue(this.create((Bean) entry.getValue()));
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy