MentaContainer

Compare Revisions

Regard whitespace Rev 21 → Rev 22

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
10,6 → 10,7
import org.mentacontainer.Container;
import org.mentacontainer.Dependency;
import org.mentacontainer.util.InjectionUtils;
import org.mentacontainer.util.InjectionUtils.Provider;
 
/**
* The implementation of the IoC container.
149,8 → 150,22
 
public Container populate(Object bean) throws Exception {
 
InjectionUtils.getObject(bean, this, false, null, true, false, true);
Provider p = new Provider() {
 
public Object get(String key) {
return MentaContainer.this.get(key);
}
public boolean contains(String key) {
return MentaContainer.this.contains(key);
}
};
 
InjectionUtils.getObject(bean, p, false, null, true, false, true);
 
return this;
}
 
/trunk/src/main/java/org/mentacontainer/util/InjectionUtils.java
9,17 → 9,13
import java.util.List;
import java.util.Map;
 
import org.mentacontainer.Container;
 
public class InjectionUtils {
 
/**
* The character used to separate the prefix from the value name when you
* are using the getObject method with a prefix. You can change the value of
* this prefix if you want to by changing this static variable.
* The character used to separate the prefix from the value name when you are using the getObject method with a prefix. You can change the value of this prefix if you want to
* by changing this static variable.
*
* Ex: getObject(User.class, "user") will get all values that begin with
* "user.".
* Ex: getObject(User.class, "user") will get all values that begin with "user.".
*/
public static char PREFIX_SEPARATOR = '.';
 
39,7 → 35,7
 
String name = m.getName();
 
Class[] types = m.getParameterTypes();
Class<?>[] types = m.getParameterTypes();
 
if (name.startsWith("set") && name.length() > 3 && types.length == 1) {
 
112,17 → 108,11
 
Method m = (Method) obj;
 
Class[] types = m.getParameterTypes();
Class<?>[] types = m.getParameterTypes();
 
Class type = field.getType();
Class<?> type = field.getType();
 
if (type.isAssignableFrom(types[0])) continue; // don't
// choose a
// field when
// we already
// have
// a
// method...
if (type.isAssignableFrom(types[0])) continue;
 
} else if (obj instanceof List) {
 
136,9 → 126,9
 
Method m = iter.next();
 
Class[] types = m.getParameterTypes();
Class<?>[] types = m.getParameterTypes();
 
Class type = field.getType();
Class<?> type = field.getType();
 
if (type.isAssignableFrom(types[0])) {
 
148,10 → 138,7
}
}
 
if (found) continue; // don't choose a field when we already
// have
// a method...
 
if (found) continue;
}
}
 
160,7 → 147,7
}
}
 
