MentaContainer

Compare Revisions

Ignore whitespace Rev 91 → Rev 92

/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
196,14 → 196,35
@Test
public void testPrimitiveInitValue2() {
Factory f = new ClassFactory(TestObj3_1.class).addInitValue(new Integer(20));
Factory f = new ClassFactory(TestObj3_1.class).addInitValue(20);
TestObj3_1 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
private static class TestObj3_2 {
int x;
long l;
public TestObj3_2(Integer x, long l) {
this.x = x;
this.l = l;
}
}
@Test
public void testPrimitiveInitValue3() {
Factory f = new ClassFactory(TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
TestObj3_2 o = f.getInstance();
Assert.assertEquals(20, o.x);
Assert.assertEquals(30L, o.l);
}
private static class TestObj4 {
int x;
222,7 → 243,7
@Test
public void testMixInitValues() {
Factory f = new ClassFactory(TestObj4.class).addInitValue(20).addInitValue("hello").addInitValue(new Long(20)).addInitValue(20L);
Factory f = new ClassFactory(TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
TestObj4 o = f.getInstance();
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
91,7 → 91,6
return this;
}
 
@Override
public ConfigurableFactory addInitValue(Object value) {
100,52 → 99,39
}
@Override
public ConfigurableFactory addInitValue(int x) {
public ConfigurableFactory addInitPrimitive(Object value) {
return addInitValue(x, int.class);
}
@Override
public ConfigurableFactory addInitValue(boolean b) {
Class<? extends Object> primitive = getPrimitiveFrom(value);
return addInitValue(b, boolean.class);
}
@Override
public ConfigurableFactory addInitValue(long l) {
if (primitive == null) throw new IllegalArgumentException("Value is not a primitive: " + value);
return addInitValue(l, long.class);
return addInitValue(value, primitive);
}
@Override
public ConfigurableFactory addInitValue(double d) {
private List<Class<? extends Object>> convertToPrimitives(List<Class<? extends Object>> list) {
return addInitValue(d, double.class);
}
 
@Override
public ConfigurableFactory addInitValue(float f) {
Iterator<Class<? extends Object>> iter = list.iterator();
return addInitValue(f, float.class);
}
 
@Override
public ConfigurableFactory addInitValue(byte b) {
List<Class<? extends Object>> results = new LinkedList<Class<? extends Object>>();
return addInitValue(b, byte.class);
}
 
@Override
public ConfigurableFactory addInitValue(short s) {
while(iter.hasNext()) {
Class<? extends Object> klass = iter.next();
Class<? extends Object> primitive = getPrimitiveFrom(klass);
if (primitive != null) {
results.add(primitive);
} else {
results.add(klass);
}
}
return addInitValue(s, short.class);
return results;
}
 
@Override
public ConfigurableFactory addInitValue(char c) {
return addInitValue(c, char.class);
}
private Class<? extends Object>[] getClasses(List<Class<? extends Object>> values) {
258,8 → 244,20
else if (w instanceof Float) { return Float.TYPE; }
else if (w instanceof Double) { return Double.TYPE; }
return null;
}
}
private static Class<? extends Object> getPrimitiveFrom(Class<? extends Object> 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; }
else if (klass.equals(Character.class)) { return Character.TYPE; }
else if (klass.equals(Integer.class)) { return Integer.TYPE; }
else if (klass.equals(Long.class)) { return Long.TYPE; }
else if (klass.equals(Float.class)) { return Float.TYPE; }
else if (klass.equals(Double.class)) { return Double.TYPE; }
return null;
}
@Override
public <T> T getInstance() {
275,7 → 273,16
} catch(Exception e) {
throw new RuntimeException("Cannot find a constructor for class: " + klass, e);
// try primitives...
try {
constructor = klass.getConstructor(getClasses(convertToPrimitives(initTypes)));
} catch(Exception ee) {
 
throw new RuntimeException("Cannot find a constructor for class: " + klass);
}
}
}
/trunk/src/main/java/org/mentacontainer/ConfigurableFactory.java
20,15 → 20,8
*/
public ConfigurableFactory addInitValue(Object value);
public ConfigurableFactory addInitValue(int x);
public ConfigurableFactory addInitValue(boolean b);
public ConfigurableFactory addInitValue(long l);
public ConfigurableFactory addInitValue(float f);
public ConfigurableFactory addInitValue(short s);
public ConfigurableFactory addInitValue(double d);
public ConfigurableFactory addInitValue(byte b);
public ConfigurableFactory addInitValue(char c);
 
public ConfigurableFactory addInitPrimitive(Object value);
/**
* Add a property to be injected through a setter when the component is instantiated.
*