Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package ch.dkitc.ridioc;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.*;
import static ch.dkitc.ridioc.DIParamUtils.checkParams;
import static ch.dkitc.ridioc.DITypeUtils.checkType;
import static ch.dkitc.ridioc.DITypeUtils.hasDefaultConstructor;
public class DIObjectFactoryHelper {
private final Map, Object> instanceCache = new HashMap, Object>();
private final DIReflectionsCache reflectionsCache;
private final Map, Class>> wrappedPrimitiveTypeMap;
private final DIStringLiteralStore stringLiteralStore;
public DIObjectFactoryHelper(String packagePrefix, Map, Class>> wrappedPrimitiveTypeMap) {
this.reflectionsCache = new DIReflectionsCache(packagePrefix);
this.wrappedPrimitiveTypeMap = wrappedPrimitiveTypeMap;
this.stringLiteralStore = new DIStringLiteralStore(wrappedPrimitiveTypeMap);
}
public T newInstance(Class type, Object... params) {
checkType(type);
checkParams(params);
Class extends T> potentialImplType = getSinglePotentialImplType(type);
return constructOneImplementation(type, potentialImplType, params);
}
/**
* Looks up or creates an instance of the given type.
*
Tries to look up the give type in the instance cache.
* If the instance cache does not yet contain an instance of the given type, the method
* tries to find exactly ONE implementation of the given type. The method creates a new instance using
* dependency injection.
*
* @param type a type to look up or create an instance for. Must not be {@code null}
* @return the looked up instance or a newly created instance of the given type
* @throws IllegalArgumentException
*/
public Object instance(Class> type) throws IllegalArgumentException {
checkType(type);
// already in cache?
if (instanceCache.containsKey(type)) {
// yes - return it
return instanceCache.get(type);
}
Class> potentialImplType = getSinglePotentialImplType(type);
Object instance = constructOneImplementation(type, potentialImplType);
// cache it!
instanceCache.put(type, instance);
return instance;
}
public String registerStringLiteral(String key, String singleValue) {
return stringLiteralStore.putSingleValue(key, singleValue);
}
public String[] registerStringLiteralArray(String key, String[] arrayValue) {
return stringLiteralStore.putArrayValue(key, arrayValue);
}
public Object registerInstance(Class> type, Object instance) {
Class> realType;
if (type.isPrimitive()) {
realType = getWrappedPrimitiveType(type);
} else {
realType = type;
}
return instanceCache.put(realType, instance);
}
private T constructOneImplementation(Class type, Class> potentialImplType, Object... params) {
List diConstructors = new DIConstructors(potentialImplType, wrappedPrimitiveTypeMap).findMatchingConstructorsByParams(params);
List exceptions = new ArrayList();
for (DIConstructor diConstructor : diConstructors) {
DIConstructorParams constructorParams = createConstructorParams(diConstructor);
DIInstanceMethodParams instanceMethodParams = new DIInstanceMethodParams(params);
try {
return newInstance(diConstructor, constructorParams, instanceMethodParams);
} catch (Exception ex) {
exceptions.add(ex);
}
}
throw new DIAggregateException("Cound not instantiate implementation for type '" + type + "'", exceptions);
}
private Object createArrayInitArg(Class> constructorParamType) {
List