use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.
the class XmlIocLoaderTest method testXmlIocLoader.
@Test
public void testXmlIocLoader() throws ObjectLoadException {
IocLoader iocLoader = getNew("org/nutz/ioc/loader/xml/conf/zzh-offered.xml");
assertTrue(iocLoader.getName() != null);
assertTrue(iocLoader.getName().length > 0);
for (String name : iocLoader.getName()) {
assertNotNull(name);
assertNotNull(iocLoader.load(null, name));
IocObject iocObject = iocLoader.load(null, name);
if (iocObject.hasArgs()) {
for (IocValue iocValue : iocObject.getArgs()) {
iocValue.getType();
iocValue.getValue();
checkValue(iocValue);
}
}
if (iocObject.getFields() != null) {
for (IocField iocField : iocObject.getFields().values()) {
assertNotNull(iocField.getName());
if (iocField.getValue() != null) {
IocValue iocValue = iocField.getValue();
checkValue(iocValue);
}
}
}
}
iocLoader.load(null, "obj").getFields().values().iterator().next().getValue().getValue();
}
use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.
the class IocCustomizedValueTypeTest method test_simple_customized.
@Test
public void test_simple_customized() {
String json = "{xb:{name:{cc:'XiaoBai'}}}";
Ioc2 ioc = new NutIoc(new MapLoader(json));
ioc.addValueProxyMaker(new ValueProxyMaker() {
public ValueProxy make(IocMaking ing, IocValue iv) {
if ("cc".equalsIgnoreCase(iv.getType())) {
return new StaticValue("CC:" + iv.getValue());
}
return null;
}
public String[] supportedTypes() {
return Lang.array("cc");
}
});
Pet pet = ioc.get(Pet.class, "xb");
assertEquals("CC:XiaoBai", pet.getName());
ioc.depose();
}
use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.
the class IocLoading method object2value.
@SuppressWarnings("unchecked")
IocValue object2value(Object obj) {
IocValue iv = new IocValue();
// Null
if (null == obj) {
iv.setType("null");
return iv;
} else // IocValue
if (obj instanceof IocValue) {
return (IocValue) obj;
} else // Map
if (obj instanceof Map<?, ?>) {
Map<String, Object> map = (Map<String, Object>) obj;
if (map.size() == 1) {
Entry<String, ?> en = map.entrySet().iterator().next();
String key = en.getKey();
// support this type or not?
if (supportedTypes.contains(key)) {
iv.setType(key);
iv.setValue(en.getValue());
return iv;
}
}
// Inner
if (map.size() > 0 && isIocObject(map)) {
iv.setType(IocValue.TYPE_INNER);
try {
iv.setValue(map2iobj(map));
} catch (ObjectLoadException e) {
throw Lang.wrapThrow(e);
}
return iv;
}
// Normal map
Map<String, IocValue> newmap = new HashMap<String, IocValue>();
for (Entry<String, Object> en : map.entrySet()) {
IocValue v = object2value(en.getValue());
newmap.put(en.getKey(), v);
}
iv.setType(IocValue.TYPE_NORMAL);
iv.setValue(newmap);
return iv;
} else // Array
if (obj.getClass().isArray()) {
Object[] array = (Object[]) obj;
IocValue[] ivs = new IocValue[array.length];
for (int i = 0; i < ivs.length; i++) {
ivs[i] = object2value(array[i]);
}
iv.setType(IocValue.TYPE_NORMAL);
iv.setValue(ivs);
return iv;
} else // Collection
if (obj instanceof Collection<?>) {
try {
Collection<IocValue> values = (Collection<IocValue>) Mirror.me(obj).born();
Iterator<?> it = ((Collection<?>) obj).iterator();
while (it.hasNext()) {
Object o = it.next();
IocValue v = object2value(o);
values.add(v);
}
iv.setType(IocValue.TYPE_NORMAL);
iv.setValue(values);
return iv;
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
// Normal
iv.setType(IocValue.TYPE_NORMAL);
iv.setValue(obj);
return iv;
}
use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.
the class DefaultValueProxyMaker method make.
@SuppressWarnings("unchecked")
public ValueProxy make(IocMaking ing, IocValue iv) {
Object value = iv.getValue();
String type = iv.getType();
// Null
if ("null".equals(type) || null == value) {
return new StaticValue(null);
}
if (value instanceof ValueProxy) {
return (ValueProxy) value;
} else // String, Number, .....
if ("normal".equals(type) || null == type) {
// Array
if (value.getClass().isArray()) {
Object[] vs = (Object[]) value;
IocValue[] tmp = new IocValue[vs.length];
for (int i = 0; i < tmp.length; i++) tmp[i] = (IocValue) vs[i];
return new ArrayValue(ing, tmp);
} else // Map
if (value instanceof Map<?, ?>) {
return new MapValue(ing, (Map<String, IocValue>) value, (Class<? extends Map<String, Object>>) value.getClass());
} else // Collection
if (value instanceof Collection<?>) {
return new CollectionValue(ing, (Collection<IocValue>) value, (Class<? extends Collection<Object>>) value.getClass());
} else // Inner Object
if (value instanceof IocObject) {
return new InnerValue((IocObject) value);
}
return new StaticValue(value);
} else // Refer
if ("refer".equals(type)) {
String s = value.toString();
if (null != s) {
String renm = s.toLowerCase();
// $Ioc
if ("$ioc".equals(renm)) {
return new IocSelfValue();
} else // $Name
if ("$name".equals(renm)) {
return new ObjectNameValue();
} else // $Context
if ("$context".equals(renm)) {
return new IocContextObjectValue();
}
}
return new ReferValue(s);
} else // Refer_Type
if (IocValue.TYPE_REFER_TYPE.equals(type)) {
if (value instanceof CharSequence) {
String[] tmp = value.toString().split("#");
return new ReferTypeValue(tmp[0], Lang.forName(tmp[1], Object.class));
} else if (value instanceof Field) {
return new ReferTypeValue((Field) value);
}
throw new IocException(ing.getObjectName(), "unspported refer_type:'%s'", value);
} else // Java
if ("java".equals(type)) {
return new JavaValue(value.toString());
} else // File
if ("file".equals(type)) {
return new FileValue(value.toString());
} else // Env
if ("env".equals(type)) {
return new EnvValue(value);
} else // System Properties
if ("sys".equals(type)) {
return new SysPropValue(value);
} else // Inner
if ("inner".equals(type)) {
return new InnerValue((IocObject) value);
} else // JNDI
if ("jndi".equals(type)) {
return new JNDI_Value(value.toString());
} else if ("el".equals(type)) {
return new EL_Value(value.toString());
}
return null;
}
use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.
the class JsonTest method test_ioc_value.
@Test
public void test_ioc_value() {
String s = "{value:1,type:'normal'}";
IocValue iv = Json.fromJson(IocValue.class, s);
assertEquals(1, ((Integer) iv.getValue()).intValue());
assertEquals("normal", iv.getType());
}
Aggregations