コード例 #1
0
ファイル: PetiteContainer.java プロジェクト: rengy/jodd
  /** Wires methods. */
  protected void wireMethods(Object bean, BeanDefinition def, Map<String, Object> acquiredBeans) {
    if (def.methods == null) {
      def.methods = petiteResolvers.resolveMethodInjectionPoint(def.type);
    }
    for (MethodInjectionPoint methodRef : def.methods) {
      String[][] refNames = methodRef.references;
      Object[] args = new Object[refNames.length];
      for (int i = 0; i < refNames.length; i++) {
        String[] refName = refNames[i];
        Object value = null;

        boolean mixing = petiteConfig.wireScopedProxy || petiteConfig.detectMixedScopes;

        if (mixing) {
          BeanDefinition refBeanDefinition = lookupBeanDefinitions(refName);

          if (refBeanDefinition != null) {
            value = scopedProxyManager.lookupValue(this, def, refBeanDefinition);
          }
        }

        if (value == null) {
          value = getBean(refName, acquiredBeans);
        }

        args[i] = value;
        if (value == null) {
          if ((def.wiringMode == WiringMode.STRICT)) {
            throw new PetiteException(
                "Wiring failed. Beans references: '"
                    + Convert.toString(refName)
                    + "' not found for method: "
                    + def.type.getName()
                    + '#'
                    + methodRef.method.getName());
          }
        }
      }

      try {
        methodRef.method.invoke(bean, args);
      } catch (Exception ex) {
        throw new PetiteException(ex);
      }
    }
  }
コード例 #2
0
ファイル: PetiteContainer.java プロジェクト: rengy/jodd
  /** Wires properties. */
  protected void wireProperties(
      Object bean, BeanDefinition def, Map<String, Object> acquiredBeans) {
    if (def.properties == null) {
      def.properties =
          petiteResolvers.resolvePropertyInjectionPoint(
              def.type, def.wiringMode == WiringMode.AUTOWIRE);
    }

    boolean mixing = petiteConfig.wireScopedProxy || petiteConfig.detectMixedScopes;

    for (PropertyInjectionPoint pip : def.properties) {
      String[] refNames = pip.references;

      Object value = null;

      if (mixing) {
        BeanDefinition refBeanDefinition = lookupBeanDefinitions(refNames);

        if (refBeanDefinition != null) {
          value = scopedProxyManager.lookupValue(this, def, refBeanDefinition);
        }
      }

      if (value == null) {
        value = getBean(refNames, acquiredBeans);
      }

      if (value == null) {
        if ((def.wiringMode == WiringMode.STRICT)) {
          throw new PetiteException(
              "Wiring failed. Beans references: '"
                  + Convert.toString(refNames)
                  + "' not found for property: "
                  + def.type.getName()
                  + '#'
                  + pip.propertyDescriptor.getName());
        }
        continue;
      }

      // BeanUtil.setDeclaredProperty(bean, pip.propertyDescriptor.getName(), value);

      Setter setter = pip.propertyDescriptor.getSetter(true);
      try {
        setter.invokeSetter(bean, value);
      } catch (Exception ex) {
        throw new PetiteException("Wiring failed", ex);
      }
    }

    // sets
    if (def.sets == null) {
      def.sets =
          petiteResolvers.resolveSetInjectionPoint(def.type, def.wiringMode == WiringMode.AUTOWIRE);
    }
    for (SetInjectionPoint sip : def.sets) {

      String[] beanNames = resolveBeanNamesForType(sip.targetClass);

      Collection beans = sip.createSet(beanNames.length);

      for (String beanName : beanNames) {
        if (beanName.equals(def.name) == false) {
          Object value = getBean(beanName, acquiredBeans);
          beans.add(value);
        }
      }

      // BeanUtil.setDeclaredProperty(bean, sip.field.getName(), beans);

      Setter setter = sip.propertyDescriptor.getSetter(true);
      try {
        setter.invokeSetter(bean, beans);
      } catch (Exception ex) {
        throw new PetiteException("Wiring failed", ex);
      }
    }
  }