public static boolean checkPrimitives(Class target, Class<? extends Object> source) {
public static boolean checkPrimitives(Class<? extends Object> target, Class<? extends Object> source) {
 
if (target.equals(int.class) && source.equals(Integer.class)) return true;
 
182,12 → 169,12
 
}
 
public static Object tryToConvert(Object source, Class targetType) {
public static Object tryToConvert(Object source, Class<? extends Object> targetType) {
 
return tryToConvert(source, targetType, false);
}
 
public static Object tryToConvert(Object source, Class targetType, boolean tryNumber) {
public static Object tryToConvert(Object source, Class<?> targetType, boolean tryNumber) {
 
String value = null;
 
280,8 → 267,10
 
try {
 
newValue = Enum.valueOf(targetType, value);
Class k = (Class) targetType; // not sure how to avoid this raw type!
 
newValue = Enum.valueOf(k, value);
 
} catch (Exception e) {
 
return null;
307,7 → 296,7
return null;
}
 
public static Class getPrimitiveFrom(Object w) {
public static Class<? extends Object> getPrimitiveFrom(Object w) {
if (w instanceof Boolean) {
return Boolean.TYPE;
} else if (w instanceof Byte) {
328,7 → 317,7
return null;
}
 
public static Class getPrimitiveFrom(Class klass) {
public static Class<? extends Object> getPrimitiveFrom(Class<? extends Object> klass) {
 
String s = klass.getName();
 
402,7 → 391,7
return null;
}
 
public static Method findMethodToInject(Class<? extends Object> target, String name, Class source) {
public static Method findMethodToInject(Class<? extends Object> target, String name, Class<? extends Object> source) {
 
StringBuffer sb = new StringBuffer(128);
 
423,7 → 412,7
 
if (m == null) {
 
Class primitive = getPrimitiveFrom(source);
Class<? extends Object> primitive = getPrimitiveFrom(source);
 
if (primitive != null) {
 
533,43 → 522,6
}
}
 
/*
* This method takes setUsername and returns username.
*
* If we have a prefix, then it returns prefix.username.
*/
private static String adjustName(String name, String prefix) {
 
StringBuilder sb;
 
if (name.length() >= 4 && (name.startsWith("get") || name.startsWith("set"))) {
 
sb = new StringBuilder(name.length() - 3);
sb.append(name.substring(3, 4).toLowerCase());
if (name.length() > 4) sb.append(name.substring(4, name.length()));
 
} else if (name.length() >= 3 && name.startsWith("is")) {
 
sb = new StringBuilder(name.length() - 2);
sb.append(name.substring(2, 3).toLowerCase());
if (name.length() > 3) sb.append(name.substring(3, name.length()));
 
} else {
 
throw new IllegalArgumentException("Cannot adjust method: " + name);
}
 
if (prefix != null) {
 
StringBuffer sb2 = new StringBuffer(128);
 
return sb2.append(prefix).append(PREFIX_SEPARATOR).append(sb.toString()).toString();
 
}
 
return sb.toString();
}
 
/**
* Extract the value of a property of a bean!
*
577,8 → 529,7
* the target bean
* @param nameProperty
* the property name
* @return they value as String. The method toString is always called to
* every property!
* @return they value as String. The method toString is always called to every property!
* @throws Exception
*/
public static String getProperty(Object bean, String nameProperty) throws Exception {
635,9 → 586,16
}
}
 
public static void getObject(Object target, Container container, boolean tryField, String prefix, boolean tryToConvert, boolean convertBoolean,
boolean allowRecursion) throws Exception {
public static interface Provider {
 
public Object get(String key);
public boolean contains(String key);
}
 
public static void getObject(Object target, Provider provider, boolean tryField, String prefix, boolean tryToConvert, boolean convertBoolean, boolean allowRecursion)
throws Exception {
 
Class<? extends Object> targetClass = target.getClass();
 
Map<String, Object> setters, fields;
683,9 → 641,9
 
String var = iter.next();
 
boolean hasValue = container.contains(var);
boolean hasValue = provider.contains(var);
 
Object value = container.contains(var);
Object value = provider.get(var);
 
boolean tryingToConvertBoolean = false;
 
708,13 → 666,13
 
// we may have a list of overloaded methods...
 
List list = null;
List<Method> list = null;
 
Method m = null;
 
if (obj instanceof List) {
 
list = (List) obj;
list = (List<Method>) obj;
 
} else {
 
735,7 → 693,7
 
Object param = type.newInstance();
 
InjectionUtils.getObject(param, container, true, prefix, true, true, false); // no
InjectionUtils.getObject(param, provider, true, prefix, true, true, false); // no
// recursion...
 
inject(m, target, param, false, false);
744,13 → 702,13
 
} else {
 
Iterator it = list.iterator();
Iterator<Method> it = list.iterator();
 
boolean injected = false;
 
while (it.hasNext()) {
 
m = (Method) it.next();
m = it.next();
 
if (inject(m, target, value, tryToConvert, tryingToConvertBoolean)) {
 
777,7 → 735,7
 
Object param = type.newInstance();
 
InjectionUtils.getObject(param, container, true, prefix, true, true, false); // no
InjectionUtils.getObject(param, provider, true, prefix, true, true, false); // no
// recursion...
 
if (inject(m, target, param, false, false)) {
798,9 → 756,9
 
String var = iter.next();
 
boolean hasValue = container.contains(var);
boolean hasValue = provider.contains(var);
 
Object value = container.get(var);
Object value = provider.get(var);
 
Field f = (Field) fields.get(var);