Search in sources :

Example 6 with IocValue

use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.

the class AnnotationIocLoader method addClass.

protected void addClass(Class<?> classZ) {
    if (classZ.isInterface() || classZ.isMemberClass() || classZ.isEnum() || classZ.isAnnotation() || classZ.isAnonymousClass())
        return;
    int modify = classZ.getModifiers();
    if (Modifier.isAbstract(modify) || (!Modifier.isPublic(modify)))
        return;
    IocBean iocBean = classZ.getAnnotation(IocBean.class);
    if (iocBean != null) {
        if (log.isDebugEnabled())
            log.debugf("Found @IocBean : %s", classZ);
        // 采用 @IocBean->name
        String beanName = iocBean.name();
        if (Strings.isBlank(beanName)) {
            // 否则采用 @InjectName
            InjectName innm = classZ.getAnnotation(InjectName.class);
            if (null != innm && !Strings.isBlank(innm.value())) {
                beanName = innm.value();
            } else // 大哥(姐),您都不设啊!? 那就用 simpleName 吧
            {
                beanName = Strings.lowerFirst(classZ.getSimpleName());
            }
        }
        // 重名了, 需要用户用@IocBean(name="xxxx") 区分一下
        if (map.containsKey(beanName))
            throw new IocException(beanName, "Duplicate beanName=%s, by %s !!  Have been define by %s !!", beanName, classZ.getName(), map.get(beanName).getType().getName());
        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();
        // args = iocBean.param();
        if (null != args && args.length > 0)
            for (String value : args) iocObject.addArg(Iocs.convert(value, true));
        // 设置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_TYPE);
                iocValue.setValue(field);
            } else
                iocValue = Iocs.convert(inject.value(), true);
            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(beanName, classZ, iocField.getName());
                IocValue iocValue;
                if (Strings.isBlank(inject.value())) {
                    iocValue = new IocValue();
                    iocValue.setType(IocValue.TYPE_REFER_TYPE);
                    iocValue.setValue(Strings.lowerFirst(methodName.substring(3)) + "#" + method.getParameterTypes()[0].getName());
                } else
                    iocValue = Iocs.convert(inject.value(), true);
                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(beanName, 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(Iocs.convert(datas[1], true));
                    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(iocBean.factory());
        }
    } else {
    // 不再检查其他类.
    }
}
Also used : IocException(org.nutz.ioc.IocException) IocObject(org.nutz.ioc.meta.IocObject) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ObjectLoadException(org.nutz.ioc.ObjectLoadException) IocException(org.nutz.ioc.IocException) Field(java.lang.reflect.Field) IocField(org.nutz.ioc.meta.IocField) IocEventSet(org.nutz.ioc.meta.IocEventSet) IocValue(org.nutz.ioc.meta.IocValue) IocField(org.nutz.ioc.meta.IocField) InjectName(org.nutz.ioc.annotation.InjectName)

Example 7 with IocValue

use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.

the class Iocs method wrap.

public static IocObject wrap(Object obj) {
    IocObject iobj = new IocObject();
    //iobj.setType(obj.getClass());
    iobj.setFactory(Iocs.class.getName() + "#self");
    IocValue ival = new IocValue(null, new StaticValue(obj));
    iobj.addArg(ival);
    return iobj;
}
Also used : IocValue(org.nutz.ioc.meta.IocValue) IocObject(org.nutz.ioc.meta.IocObject) StaticValue(org.nutz.ioc.val.StaticValue)

Example 8 with IocValue

use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.

the class Iocs method convert.

public static IocValue convert(String value, boolean dftRefer) {
    IocValue iocValue = new IocValue();
    if (dftRefer && value.startsWith(":")) {
        iocValue.setType(IocValue.TYPE_NORMAL);
        iocValue.setValue(value.substring(1));
    //        } else if (value.startsWith("$")) {
    //            iocValue.setType(IocValue.TYPE_REFER);
    //            iocValue.setValue(value.substring(1));
    } else if (value.contains(":")) {
        String type = value.substring(0, value.indexOf(':'));
        if (IocValue.types.contains(type)) {
            iocValue.setType(type);
            if (value.endsWith(":")) {
                iocValue.setValue("");
            } else
                iocValue.setValue(value.substring(value.indexOf(':') + 1));
        } else {
            iocValue.setType(IocValue.TYPE_NORMAL);
            iocValue.setValue(value);
        }
    } else {
        // XXX 兼容性改变, 1.b.52 开始默认就是refer, 如果真的要输入常量
        //log.info("auto set as         refer:" + value);
        iocValue.setType(IocValue.TYPE_REFER);
        iocValue.setValue(value);
    }
    return iocValue;
}
Also used : IocValue(org.nutz.ioc.meta.IocValue)

Example 9 with IocValue

use of org.nutz.ioc.meta.IocValue in project nutz by nutzam.

the class XmlIocLoader method parseX.

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 (EL_TAG.equalsIgnoreCase(type)) {
        iocValue.setType(EL_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;
}
Also used : IocValue(org.nutz.ioc.meta.IocValue) IocObject(org.nutz.ioc.meta.IocObject) HashSet(java.util.HashSet)

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