MentaContainer

Compare Revisions

Ignore whitespace Rev 101 → Rev 102

/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
3,7 → 3,6
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
 
public class ClassFactoryTest {
270,4 → 269,22
Assert.assertEquals(new Long(20), o.l1);
Assert.assertEquals(20, o.l2);
}
private static class TestObj22 {
public TestObj22() { }
}
@Test
public void testOnlyOneZeroArgConstructor() {
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj22.class);
TestObj22 o = f.getInstance();
Assert.assertNotNull(o);
}
}
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
26,8 → 26,6
 
private Map<String, Factory> factoriesByName = new Hashtable<String, Factory>();
private Map<Class<? extends Object>, List<String>> factoryKeysByType = new Hashtable<Class<? extends Object>, List<String>>();
private Map<String, Scope> scopes = new Hashtable<String, Scope>();
private Map<String, Object> singletonsCache = new Hashtable<String, Object>();
38,6 → 36,8
private Set<ConstructorDependency> constructorDependencies = Collections.synchronizedSet(new HashSet<ConstructorDependency>());
private Set<ConstructorDependency> forConstructMethod = Collections.synchronizedSet(new HashSet<ConstructorDependency>());
@Override
public Class<? extends Object> getType(String key) {
361,38 → 361,19
}
}
private void mapeTypeToKeys(String key, Factory factory) {
Class<? extends Object> type = factory.getType();
synchronized(factoryKeysByType) {
List<String> list = factoryKeysByType.get(type);
if (list == null) {
list = new LinkedList<String>();
factoryKeysByType.put(type, list);
}
list.add(key);
}
}
 
@Override
public Factory ioc(String key, Factory factory, Scope scope) {
factoriesByName.put(key, factory);
mapeTypeToKeys(key, factory);
singletonsCache.remove(key); // just in case we are overriding a previous singleton bean...
threadLocalsCache.remove(key); // just in case we are overriding a previous thread local...
scopes.put(key, scope);
forConstructMethod.add(new ConstructorDependency(key, factory.getType()));
return factory;
}
467,6 → 448,14
return constructorDependencies;
}
@Override
public <T> T construct(Class<? extends Object> klass) {
ClassFactory f = new ClassFactory(this, klass, forConstructMethod);
return (T) f.getInstance();
}
 
@Override
public Container populate(Object bean) {
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
36,13 → 36,23
private boolean useZeroArgumentsConstructor = false;
private final Set<ConstructorDependency> constructorDependencies;
public ClassFactory(MentaContainer container, Class<? extends Object> klass) {
 
this(container, klass, null);
}
ClassFactory(MentaContainer container, Class<? extends Object> klass, Set<ConstructorDependency> constructorDependencies) {
this.container = container;
this.klass = klass;
this.constructorDependencies = constructorDependencies;
}
 
@Override
public ConfigurableFactory addPropertyValue(String name, Object value) {
401,7 → 411,9
LinkedList<Class<? extends Object>> newInitTypes = new LinkedList<Class<? extends Object>>();
LinkedList<Object> newInitValues = new LinkedList<Object>();
Set<ConstructorDependency> dependencies = new HashSet<ConstructorDependency>(container.getConstructorDependencies());
Set<ConstructorDependency> constructorDependencies = this.constructorDependencies != null ? this.constructorDependencies : container.getConstructorDependencies();
Set<ConstructorDependency> dependencies = new HashSet<ConstructorDependency>(constructorDependencies);
Class<? extends Object>[] constructorParams = c.getParameterTypes();
/trunk/src/main/java/org/mentacontainer/Container.java
105,6 → 105,8
*/
public Container populate(Object bean);
public <T> T construct(Class<? extends Object> klass);
/**
* Check whether the container currently has a value for this key. For example,
* if it is a singleton AND someone has requested it, the container will have it cached.