MentaContainer

Compare Revisions

Ignore whitespace Rev 147 → Rev 148

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
460,7 → 460,7
}
@Override
public Class<? extends Object> getType() {
public Class<?> getType() {
return SomeObject.class;
}
/trunk/src/main/java/org/mentacontainer/impl/ConstructorDependency.java
10,9 → 10,9
private final String sourceFromContainer;
private final Class<? extends Object> sourceType;
private final Class<?> sourceType;
public ConstructorDependency(String sourceFromContainer, Class<? extends Object> sourceType) {
public ConstructorDependency(String sourceFromContainer, Class<?> sourceType) {
this.sourceFromContainer = sourceFromContainer;
24,7 → 24,7
return sourceFromContainer;
}
public Class<? extends Object> getSourceType() {
public Class<?> getSourceType() {
return sourceType;
}
/trunk/src/main/java/org/mentacontainer/impl/GenericFactory.java
12,7 → 12,7
private final Method method;
private final Class<? extends Object> type;
private final Class<?> type;
private Interceptor<E> interceptor = null;
68,7 → 68,7
}
@Override
public Class<? extends Object> getType() {
public Class<?> getType() {
return type;
}
}
/trunk/src/main/java/org/mentacontainer/impl/SetterDependency.java
17,11 → 17,11
private final String sourceFromContainer;
private final Class<? extends Object> sourceType;
private final Class<?> sourceType;
private Map<String, Method> cache = new HashMap<String, Method>();
public SetterDependency(String targetProperty, String sourceFromContainer, Class<? extends Object> sourceType) {
public SetterDependency(String targetProperty, String sourceFromContainer, Class<?> sourceType) {
this.targetProperty = targetProperty;
60,7 → 60,7
return true;
}
public Method check(Class<? extends Object> targetClass) {
public Method check(Class<?> targetClass) {
 
String className = targetClass.getName();
 
98,7 → 98,7
// check type...
Class<? extends Object>[] params = theMethod.getParameterTypes();
Class<?>[] params = theMethod.getParameterTypes();
if (params == null || params.length != 1) continue;
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
39,7 → 39,7
private Set<ConstructorDependency> forConstructMethod = Collections.synchronizedSet(new HashSet<ConstructorDependency>());
@Override
public Class<? extends Object> getType(Object key) {
public Class<?> getType(Object key) {
String k = InjectionUtils.getKeyName(key);
395,7 → 395,7
}
@Override
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass) {
public ConfigurableFactory ioc(Object key, Class<?> klass) {
ConfigurableFactory cc = new ClassFactory(this, klass);
405,7 → 405,7
}
@Override
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope) {
public ConfigurableFactory ioc(Object key, Class<?> klass, Scope scope) {
ConfigurableFactory cc = new ClassFactory(this, klass);
440,7 → 440,7
 
private void autowireBySetter(String targetProperty, String sourceFromContainer) {
Class<? extends Object> sourceType = getType(sourceFromContainer);
Class<?> sourceType = getType(sourceFromContainer);
 
SetterDependency d = new SetterDependency(targetProperty, sourceFromContainer, sourceType);
454,7 → 454,7
private void autowireByConstructor(String sourceFromContainer) {
Class<? extends Object> sourceType = getType(sourceFromContainer);
Class<?> sourceType = getType(sourceFromContainer);
ConstructorDependency d = new ConstructorDependency(sourceFromContainer, sourceType);
467,7 → 467,7
}
@Override
public <T> T construct(Class<? extends Object> klass) {
public <T> T construct(Class<?> klass) {
ClassFactory f = new ClassFactory(this, klass, forConstructMethod);
/trunk/src/main/java/org/mentacontainer/impl/SingletonFactory.java
6,7 → 6,7
private final Object instance;
private final Class<? extends Object> type;
private final Class<?> type;
public SingletonFactory(Object instance) {
23,7 → 23,7
}
@Override
public Class<? extends Object> getType() {
public Class<?> getType() {
return type;
}
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
24,15 → 24,15
private final MentaContainer container;
private final Class<? extends Object> klass;
private final Class<?> klass;
private Map<String, Object> props = null;
private List<Object> initValues = null;
private List<Class<? extends Object>> initTypes = null;
private List<Class<?>> initTypes = null;
private Constructor<? extends Object> constructor = null;
private Constructor<?> constructor = null;
private Map<String, Method> cache = null;
40,12 → 40,12
private final Set<ConstructorDependency> constructorDependencies;
public ClassFactory(MentaContainer container, Class<? extends Object> klass) {
public ClassFactory(MentaContainer container, Class<?> klass) {
 
this(container, klass, null);
}
ClassFactory(MentaContainer container, Class<? extends Object> klass, Set<ConstructorDependency> constructorDependencies) {
ClassFactory(MentaContainer container, Class<?> klass, Set<ConstructorDependency> constructorDependencies) {
this.container = container;
101,13 → 101,13
return addInitValue(new DependencyKey(k), container.getType(k));
}
private ConfigurableFactory addInitValue(Object value, Class<? extends Object> type) {
private ConfigurableFactory addInitValue(Object value, Class<?> type) {
if (initValues == null) {
initValues = new LinkedList<Object>();
initTypes = new LinkedList<Class<? extends Object>>();
initTypes = new LinkedList<Class<?>>();
}
initValues.add(value);
126,26 → 126,26
@Override
public ConfigurableFactory addInitPrimitive(Object value) {
Class<? extends Object> primitive = getPrimitiveFrom(value);
Class<?> primitive = getPrimitiveFrom(value);
if (primitive == null) throw new IllegalArgumentException("Value is not a primitive: " + value);
return addInitValue(value, primitive);
}
private List<Class<? extends Object>> convertToPrimitives(List<Class<? extends Object>> list) {
private List<Class<?>> convertToPrimitives(List<Class<?>> list) {
if (list == null) return null;
Iterator<Class<? extends Object>> iter = list.iterator();
Iterator<Class<?>> iter = list.iterator();
List<Class<? extends Object>> results = new LinkedList<Class<? extends Object>>();
List<Class<?>> results = new LinkedList<Class<?>>();
while(iter.hasNext()) {
Class<? extends Object> klass = iter.next();
Class<?> klass = iter.next();
Class<? extends Object> primitive = getPrimitiveFrom(klass);
Class<?> primitive = getPrimitiveFrom(klass);
if (primitive != null) {
160,11 → 160,11
return results;
}
private Class<? extends Object>[] getClasses(List<Class<? extends Object>> values) {
private Class<?>[] getClasses(List<Class<?>> values) {
if (values == null) return new Class[0];
Class<? extends Object>[] types = (Class<? extends Object>[]) new Class[values.size()];
Class<?>[] types = (Class<?>[]) new Class[values.size()];
return values.toArray(types);
}
225,7 → 225,7
// try primitive...
Class<? extends Object> primitive = getPrimitiveFrom(value);
Class<?> primitive = getPrimitiveFrom(value);
if (primitive != null) {
265,7 → 265,7
}
}
private static Class<? extends Object> getPrimitiveFrom(Object w) {
private static Class<?> getPrimitiveFrom(Object w) {
if (w instanceof Boolean) { return Boolean.TYPE; }
else if (w instanceof Byte) { return Byte.TYPE; }
else if (w instanceof Short) { return Short.TYPE; }
277,7 → 277,7
return null;
}
private static Class<? extends Object> getPrimitiveFrom(Class<? extends Object> klass) {
private static Class<?> getPrimitiveFrom(Class<?> klass) {
if (klass.equals(Boolean.class)) { return Boolean.TYPE; }
else if (klass.equals(Byte.class)) { return Byte.TYPE; }
else if (klass.equals(Short.class)) { return Short.TYPE; }
376,14 → 376,14
return (T) obj;
}
private static boolean betterIsAssignableFrom(Class<? extends Object> klass1, Class<? extends Object> klass2) {
private static boolean betterIsAssignableFrom(Class<?> klass1, Class<?> klass2) {
// with autoboxing both ways...
if (klass1.isAssignableFrom(klass2)) return true;
Class<? extends Object> k1 = klass1.isPrimitive() ? klass1 : getPrimitiveFrom(klass1);
Class<? extends Object> k2 = klass2.isPrimitive() ? klass2 : getPrimitiveFrom(klass2);
Class<?> k1 = klass1.isPrimitive() ? klass1 : getPrimitiveFrom(klass1);
Class<?> k2 = klass2.isPrimitive() ? klass2 : getPrimitiveFrom(klass2);
if (k1 == null || k2 == null) return false;
392,19 → 392,19
private void checkConstructorDependencies() {
Constructor<? extends Object>[] constructors = klass.getConstructors();
Constructor<?>[] constructors = klass.getConstructors();
for(Constructor<? extends Object> c : constructors) {
for(Constructor<?> c : constructors) {
LinkedList<Class<? extends Object>> providedInitTypes = null;
LinkedList<Class<?>> providedInitTypes = null;
if (initTypes != null) {
providedInitTypes = new LinkedList<Class<? extends Object>>(initTypes);
providedInitTypes = new LinkedList<Class<?>>(initTypes);
} else {
providedInitTypes = new LinkedList<Class<? extends Object>>();
providedInitTypes = new LinkedList<Class<?>>();
}
LinkedList<Object> providedInitValues = null;
418,22 → 418,22
providedInitValues = new LinkedList<Object>();
}
LinkedList<Class<? extends Object>> newInitTypes = new LinkedList<Class<? extends Object>>();
LinkedList<Class<?>> newInitTypes = new LinkedList<Class<?>>();
LinkedList<Object> newInitValues = new LinkedList<Object>();
Set<ConstructorDependency> constructorDependencies = this.constructorDependencies != null ? this.constructorDependencies : container.getConstructorDependencies();
Set<ConstructorDependency> dependencies = new HashSet<ConstructorDependency>(constructorDependencies);
Class<? extends Object>[] constructorParams = c.getParameterTypes();
Class<?>[] constructorParams = c.getParameterTypes();
if (constructorParams == null || constructorParams.length == 0) continue; // skip default constructor for now...
for(Class<? extends Object> constructorParam : constructorParams) {
for(Class<?> constructorParam : constructorParams) {
// first see if it was provided...
Class<? extends Object> provided = providedInitTypes.isEmpty() ? null : providedInitTypes.get(0);
Class<?> provided = providedInitTypes.isEmpty() ? null : providedInitTypes.get(0);
if (provided != null && constructorParam.isAssignableFrom(provided)) {
499,7 → 499,7
}
@Override
public Class<? extends Object> getType() {
public Class<?> getType() {
return klass;
}
}
/trunk/src/main/java/org/mentacontainer/Container.java
38,7 → 38,7
* @param key The factory
* @return The type returned by this factory
*/
public Class<? extends Object> getType(Object key);
public Class<?> getType(Object key);
/**
* Configure a bean to be returned with the given implementation when {@link #get(String)} is called.
50,7 → 50,7
* @return The factory created as a ConfigurableFactory. (Fluent API)
* @see Scope
*/
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope);
public ConfigurableFactory ioc(Object key, Class<?> klass, Scope scope);
/**
* Same as {@link #ioc(String, Class, Scope)} except that it assumes
118,7 → 118,7
* @param klass The class that should be instantiated.
* @return An instantiated bean.
*/
public <T> T construct(Class<? extends Object> klass);
public <T> T construct(Class<?> klass);
/**
* Check whether the container currently has a value for this key. For example,
/trunk/src/main/java/org/mentacontainer/Factory.java
20,5 → 20,5
*
* @return the type of objects returned by this factory.
*/
public Class<? extends Object> getType();
public Class<?> getType();
}
/trunk/src/main/java/org/mentacontainer/util/FindMethod.java
19,9 → 19,9
* @param name Name of method to find
* @param parameterTypes Parameter types to search for
*/
public static Method getMethod(Class<? extends Object> source,
public static Method getMethod(Class<?> source,
String name,
Class<? extends Object>[] parameterTypes)
Class<?>[] parameterTypes)
throws NoSuchMethodException {
return internalFind(source.getMethods(),
name,
35,9 → 35,9
* @param name Name of method to find
* @param parameterTypes Parameter types to search for
*/
public static Method getDeclaredMethod(Class<? extends Object> source,
public static Method getDeclaredMethod(Class<?> source,
String name,
Class<? extends Object>[] parameterTypes)
Class<?>[] parameterTypes)
throws NoSuchMethodException {
return internalFind(source.getDeclaredMethods(),
name,
49,7 → 49,7
*/
private static Method internalFind(Method[] toTest,
String name,
Class<? extends Object>[] parameterTypes)
Class<?>[] parameterTypes)
throws NoSuchMethodException {
int l = parameterTypes.length;
 
61,7 → 61,7
if (!toTest[i].getName().equals(name))
continue;
// Check the parameters match
Class<? extends Object>[] params = toTest[i].getParameterTypes();
Class<?>[] params = toTest[i].getParameterTypes();
 
if (params.length != l)
continue;
102,16 → 102,16
int j;
// In terms of the JLS, current is T
Method current = applicableMethods.get(i);
Class<? extends Object>[] currentParams = current.getParameterTypes();
Class<? extends Object> currentDeclarer = current.getDeclaringClass();
Class<?>[] currentParams = current.getParameterTypes();
Class<?> currentDeclarer = current.getDeclaringClass();
 
for (j = 0; j < size; j++) {
if (i == j)
continue;
// In terms of the JLS, test is U
Method test = applicableMethods.get(j);
Class<? extends Object>[] testParams = test.getParameterTypes();
Class<? extends Object> testDeclarer = test.getDeclaringClass();
Class<?>[] testParams = test.getParameterTypes();
Class<?> testDeclarer = test.getDeclaringClass();
 
// Check if T is a subclass of U, breaking if not
if (!testDeclarer.isAssignableFrom(currentDeclarer))
/trunk/src/main/java/org/mentacontainer/util/InjectionUtils.java
19,11 → 19,11
*/
public static char PREFIX_SEPARATOR = '.';
 
private static Map<Class<? extends Object>, Map<String, Object>> settersMaps = new HashMap<Class<? extends Object>, Map<String, Object>>();
private static Map<Class<?>, Map<String, Object>> settersMaps = new HashMap<Class<?>, Map<String, Object>>();
 
private static Map<Class<? extends Object>, Map<String, Object>> fieldsMaps = new HashMap<Class<? extends Object>, Map<String, Object>>();
private static Map<Class<?>, Map<String, Object>> fieldsMaps = new HashMap<Class<?>, Map<String, Object>>();
 
public static void prepareForInjection(Class<? extends Object> klass, Map<String, Object> setters, Map<String, Object> fields) {
public static void prepareForInjection(Class<?> klass, Map<String, Object> setters, Map<String, Object> fields) {
 
StringBuffer sb = new StringBuffer(32);
 
147,7 → 147,7
}
}
 
public static boolean checkPrimitives(Class<? extends Object> target, Class<? extends Object> source) {
public static boolean checkPrimitives(Class<?> target, Class<?> source) {
 
if (target.equals(int.class) && source.equals(Integer.class)) return true;
 
169,7 → 169,7
 
}
 
public static Object tryToConvert(Object source, Class<? extends Object> targetType) {
public static Object tryToConvert(Object source, Class<?> targetType) {
 
return tryToConvert(source, targetType, false);
}
282,7 → 282,7
 
}
 
public static Object shouldConvertToNull(Object value, Class<? extends Object> targetType) {
public static Object shouldConvertToNull(Object value, Class<?> targetType) {
 
if (targetType.equals(String.class)) {
 
296,7 → 296,7
return null;
}
 
public static Class<? extends Object> getPrimitiveFrom(Object w) {
public static Class<?> getPrimitiveFrom(Object w) {
if (w instanceof Boolean) {
return Boolean.TYPE;
} else if (w instanceof Byte) {
317,7 → 317,7
return null;
}
 
public static Class<? extends Object> getPrimitiveFrom(Class<? extends Object> klass) {
public static Class<?> getPrimitiveFrom(Class<?> klass) {
 
String s = klass.getName();
 
345,7 → 345,7
return getField(target.getClass(), name);
}
 
public static Field getField(Class<? extends Object> target, String name) {
public static Field getField(Class<?> target, String name) {
Field fields[] = target.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (name.equals(fields[i].getName())) {
369,7 → 369,7
return obj.toString();
}
 
public static Method findMethodToGet(Class<? extends Object> target, String name) {
public static Method findMethodToGet(Class<?> target, String name) {
 
StringBuffer sb = new StringBuffer(128);
 
405,7 → 405,7
return null;
}
 
public static Method findMethodToInject(Class<? extends Object> target, String name, Class<? extends Object> source) {
public static Method findMethodToInject(Class<?> target, String name, Class<?> source) {
 
StringBuffer sb = new StringBuffer(128);
 
426,7 → 426,7
 
if (m == null) {
 
Class<? extends Object> primitive = getPrimitiveFrom(source);
Class<?> primitive = getPrimitiveFrom(source);
 
if (primitive != null) {
 
448,7 → 448,7
 
}
 
public static Field findFieldToInject(Class<? extends Object> target, String name, Class<? extends Object> source) {
public static Field findFieldToInject(Class<?> target, String name, Class<?> source) {
 
Field f = getField(target, name);
 
524,7 → 524,7
 
}
 
public static boolean hasDefaultConstructor(Class<? extends Object> klass) {
public static boolean hasDefaultConstructor(Class<?> klass) {
 
try {
 
552,7 → 552,7
 
String methodName = getter(nameProperty);
 
Class<? extends Object> clazz = bean.getClass();
Class<?> clazz = bean.getClass();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == 0) {
610,7 → 610,7
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();
Class<?> targetClass = target.getClass();
 
Map<String, Object> setters, fields;
 
/trunk/src/main/java/org/mentacontainer/util/FindConstructor.java
18,8 → 18,8
* @param source Class to find a constructor for
* @param parameterTypes Parameter types to search for
*/
public static Constructor<? extends Object> getConstructor(Class<? extends Object> source,
Class<? extends Object>[] parameterTypes)
public static Constructor<?> getConstructor(Class<?> source,
Class<?>[] parameterTypes)
throws NoSuchMethodException {
return internalFind(source.getConstructors(),
parameterTypes);
31,8 → 31,8
* @param source Class to find method in
* @param parameterTypes Parameter types to search for
*/
public static Constructor<? extends Object> getDeclaredConstructor(Class<? extends Object> source,
Class<? extends Object>[] parameterTypes)
public static Constructor<?> getDeclaredConstructor(Class<?> source,
Class<?>[] parameterTypes)
throws NoSuchMethodException {
return internalFind(source.getDeclaredConstructors(),
parameterTypes);
41,18 → 41,18
/**
* Internal method to find the most specific applicable method
*/
private static Constructor<? extends Object> internalFind(Constructor<? extends Object>[] toTest,
Class<? extends Object>[] parameterTypes)
private static Constructor<?> internalFind(Constructor<?>[] toTest,
Class<?>[] parameterTypes)
throws NoSuchMethodException {
int l = parameterTypes.length;
 
// First find the applicable methods
List<Constructor<? extends Object>> applicableMethods = new LinkedList<Constructor<? extends Object>>();
List<Constructor<?>> applicableMethods = new LinkedList<Constructor<?>>();
 
for (int i = 0; i < toTest.length; i++) {
// Check the parameters match
Class<? extends Object>[] params = toTest[i].getParameterTypes();
Class<?>[] params = toTest[i].getParameterTypes();
 
if (params.length != l)
continue;
92,17 → 92,17
for (int i = 0; i < size; i++) {
int j;
// In terms of the JLS, current is T
Constructor<? extends Object> current = applicableMethods.get(i);
Class<? extends Object>[] currentParams = current.getParameterTypes();
Class<? extends Object> currentDeclarer = current.getDeclaringClass();
Constructor<?> current = applicableMethods.get(i);
Class<?>[] currentParams = current.getParameterTypes();
Class<?> currentDeclarer = current.getDeclaringClass();
 
for (j = 0; j < size; j++) {
if (i == j)
continue;
// In terms of the JLS, test is U
Constructor<? extends Object> test = applicableMethods.get(j);
Class<? extends Object>[] testParams = test.getParameterTypes();
Class<? extends Object> testDeclarer = test.getDeclaringClass();
Constructor<?> test = applicableMethods.get(j);
Class<?>[] testParams = test.getParameterTypes();
Class<?> testDeclarer = test.getDeclaringClass();
 
// Check if T is a subclass of U, breaking if not
if (!testDeclarer.isAssignableFrom(currentDeclarer))