Package org.nutz.ioc.meta

Examples of org.nutz.ioc.meta.IocField


      }

      // 获得每个字段的注入方式
      FieldInjector[] fields = new FieldInjector[iobj.getFields().length];
      for (int i = 0; i < fields.length; i++) {
        IocField ifld = iobj.getFields()[i];
        try {
          ValueProxy vp = ing.makeValue(ifld.getValue());
          fields[i] = FieldInjector.create(mirror, ifld.getName(), vp);
        }
        catch (Exception e) {
          throw Lang.wrapThrow(e, "Fail to eval Injector for field: '%s'", ifld.getName());
        }
      }
      dw.setFields(fields);

      // 如果是单例对象,前面已经生成实例了,在这里需要填充一下它的字段
View Full Code Here


  @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) {
View Full Code Here

  }

  protected void parseFields(Element beanElement, IocObject iocObject) throws Throwable {
    List<Element> list = getChildNodesByTagName(beanElement, TAG_FIELD);
    for (Element fieldElement : list) {
        IocField iocField = new IocField();
        iocField.setName(fieldElement.getAttribute("name"));
        if (fieldElement.hasChildNodes()) {
          NodeList nodeList = fieldElement.getChildNodes();
          for (int j = 0; j < nodeList.getLength(); j++) {
            if (nodeList.item(j) instanceof Element) {
              iocField.setValue(parseX((Element) nodeList.item(j)));
              break;
            }
          }
        }
        iocObject.addField(iocField);
View Full Code Here

      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 = mirror.getMethods();
      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;
        if (method.getName().startsWith("set")
          && method.getName().length() > 3
          && method.getParameterTypes().length == 1) {
          IocField iocField = new IocField();
          iocField.setName(Strings.lowerFirst(method.getName().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(method.getName().substring(3)));
          } else
            iocValue = convert(inject.value());
          iocField.setValue(iocValue);
          iocObject.addField(iocField);
          fieldList.add(iocField.getName());
        }
      }
      // 处理字段(以@IocBean.field方式),只允许引用同名的bean, 就映射为 refer:FieldName
      String[] flds = iocBean.fields();
//      if (null == flds || flds.length == 0) {
//        flds = iocBean.field();
//      }
      if (flds != null && flds.length > 0) {
        for (String fieldInfo : flds) {
          if (fieldList.contains(fieldInfo))
            throw duplicateField(classZ,fieldInfo);
          IocField iocField = new IocField();
          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 (LOG.isDebugEnabled())
        LOG.debugf("Processed Ioc Class : %s as [%s]", classZ, beanName);
    }
View Full Code Here

    }

    protected void parseFields(Element beanElement, IocObject iocObject) throws Throwable {
        List<Element> list = getChildNodesByTagName(beanElement, TAG_FIELD);
        for (Element fieldElement : list) {
                IocField iocField = new IocField();
                iocField.setName(fieldElement.getAttribute("name"));
                if (fieldElement.hasChildNodes()) {
                    NodeList nodeList = fieldElement.getChildNodes();
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        if (nodeList.item(j) instanceof Element) {
                            iocField.setValue(parseX((Element) nodeList.item(j)));
                            break;
                        }
                    }
                }
                iocObject.addField(iocField);
View Full Code Here

            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 {
            if (log.isWarnEnabled()) {
                Field[] fields = classZ.getDeclaredFields();
View Full Code Here

            }

            // 获得每个字段的注入方式
            FieldInjector[] fields = new FieldInjector[iobj.getFields().length];
            for (int i = 0; i < fields.length; i++) {
                IocField ifld = iobj.getFields()[i];
                try {
                    ValueProxy vp = ing.makeValue(ifld.getValue());
                    fields[i] = FieldInjector.create(mirror, ifld.getName(), vp);
                }
                catch (Exception e) {
                    throw Lang.wrapThrow(e, "Fail to eval Injector for field: '%s'", ifld.getName());
                }
            }
            dw.setFields(fields);

            // 如果是单例对象,前面已经生成实例了,在这里需要填充一下它的字段
View Full Code Here

    @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) {
View Full Code Here

    }

    protected void parseFields(Element beanElement, IocObject iocObject) throws Throwable {
        List<Element> list = getChildNodesByTagName(beanElement, TAG_FIELD);
        for (Element fieldElement : list) {
                IocField iocField = new IocField();
                iocField.setName(fieldElement.getAttribute("name"));
                if ("true".equals(fieldElement.getAttribute("optional")))
                  iocField.setOptional(true);
                if (fieldElement.hasChildNodes()) {
                    NodeList nodeList = fieldElement.getChildNodes();
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        if (nodeList.item(j) instanceof Element) {
                            iocField.setValue(parseX((Element) nodeList.item(j)));
                            break;
                        }
                    }
                }
                iocObject.addField(iocField);
View Full Code Here

            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())) {
View Full Code Here

TOP

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

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.