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)
              throw Lang.makeThrow("Undefined object '%s'", name);

            // 修正对象类型
            if (null == iobj.getType())
              if (null == type)
                throw Lang.makeThrow("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);
            op = maker.make(ing, iobj);
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

    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

      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

        // 大哥(姐),您都不设啊!? 那就用 simpleName 吧
        else {
          beanName = Strings.lowerFirst(classZ.getSimpleName());
        }
      }
      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 = 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

    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

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

                        // 读取对象定义
                        IocObject iobj = loader.load(createLoading(), name);
                        if (null == iobj)
                            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);
                        op = maker.make(ing, iobj);
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.