Package org.nutz.ioc.meta

Examples of org.nutz.ioc.meta.IocObject


                    try {
                        if (log.isDebugEnabled())
                            log.debug("\t >> Load definition");

                        // 读取对象定义
                        IocObject iobj = loader.load(createLoading(), name);
                        if (null == iobj) {
                          for (String iocBeanName : loader.getName()) {
                            // 相似性少于3 --> 大小写错误,1-2个字符调换顺序或写错
                if (3 > LevenshteinDistance.computeLevenshteinDistance(name.toLowerCase(), iocBeanName.toLowerCase())) {
                  throw new IocException("Undefined object '%s' but found similar name '%s'", name, iocBeanName);
                }
              }
                            throw new IocException("Undefined object '%s'", name);
                        }

                        // 修正对象类型
                        if (null == iobj.getType())
                            if (null == type)
                                throw new IocException("NULL TYPE object '%s'", name);
                            else
                                iobj.setType(type);

                        // 检查对象级别
                        if (Strings.isBlank(iobj.getScope()))
                            iobj.setScope(defaultScope);

                        // 根据对象定义,创建对象,maker 会自动的缓存对象到 context 中
                        if (log.isDebugEnabled())
                            log.debugf("\t >> Make...'%s'<%s>", name, type == null ? "" : type);
                        op = maker.make(ing, iobj);
View Full Code Here


            log.debug("Loading define for name="+name);
        // If has parent
        Object p = m.get("parent");
        if (null != p) {
            checkParents(name);
            IocObject parent = load(loading, p.toString());
            // create new map without parent
            Map<String, Object> newMap = new HashMap<String, Object>();
            for (Entry<String, Object> en : m.entrySet()) {
                if ("parent".equals(en.getKey()))
                    continue;
                newMap.put(en.getKey(), en.getValue());
            }
            // Create self IocObject
            IocObject self = loading.map2iobj(newMap);

            // Merge with parent
            return Iocs.mergeWith(self, parent);
        }
        return loading.map2iobj(m);
View Full Code Here

    public boolean has(String name) {
        return proxyIocLoader.has(name);
    }

    public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
        IocObject iocObject = map.get(name);
        if (iocObject == null) {
            iocObject = proxyIocLoader.load(loading, name);
            if (iocObject == null)
                return null;
            if (iocObject.isSingleton() && iocObject.getType() != null)
                map.put(name, iocObject);
        }
        return iocObject;
    }
View Full Code Here

        return new ObjectLoadException(String.format(fmt, args), e);
    }

    @SuppressWarnings("unchecked")
    public IocObject map2iobj(Map<String, Object> map) throws ObjectLoadException {
        final IocObject iobj = new IocObject();
        if (!isIocObject(map)) {
            for (Entry<String, Object> en : map.entrySet()) {
                IocField ifld = new IocField();
                ifld.setName(en.getKey());
                ifld.setValue(object2value(en.getValue()));
                iobj.addField(ifld);
            }
            if (log.isWarnEnabled()) // TODO 移除这种兼容性
                log.warn("Using *Declared* ioc-define (without type or events)!!! Pls use Standard Ioc-Define!!"
                            + " Bean will define as:\n"
                            + Json.toJson(iobj));
        } else {
            Object v = map.get("type");
            // type
            try {
                String typeName = (String) v;
                if (!Strings.isBlank(typeName)) {
                    iobj.setType(Lang.loadClass(typeName));
                }
            }
            catch (Exception e) {
                throw E(e, "Wrong type name: '%s'", v);
            }
            // singleton
            try {
                v = map.get("singleton");
                if (null != v)
                    iobj.setSingleton(Castors.me().castTo(v, boolean.class));
            }
            catch (FailToCastObjectException e) {
                throw E(e, "Wrong singleton: '%s'", v);
            }
            // scope
            v = map.get("scope");
            if (null != v)
                iobj.setScope(v.toString());
            // events
            try {
                v = map.get("events");
                if (null != v) {
                    IocEventSet ies = Lang.map2Object((Map<?, ?>) v, IocEventSet.class);
                    iobj.setEvents(ies);
                }
            }
            catch (Exception e) {
                throw E(e, "Wrong events: '%s'", v);
            }
            // args
            try {
                v = map.get("args");
                if (null != v) {
                    Lang.each(v, new Each<Object>() {
                        public void invoke(int i, Object ele, int length) {
                            iobj.addArg(object2value(ele));
                        }
                    });
                }
            }
            catch (Exception e) {
                throw E(e, "Wrong args: '%s'", v);
            }
            // fields
            try {
                v = map.get("fields");
                if (null != v) {
                    Map<String, Object> fields = (Map<String, Object>) v;
                    for (Entry<String, Object> en : fields.entrySet()) {
                        IocField ifld = new IocField();
                        ifld.setName(en.getKey());
                        ifld.setValue(object2value(en.getValue()));
                        iobj.addField(ifld);
                    }
                }
            }
            catch (Exception e) {
                throw E(e, "Wrong args: '%s'", v);
            }
            // factory方法
            v = map.get("factory");
            if (v != null && !Strings.isBlank(v.toString())) {
              iobj.setFactory(v.toString());
      }
        }
        return iobj;
    }
View Full Code Here

        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()) {
                    assertNotNull(iocField.getName());
                    if (iocField.getValue() != null) {
                        IocValue iocValue = iocField.getValue();
                        checkValue(iocValue);
                    }
View Full Code Here

        assertTrue(iocLoader.has("classA"));
    }

    @Test
    public void testLoad() throws Throwable {
        IocObject iocObject = iocLoader.load(null, "classB");
        assertNotNull(iocObject);
        assertNotNull(iocObject.getFields());
        assertTrue(iocObject.getFields().length == 1);
        assertEquals("refer", iocObject.getFields()[0].getValue().getType());
    }
View Full Code Here

TOP

Related Classes of org.nutz.ioc.meta.IocObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.