MentaContainer

Compare Revisions

Ignore whitespace Rev 137 → Rev 139

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
41,8 → 41,10
@Override
public Class<? extends Object> getType(Object key) {
Factory factory = factoriesByName.get(key.toString());
String k = InjectionUtils.getKeyName(key);
Factory factory = factoriesByName.get(k);
if (factory == null) return null;
return factory.getType();
115,7 → 117,7
@Override
public <T> T clear(Object key) {
String keyString = key.toString();
String keyString = InjectionUtils.getKeyName(key);
if (!factoriesByName.containsKey(keyString)) return null;
197,7 → 199,7
@Override
public <T> T get(Object key) {
String keyString = key.toString();
String keyString = InjectionUtils.getKeyName(key);
 
if (!factoriesByName.containsKey(keyString)) return null;
 
368,7 → 370,7
@Override
public Factory ioc(Object key, Factory factory, Scope scope) {
String keyString = key.toString();
String keyString = InjectionUtils.getKeyName(key);
factoriesByName.put(keyString, factory);
417,7 → 419,7
// autowire by constructor and setter...
String s = sourceFromContainer.toString();
String s = InjectionUtils.getKeyName(sourceFromContainer);
autowireBySetter(s);
429,7 → 431,7
// autowire by constructor and setter...
String s = sourceFromContainer.toString();
String s = InjectionUtils.getKeyName(sourceFromContainer);
autowireBySetter(beanProperty, s);
504,7 → 506,7
@Override
public synchronized boolean check(Object obj) {
String key = obj.toString();
String key = InjectionUtils.getKeyName(obj);
if (!factoriesByName.containsKey(key)) return false;
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
13,6 → 13,7
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.util.FindConstructor;
import org.mentacontainer.util.FindMethod;
import org.mentacontainer.util.InjectionUtils;
 
/**
* The implementation of the Configurable Factory.
79,9 → 80,11
}
@Override
public ConfigurableFactory addPropertyDependency(String property, String key) {
public ConfigurableFactory addPropertyDependency(String property, Object key) {
return addPropertyValue(property, new DependencyKey(key));
String k = InjectionUtils.getKeyName(key);
return addPropertyValue(property, new DependencyKey(k));
}
@Override
91,9 → 94,11
}
@Override
public ConfigurableFactory addInitDependency(String key) {
public ConfigurableFactory addConstructorDependency(Object key) {
return addInitValue(new DependencyKey(key), container.getType(key));
String k = InjectionUtils.getKeyName(key);
return addInitValue(new DependencyKey(k), container.getType(k));
}
private ConfigurableFactory addInitValue(Object value, Class<? extends Object> type) {
/trunk/src/main/java/org/mentacontainer/ConfigurableFactory.java
45,7 → 45,7
* @param key The key used to get an instance from the container
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory addInitDependency(String key);
public ConfigurableFactory addConstructorDependency(Object key);
/**
* In case you want to force the use of a zero argument constructor and avoid any ambiguity when choosing the constructor to use.
85,5 → 85,5
* @param key The dependency name, in other words, the key used to get a bean from the container.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory addPropertyDependency(String property, String key);
public ConfigurableFactory addPropertyDependency(String property, Object key);
}
/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java
122,7 → 122,7
 
c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
 
c.ioc("accountDAO", JdbcAccountDAO.class).addInitDependency("connection");
c.ioc("accountDAO", JdbcAccountDAO.class).addConstructorDependency("connection");
 
AccountDAO accountDAO = c.get("accountDAO");
 
/trunk/src/main/java/org/mentacontainer/util/InjectionUtils.java
354,6 → 354,20
}
return null;
}
public static String getKeyName(Object obj) {
if (obj instanceof Class<?>) {
Class<?> k = (Class<?>) obj;
String s = k.getSimpleName();
StringBuilder sb = new StringBuilder(s.length());
sb.append(s.substring(0, 1).toLowerCase());
if (s.length() > 1) {
sb.append(s.substring(1));
}
return sb.toString();
}
return obj.toString();
}
 
public static Method findMethodToGet(Class<? extends Object> target, String name) {