private void initField() {
    field = ReflectionUtils.getFieldByNameIncludingSuperclasses(fieldName, object.getClass());

    if (field == null) {
      return;
    }

    fieldType = field.getType();

    try {
      fieldTypeConverter = lookup.lookupConverterForType(fieldType);
    } catch (ComponentConfigurationException e) {
      // ignore, handle later
    }
  }
  private void initSetter() {
    setter = ReflectionUtils.getSetter(fieldName, object.getClass());

    if (setter == null) {
      return;
    }

    setterParamType = setter.getParameterTypes()[0];

    try {
      setterTypeConverter = lookup.lookupConverterForType(setterParamType);
    } catch (ComponentConfigurationException e) {
      // ignore, handle later
    }
  }
  public Object fromConfiguration(
      ConverterLookup converterLookup,
      PlexusConfiguration configuration,
      Class type,
      Class baseType,
      ClassLoader classLoader,
      ExpressionEvaluator expressionEvaluator,
      ConfigurationListener listener)
      throws ComponentConfigurationException {
    Object retValue = fromExpression(configuration, expressionEvaluator, type);
    if (retValue != null) {
      return retValue;
    }

    Class implementation = getClassForImplementationHint(null, configuration, classLoader);

    if (implementation != null) {
      retValue = instantiateObject(implementation);
    } else {
      // we can have 2 cases here:
      //  - provided collection class which is not abstract
      //     like Vector, ArrayList, HashSet - so we will just instantantiate it
      // - we have an abtract class so we have to use default collection type
      int modifiers = type.getModifiers();

      if (Modifier.isAbstract(modifiers)) {
        retValue = getDefaultCollection(type);
      } else {
        try {
          retValue = type.newInstance();
        } catch (IllegalAccessException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        } catch (InstantiationException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        }
      }
    }
    // now we have collection and we have to add some objects to it

    for (int i = 0; i < configuration.getChildCount(); i++) {
      PlexusConfiguration c = configuration.getChild(i);

      Class childType = getImplementationClass(null, baseType, c, classLoader);

      ConfigurationConverter converter = converterLookup.lookupConverterForType(childType);

      Object object =
          converter.fromConfiguration(
              converterLookup, c, childType, baseType, classLoader, expressionEvaluator, listener);

      Collection collection = (Collection) retValue;
      collection.add(object);
    }

    return retValue;
  }
Esempio n. 4
0
  public Object fromConfiguration(
      ConverterLookup converterLookup,
      PlexusConfiguration configuration,
      Class type,
      Class baseType,
      ClassLoader classLoader,
      ExpressionEvaluator expressionEvaluator)
      throws ComponentConfigurationException {
    Object retValue = fromExpression(configuration, expressionEvaluator, type);
    if (retValue != null) {
      return retValue;
    }

    Class implementation = getClassForImplementationHint(null, configuration, classLoader);

    if (implementation != null) {
      retValue = instantiateObject(implementation);
    } else {
      // we can have 2 cases here:
      //  - provided collection class which is not abstract
      //     like Vector, ArrayList, HashSet - so we will just instantantiate it
      // - we have an abtract class so we have to use default collection type
      int modifiers = type.getModifiers();

      if (Modifier.isAbstract(modifiers)) {
        retValue = getDefaultCollection(type);
      } else {
        try {
          retValue = type.newInstance();
        } catch (IllegalAccessException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        } catch (InstantiationException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        }
      }
    }
    // now we have collection and we have to add some objects to it

    for (int i = 0; i < configuration.getChildCount(); i++) {
      PlexusConfiguration c = configuration.getChild(i);
      // Object o = null;

      String configEntry = c.getName();

      String name = StringUtils.capitalizeFirstLetter(fromXML(configEntry));

      Class childType = getClassForImplementationHint(null, c, classLoader);

      if (childType == null) {
        // Some classloaders don't create Package objects for classes
        // so we have to resort to slicing up the class name

        String baseTypeName = baseType.getName();

        int lastDot = baseTypeName.lastIndexOf('.');

        String className;

        if (lastDot == -1) {
          className = name;
        } else {
          String basePackage = baseTypeName.substring(0, lastDot);

          className = basePackage + "." + name;
        }

        childType = loadClass(className, classLoader);
      }

      ConfigurationConverter converter = converterLookup.lookupConverterForType(childType);

      Object object =
          converter.fromConfiguration(
              converterLookup, c, childType, baseType, classLoader, expressionEvaluator);

      Collection collection = (Collection) retValue;
      collection.add(object);
    }

    return retValue;
  }