Search in sources :

Example 1 with IocValue

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();
}
Also used : IocValue(org.nutz.ioc.meta.IocValue) IocObject(org.nutz.ioc.meta.IocObject) IocLoader(org.nutz.ioc.IocLoader) IocField(org.nutz.ioc.meta.IocField) Test(org.junit.Test)

Example 2 with IocValue

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();
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) MapLoader(org.nutz.ioc.loader.map.MapLoader) IocMaking(org.nutz.ioc.IocMaking) ValueProxy(org.nutz.ioc.ValueProxy) ValueProxyMaker(org.nutz.ioc.ValueProxyMaker) IocValue(org.nutz.ioc.meta.IocValue) Ioc2(org.nutz.ioc.Ioc2) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 3 with IocValue

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;
}
Also used : HashMap(java.util.HashMap) FailToCastObjectException(org.nutz.castor.FailToCastObjectException) IocValue(org.nutz.ioc.meta.IocValue) Collection(java.util.Collection) IocObject(org.nutz.ioc.meta.IocObject) Iocs.isIocObject(org.nutz.ioc.Iocs.isIocObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with IocValue

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;
}
Also used : IocException(org.nutz.ioc.IocException) Field(java.lang.reflect.Field) ValueProxy(org.nutz.ioc.ValueProxy) IocObject(org.nutz.ioc.meta.IocObject) IocValue(org.nutz.ioc.meta.IocValue) Collection(java.util.Collection) IocObject(org.nutz.ioc.meta.IocObject) Map(java.util.Map)

Example 5 with IocValue

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());
}
Also used : IocValue(org.nutz.ioc.meta.IocValue) Test(org.junit.Test)

Aggregations

IocValue (org.nutz.ioc.meta.IocValue)9 IocObject (org.nutz.ioc.meta.IocObject)6 Test (org.junit.Test)3 Field (java.lang.reflect.Field)2 Collection (java.util.Collection)2 Map (java.util.Map)2 IocException (org.nutz.ioc.IocException)2 ValueProxy (org.nutz.ioc.ValueProxy)2 IocField (org.nutz.ioc.meta.IocField)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FailToCastObjectException (org.nutz.castor.FailToCastObjectException)1 Pet (org.nutz.dao.test.meta.Pet)1 Ioc2 (org.nutz.ioc.Ioc2)1 IocLoader (org.nutz.ioc.IocLoader)1 IocMaking (org.nutz.ioc.IocMaking)1 Iocs.isIocObject (org.nutz.ioc.Iocs.isIocObject)1 ObjectLoadException (org.nutz.ioc.ObjectLoadException)1