Package org.nutz.ioc.meta

Examples of org.nutz.ioc.meta.IocValue


            }
        }
    }

    protected IocValue convert(String value) {
        IocValue iocValue = new IocValue();
        if (value.contains(":")) {
            iocValue.setType(value.substring(0, value.indexOf(':')));
            iocValue.setValue(value.substring(value.indexOf(':') + 1));
        } else {
            iocValue.setValue(value); // TODO 是否应该改为默认refer呢?
        }
        return iocValue;
    }
View Full Code Here


        return iobj;
    }

    @SuppressWarnings("unchecked")
    IocValue object2value(Object obj) {
        IocValue iv = new IocValue();
        // Null
        if (null == obj) {
            iv.setType("null");
            return iv;
        }
        // IocValue
        else if (obj instanceof IocValue) {
            return (IocValue) obj;
        }
        // Map
        else 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 (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;
        }
        // Array
        else 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;
        }
        // Collection
        else 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;
View Full Code Here

    }

    @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());
    }
View Full Code Here

    protected static final String JNDI_TAG = IocValue.TYPE_JNDI;
    protected static final String SYS_TAG = IocValue.TYPE_SYS;
    protected static final String APP_TAG = IocValue.TYPE_APP;

    protected IocValue parseX(Element element) throws Throwable {
        IocValue iocValue = new IocValue();
        String type = element.getNodeName();
        if (EVN_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(EVN_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (SYS_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(SYS_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (JNDI_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(JNDI_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (JAVA_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(JAVA_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (REFER_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(REFER_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (FILE_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(FILE_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (APP_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(APP_TAG);
            iocValue.setValue(element.getTextContent());
        } else if (OBJ_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(REFER_TAG);
            iocValue.setValue(paserBean(element, true));
        } else if (MAP_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(null);
            iocValue.setValue(paserMap(element));
        } else if (LIST_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(null);
            iocValue.setValue(paserCollection(element));
        } else if (ARRAY_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(null);
            iocValue.setValue(paserCollection(element).toArray());
        } else if (SET_TAG.equalsIgnoreCase(type)) {
            iocValue.setType(null);
            Set<Object> set = new HashSet<Object>();
            set.addAll(paserCollection(element));
            iocValue.setValue(set);
        } else {
            iocValue.setType(null);
            if (element.getFirstChild() != null)
                iocValue.setValue(element.getFirstChild().getTextContent());
        }
        return iocValue;
    }
View Full Code Here

                // 无需检查,因为字段名是唯一的
                // 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());
                }
View Full Code Here

      }
        }
    }

    protected IocValue convert(String value) {
        IocValue iocValue = new IocValue();
        if (value.contains(":")) {
            iocValue.setType(value.substring(0, value.indexOf(':')));
            iocValue.setValue(value.substring(value.indexOf(':') + 1));
        } else {
            iocValue.setValue(value); // TODO 是否应该改为默认refer呢?
        }
        return iocValue;
    }
View Full Code Here

        return iobj;
    }

    @SuppressWarnings("unchecked")
    IocValue object2value(Object obj) {
        IocValue iv = new IocValue();
        // Null
        if (null == obj) {
            iv.setType("null");
            return iv;
        }
        // IocValue
        else if (obj instanceof IocValue) {
            return (IocValue) obj;
        }
        // Map
        else 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;
        }
        // Array
        else 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;
        }
        // Collection
        else 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;
View Full Code Here

    }

    @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());
    }
View Full Code Here

            }
            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

TOP

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

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.