Package org.nutz.ioc.meta

Examples of org.nutz.ioc.meta.IocObject


    public IocObject load(IocLoading loading, String name) throws ObjectLoadException {

        for (IocLoader iocLoader : iocLoaders)
            if (iocLoader.has(name)) {
                IocObject iocObject = iocLoader.load(loading, name);
                if (log.isDebugEnabled())
                    log.debugf("Found IocObject(%s) in IocLoader(%s)", name, iocLoader.getClass().getSimpleName() + "@" + iocLoader.hashCode());
                return iocObject;
            }
        throw new ObjectLoadException("Object '" + name + "' without define!");
View Full Code Here


        if (iocMap.containsKey(beanId))
            throw Lang.makeThrow("Name of bean is not unique! name=" + beanId);

        if (LOG.isDebugEnabled())
            LOG.debugf("Resolving bean define, name = %s", beanId);
        IocObject iocObject = new IocObject();
        String beanType = beanElement.getAttribute("type");
        if (!Strings.isBlank(beanType))
            iocObject.setType(Lang.loadClass(beanType));
        String beanScope = beanElement.getAttribute("scope");
        if (!Strings.isBlank(beanScope))
            iocObject.setScope(beanScope);
        String beanParent = beanElement.getAttribute("parent");
        if (!Strings.isBlank(beanParent))
            parentMap.put(beanId, beanParent);

        parseArgs(beanElement, iocObject);
View Full Code Here

            while (it.hasNext()) {
                Entry<String, String> entry = it.next();
                String beanId = entry.getKey();
                String parentId = entry.getValue();
                if (parentMap.get(parentId) == null) {
                    IocObject newIocObject = Iocs.mergeWith(iocMap.get(beanId),
                                                            iocMap.get(parentId));
                    iocMap.put(beanId, newIocObject);
                    it.remove();
                }
            }
View Full Code Here

                                     "Duplicate beanName=%s, by %s !!  Have been define by %s !!",
                                     beanName,
                                     classZ,
                                     map.get(beanName).getClass());

            IocObject iocObject = new IocObject();
            iocObject.setType(classZ);
            map.put(beanName, iocObject);

            iocObject.setSingleton(iocBean.singleton());
            if (!Strings.isBlank(iocBean.scope()))
                iocObject.setScope(iocBean.scope());

            // 看看构造函数都需要什么函数
            String[] args = iocBean.args();
            // if (null == args || args.length == 0)
            // args = iocBean.param();
            if (null != args && args.length > 0)
                for (String value : args)
                    iocObject.addArg(convert(value));

            // 设置Events
            IocEventSet eventSet = new IocEventSet();
            iocObject.setEvents(eventSet);
            if (!Strings.isBlank(iocBean.create()))
                eventSet.setCreate(iocBean.create().trim().intern());
            if (!Strings.isBlank(iocBean.depose()))
                eventSet.setDepose(iocBean.depose().trim().intern());
            if (!Strings.isBlank(iocBean.fetch()))
                eventSet.setFetch(iocBean.fetch().trim().intern());

            // 处理字段(以@Inject方式,位于字段)
            List<String> fieldList = new ArrayList<String>();
            Mirror<?> mirror = Mirror.me(classZ);
            Field[] fields = mirror.getFields(Inject.class);
            for (Field field : fields) {
                Inject inject = field.getAnnotation(Inject.class);
                // 无需检查,因为字段名是唯一的
                // if(fieldList.contains(field.getName()))
                // throw duplicateField(classZ,field.getName());
                IocField iocField = new IocField();
                iocField.setName(field.getName());
                IocValue iocValue;
                if (Strings.isBlank(inject.value())) {
                    iocValue = new IocValue();
                    iocValue.setType(IocValue.TYPE_REFER);
                    iocValue.setValue(field.getName());
                } else
                    iocValue = convert(inject.value());
                iocField.setValue(iocValue);
                iocObject.addField(iocField);
                fieldList.add(iocField.getName());
            }
            // 处理字段(以@Inject方式,位于set方法)
            Method[] methods;
            try {
                methods = classZ.getMethods();
            }
            catch (Exception e) {
                // 如果获取失败,就忽略之
                log.info("Fail to call getMethods(), miss class or Security Limit, ignore it", e);
                methods = new Method[0];
            }
            for (Method method : methods) {
                Inject inject = method.getAnnotation(Inject.class);
                if (inject == null)
                    continue;
                // 过滤特殊方法
                int m = method.getModifiers();
                if (Modifier.isAbstract(m) || (!Modifier.isPublic(m)) || Modifier.isStatic(m))
                    continue;
                String methodName = method.getName();
                if (methodName.startsWith("set")
                    && methodName.length() > 3
                    && method.getParameterTypes().length == 1) {
                    IocField iocField = new IocField();
                    iocField.setName(Strings.lowerFirst(methodName.substring(3)));
                    if (fieldList.contains(iocField.getName()))
                        throw duplicateField(classZ, iocField.getName());
                    IocValue iocValue;
                    if (Strings.isBlank(inject.value())) {
                        iocValue = new IocValue();
                        iocValue.setType(IocValue.TYPE_REFER);
                        iocValue.setValue(Strings.lowerFirst(methodName.substring(3)));
                    } else
                        iocValue = convert(inject.value());
                    iocField.setValue(iocValue);
                    iocObject.addField(iocField);
                    fieldList.add(iocField.getName());
                }
            }
            // 处理字段(以@IocBean.field方式)
            String[] flds = iocBean.fields();
            if (flds != null && flds.length > 0) {
                for (String fieldInfo : flds) {
                    if (fieldList.contains(fieldInfo))
                        throw duplicateField(classZ, fieldInfo);
                    IocField iocField = new IocField();
                    if (fieldInfo.contains(":")) { // dao:jndi:dataSource/jdbc形式
                        String[] datas = fieldInfo.split(":", 2);
                        // 完整形式, 与@Inject完全一致了
                        iocField.setName(datas[0]);
                        iocField.setValue(convert(datas[1]));
                        iocObject.addField(iocField);
                    } else {
                        // 基本形式, 引用与自身同名的bean
                        iocField.setName(fieldInfo);
                        IocValue iocValue = new IocValue();
                        iocValue.setType(IocValue.TYPE_REFER);
                        iocValue.setValue(fieldInfo);
                        iocField.setValue(iocValue);
                        iocObject.addField(iocField);
                    }
                    fieldList.add(iocField.getName());
                }
            }
        } else {
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);
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

        if (iocMap.containsKey(beanId))
            throw Lang.makeThrow("Name of bean is not unique! name=" + beanId);

        if (LOG.isDebugEnabled())
            LOG.debugf("Resolving bean define, name = %s", beanId);
        IocObject iocObject = new IocObject();
        String beanType = beanElement.getAttribute("type");
        if (!Strings.isBlank(beanType))
            iocObject.setType(Lang.loadClass(beanType));
        String beanScope = beanElement.getAttribute("scope");
        if (!Strings.isBlank(beanScope))
            iocObject.setScope(beanScope);
        String beanParent = beanElement.getAttribute("parent");
        if (!Strings.isBlank(beanParent))
            parentMap.put(beanId, beanParent);
        String factory = beanElement.getAttribute("factory");
        if (!Strings.isBlank(factory))
          iocObject.setFactory(factory);

        parseArgs(beanElement, iocObject);
        parseFields(beanElement, iocObject);
        parseEvents(beanElement, iocObject);
