예제 #1
0
 // we could magically make this work in many cases by doing
 // getAnyRef() (or getValue().unwrapped()), but anytime we
 // rely on that, we aren't doing the type conversions Config
 // usually does, and we will throw ClassCastException instead
 // of a nicer error message giving the name of the bad
 // setting. So, instead, we only support a limited number of
 // types plus you can always use Object, ConfigValue, Config,
 // ConfigObject, etc.  as an escape hatch.
 private static Object getValue(
     Class<?> beanClass,
     Type parameterType,
     Class<?> parameterClass,
     Config config,
     String configPropName) {
   if (parameterClass == Boolean.class || parameterClass == boolean.class) {
     return config.getBoolean(configPropName);
   } else if (parameterClass == Integer.class || parameterClass == int.class) {
     return config.getInt(configPropName);
   } else if (parameterClass == Double.class || parameterClass == double.class) {
     return config.getDouble(configPropName);
   } else if (parameterClass == Long.class || parameterClass == long.class) {
     return config.getLong(configPropName);
   } else if (parameterClass == String.class) {
     return config.getString(configPropName);
   } else if (parameterClass == Duration.class) {
     return config.getDuration(configPropName);
   } else if (parameterClass == ConfigMemorySize.class) {
     return config.getMemorySize(configPropName);
   } else if (parameterClass == Object.class) {
     return config.getAnyRef(configPropName);
   } else if (parameterClass == List.class) {
     return getListValue(beanClass, parameterType, parameterClass, config, configPropName);
   } else if (parameterClass == Map.class) {
     // we could do better here, but right now we don't.
     Type[] typeArgs = ((ParameterizedType) parameterType).getActualTypeArguments();
     if (typeArgs[0] != String.class || typeArgs[1] != Object.class) {
       throw new ConfigException.BadBean(
           "Bean property '"
               + configPropName
               + "' of class "
               + beanClass.getName()
               + " has unsupported Map<"
               + typeArgs[0]
               + ","
               + typeArgs[1]
               + ">, only Map<String,Object> is supported right now");
     }
     return config.getObject(configPropName).unwrapped();
   } else if (parameterClass == Config.class) {
     return config.getConfig(configPropName);
   } else if (parameterClass == ConfigObject.class) {
     return config.getObject(configPropName);
   } else if (parameterClass == ConfigValue.class) {
     return config.getValue(configPropName);
   } else if (parameterClass == ConfigList.class) {
     return config.getList(configPropName);
   } else if (hasAtLeastOneBeanProperty(parameterClass)) {
     return createInternal(config.getConfig(configPropName), parameterClass);
   } else {
     throw new ConfigException.BadBean(
         "Bean property "
             + configPropName
             + " of class "
             + beanClass.getName()
             + " has unsupported type "
             + parameterType);
   }
 }
예제 #2
0
  private static Object getListValue(
      Class<?> beanClass,
      Type parameterType,
      Class<?> parameterClass,
      Config config,
      String configPropName) {
    Type elementType = ((ParameterizedType) parameterType).getActualTypeArguments()[0];

    if (elementType == Boolean.class) {
      return config.getBooleanList(configPropName);
    } else if (elementType == Integer.class) {
      return config.getIntList(configPropName);
    } else if (elementType == Double.class) {
      return config.getDoubleList(configPropName);
    } else if (elementType == Long.class) {
      return config.getLongList(configPropName);
    } else if (elementType == String.class) {
      return config.getStringList(configPropName);
    } else if (elementType == Duration.class) {
      return config.getDurationList(configPropName);
    } else if (elementType == ConfigMemorySize.class) {
      return config.getMemorySizeList(configPropName);
    } else if (elementType == Object.class) {
      return config.getAnyRefList(configPropName);
    } else if (elementType == Config.class) {
      return config.getConfigList(configPropName);
    } else if (elementType == ConfigObject.class) {
      return config.getObjectList(configPropName);
    } else if (elementType == ConfigValue.class) {
      return config.getList(configPropName);
    } else if (hasAtLeastOneBeanProperty((Class<?>) elementType)) {
      List<Object> beanList = new ArrayList<Object>();
      List<? extends Config> configList = config.getConfigList(configPropName);
      for (Config listMember : configList) {
        beanList.add(createInternal(listMember, (Class<?>) elementType));
      }
      return beanList;
    } else {
      throw new ConfigException.BadBean(
          "Bean property '"
              + configPropName
              + "' of class "
              + beanClass.getName()
              + " has unsupported list element type "
              + elementType);
    }
  }