}
}
}
private static Object createInstance(Class<?> clazz, boolean inject, boolean singleton, Object... args) {
Factory factory = null;
Singleton entity = null;
synchronized(s_factories) {
if (singleton) {
entity = s_singletons.get(clazz);
if (entity != null) {
s_logger.debug("Found singleton instantiation for " + clazz.toString());
return entity.singleton;
}
}
InjectInfo info = s_factories.get(clazz);
if (info == null) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallbackFilter(s_callbackFilter);
enhancer.setCallbacks(s_callbacks);
factory = (Factory)enhancer.create();
info = new InjectInfo(enhancer, factory);
s_factories.put(clazz, info);
} else {
factory = info.factory;
}
}
Class<?>[] argTypes = null;
if (args != null && args.length > 0) {
Constructor<?>[] constructors = clazz.getConstructors();
for (Constructor<?> constructor : constructors) {
Class<?>[] paramTypes = constructor.getParameterTypes();
if (paramTypes.length == args.length) {
boolean found = true;
for (int i = 0; i < paramTypes.length; i++) {
if (!paramTypes[i].isAssignableFrom(args[i].getClass()) && !paramTypes[i].isPrimitive()) {
found = false;
break;
}
}
if (found) {
argTypes = paramTypes;
break;
}
}
}
if (argTypes == null) {
throw new CloudRuntimeException("Unable to find constructor to match parameters given: " + clazz.getName());
}
entity = new Singleton(factory.newInstance(argTypes, args, s_callbacks));
} else {
entity = new Singleton(factory.newInstance(s_callbacks));
}
if (inject) {
inject(clazz, entity.singleton);
entity.state = Singleton.State.Injected;