View Full Code Here

            while (it.hasNext()) {
                Entry<String, String> entry = it.next();
                String beanId = entry.getKey();
                String parentId = entry.getValue();
                if (parentMap.get(parentId) == null) {
                    IocObject newIocObject = Iocs.mergeWith(iocMap.get(beanId),
                                                            iocMap.get(parentId));
                    iocMap.put(beanId, newIocObject);
                    it.remove();
                }
            }
View Full Code Here

    public IocObject load(IocLoading loading, String name) throws ObjectLoadException {

        for (IocLoader iocLoader : iocLoaders)
            if (iocLoader.has(name)) {
                IocObject iocObject = iocLoader.load(loading, name);
                if (log.isDebugEnabled())
                    log.debugf("Found IocObject(%s) in IocLoader(%s)", name, iocLoader.getClass().getSimpleName() + "@" + iocLoader.hashCode());
                return iocObject;
            }
        throw new ObjectLoadException("Object '" + name + "' without define!");
View Full Code Here

                                     "Duplicate beanName=%s, by %s !!  Have been define by %s !!",
                                     beanName,
                                     classZ,
                                     map.get(beanName).getClass());

            IocObject iocObject = new IocObject();
            iocObject.setType(classZ);
            map.put(beanName, iocObject);

            iocObject.setSingleton(iocBean.singleton());
            if (!Strings.isBlank(iocBean.scope()))
                iocObject.setScope(iocBean.scope());

            // 看看构造函数都需要什么函数
            String[] args = iocBean.args();
            // if (null == args || args.length == 0)
            // args = iocBean.param();
            if (null != args && args.length > 0)
                for (String value : args)
                    iocObject.addArg(convert(value));

            // 设置Events
            IocEventSet eventSet = new IocEventSet();
            iocObject.setEvents(eventSet);
            if (!Strings.isBlank(iocBean.create()))
                eventSet.setCreate(iocBean.create().trim().intern());
            if (!Strings.isBlank(iocBean.depose()))
                eventSet.setDepose(iocBean.depose().trim().intern());
            if (!Strings.isBlank(iocBean.fetch()))
                eventSet.setFetch(iocBean.fetch().trim().intern());

            // 处理字段(以@Inject方式,位于字段)
            List<String> fieldList = new ArrayList<String>();
            Mirror<?> mirror = Mirror.me(classZ);
            Field[] fields = mirror.getFields(Inject.class);
            for (Field field : fields) {
                Inject inject = field.getAnnotation(Inject.class);
                // 无需检查,因为字段名是唯一的
                // if(fieldList.contains(field.getName()))
                // throw duplicateField(classZ,field.getName());
                IocField iocField = new IocField();
                iocField.setName(field.getName());
                IocValue iocValue;
                if (Strings.isBlank(inject.value())) {
                    iocValue = new IocValue();
                    iocValue.setType(IocValue.TYPE_REFER);
                    iocValue.setValue(field.getName());
                } else
                    iocValue = convert(inject.value());
                iocField.setValue(iocValue);
                iocField.setOptional(inject.optional());
                iocObject.addField(iocField);
                fieldList.add(iocField.getName());
            }
            // 处理字段(以@Inject方式,位于set方法)
            Method[] methods;
            try {
                methods = classZ.getMethods();
            }
            catch (Exception e) {
                // 如果获取失败,就忽略之
                log.infof("Fail to call getMethods() in Class=%s, miss class or Security Limit, ignore it", classZ, e);
                methods = new Method[0];
            }
            catch (NoClassDefFoundError e) {
              log.infof("Fail to call getMethods() in Class=%s, miss class or Security Limit, ignore it", classZ, e);
                methods = new Method[0];
      }
            for (Method method : methods) {
                Inject inject = method.getAnnotation(Inject.class);
                if (inject == null)
                    continue;
                // 过滤特殊方法
                int m = method.getModifiers();
                if (Modifier.isAbstract(m) || (!Modifier.isPublic(m)) || Modifier.isStatic(m))
                    continue;
                String methodName = method.getName();
                if (methodName.startsWith("set")
                    && methodName.length() > 3
                    && method.getParameterTypes().length == 1) {
                    IocField iocField = new IocField();
                    iocField.setName(Strings.lowerFirst(methodName.substring(3)));
                    if (fieldList.contains(iocField.getName()))
                        throw duplicateField(classZ, iocField.getName());
                    IocValue iocValue;
                    if (Strings.isBlank(inject.value())) {
                        iocValue = new IocValue();
                        iocValue.setType(IocValue.TYPE_REFER);
                        iocValue.setValue(Strings.lowerFirst(methodName.substring(3)));
                    } else
                        iocValue = convert(inject.value());
                    iocField.setValue(iocValue);
                    iocObject.addField(iocField);
                    fieldList.add(iocField.getName());
                }
            }
            // 处理字段(以@IocBean.field方式)
            String[] flds = iocBean.fields();
            if (flds != null && flds.length > 0) {
                for (String fieldInfo : flds) {
                    if (fieldList.contains(fieldInfo))
                        throw duplicateField(classZ, fieldInfo);
                    IocField iocField = new IocField();
                    if (fieldInfo.contains(":")) { // dao:jndi:dataSource/jdbc形式
                        String[] datas = fieldInfo.split(":", 2);
                        // 完整形式, 与@Inject完全一致了
                        iocField.setName(datas[0]);
                        iocField.setValue(convert(datas[1]));
                        iocObject.addField(iocField);
                    } else {
                        // 基本形式, 引用与自身同名的bean
                        iocField.setName(fieldInfo);
                        IocValue iocValue = new IocValue();
                        iocValue.setType(IocValue.TYPE_REFER);
                        iocValue.setValue(fieldInfo);
                        iocField.setValue(iocValue);
                        iocObject.addField(iocField);
                    }
                    fieldList.add(iocField.getName());
                }
            }
           
            // 处理工厂方法
            if (!Strings.isBlank(iocBean.factory())) {
              iocObject.setFactory(beanName);
            }
        } else {
          // 这里只是检查一下@Inject,要避免抛出异常.
            try {
        if (log.isWarnEnabled()) {
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.