@Test public void testFillValues() { List<MConfig> configs = getConfigs(); ((MStringInput) configs.get(0).getInputs().get(0)).setValue("value"); TestConfiguration config = new TestConfiguration(); ConfigUtils.fromConfigs(configs, config); AssertJUnit.assertEquals("value", config.aConfig.a1); }
@SuppressWarnings("unchecked") public static void fillValues(String json, Object configuration) { Class<?> klass = configuration.getClass(); Set<String> configNames = new HashSet<String>(); JSONObject jsonConfigs = JSONUtils.parse(json); for(Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); String configName = configField.getName(); Config configAnnotation = configField.getAnnotation(Config.class); if(configAnnotation == null) { continue; } configName = getConfigName(configField, configAnnotation, configNames); try { configField.set(configuration, configField.getType().newInstance()); } catch (Exception e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } JSONObject jsonInputs = (JSONObject) jsonConfigs.get(configField.getName()); if(jsonInputs == null) { continue; } Object configValue; try { configValue = configField.get(configuration); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } for(Field inputField : configField.getType().getDeclaredFields()) { inputField.setAccessible(true); String inputName = inputField.getName(); Input inputAnnotation = inputField.getAnnotation(Input.class); if(inputAnnotation == null || jsonInputs.get(inputName) == null) { try { inputField.set(configValue, null); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } continue; } Class<?> type = inputField.getType(); try { if(type == String.class) { inputField.set(configValue, jsonInputs.get(inputName)); } else if (type.isAssignableFrom(Map.class)) { Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = (JSONObject) jsonInputs.get(inputName); for(Object key : jsonObject.keySet()) { map.put((String)key, (String)jsonObject.get(key)); } inputField.set(configValue, map); } else if(type == Integer.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).intValue()); } else if(type == Long.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).longValue()); } else if(type.isEnum()) { inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName))); } else if(type == Boolean.class) { inputField.set(configValue, (Boolean) jsonInputs.get(inputName)); }else { throw new SqoopException(ModelError.MODEL_004, "Unsupported type " + type.getName() + " for input " + configName + "." + inputName); } } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } } } }
ConfigUtils { @SuppressWarnings("unchecked") public static void fillValues(String json, Object configuration) { Class<?> klass = configuration.getClass(); Set<String> configNames = new HashSet<String>(); JSONObject jsonConfigs = JSONUtils.parse(json); for(Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); String configName = configField.getName(); Config configAnnotation = configField.getAnnotation(Config.class); if(configAnnotation == null) { continue; } configName = getConfigName(configField, configAnnotation, configNames); try { configField.set(configuration, configField.getType().newInstance()); } catch (Exception e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } JSONObject jsonInputs = (JSONObject) jsonConfigs.get(configField.getName()); if(jsonInputs == null) { continue; } Object configValue; try { configValue = configField.get(configuration); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } for(Field inputField : configField.getType().getDeclaredFields()) { inputField.setAccessible(true); String inputName = inputField.getName(); Input inputAnnotation = inputField.getAnnotation(Input.class); if(inputAnnotation == null || jsonInputs.get(inputName) == null) { try { inputField.set(configValue, null); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } continue; } Class<?> type = inputField.getType(); try { if(type == String.class) { inputField.set(configValue, jsonInputs.get(inputName)); } else if (type.isAssignableFrom(Map.class)) { Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = (JSONObject) jsonInputs.get(inputName); for(Object key : jsonObject.keySet()) { map.put((String)key, (String)jsonObject.get(key)); } inputField.set(configValue, map); } else if(type == Integer.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).intValue()); } else if(type == Long.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).longValue()); } else if(type.isEnum()) { inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName))); } else if(type == Boolean.class) { inputField.set(configValue, (Boolean) jsonInputs.get(inputName)); }else { throw new SqoopException(ModelError.MODEL_004, "Unsupported type " + type.getName() + " for input " + configName + "." + inputName); } } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } } } } }
ConfigUtils { @SuppressWarnings("unchecked") public static void fillValues(String json, Object configuration) { Class<?> klass = configuration.getClass(); Set<String> configNames = new HashSet<String>(); JSONObject jsonConfigs = JSONUtils.parse(json); for(Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); String configName = configField.getName(); Config configAnnotation = configField.getAnnotation(Config.class); if(configAnnotation == null) { continue; } configName = getConfigName(configField, configAnnotation, configNames); try { configField.set(configuration, configField.getType().newInstance()); } catch (Exception e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } JSONObject jsonInputs = (JSONObject) jsonConfigs.get(configField.getName()); if(jsonInputs == null) { continue; } Object configValue; try { configValue = configField.get(configuration); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } for(Field inputField : configField.getType().getDeclaredFields()) { inputField.setAccessible(true); String inputName = inputField.getName(); Input inputAnnotation = inputField.getAnnotation(Input.class); if(inputAnnotation == null || jsonInputs.get(inputName) == null) { try { inputField.set(configValue, null); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } continue; } Class<?> type = inputField.getType(); try { if(type == String.class) { inputField.set(configValue, jsonInputs.get(inputName)); } else if (type.isAssignableFrom(Map.class)) { Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = (JSONObject) jsonInputs.get(inputName); for(Object key : jsonObject.keySet()) { map.put((String)key, (String)jsonObject.get(key)); } inputField.set(configValue, map); } else if(type == Integer.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).intValue()); } else if(type == Long.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).longValue()); } else if(type.isEnum()) { inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName))); } else if(type == Boolean.class) { inputField.set(configValue, (Boolean) jsonInputs.get(inputName)); }else { throw new SqoopException(ModelError.MODEL_004, "Unsupported type " + type.getName() + " for input " + configName + "." + inputName); } } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } } } }  }
ConfigUtils { @SuppressWarnings("unchecked") public static void fillValues(String json, Object configuration) { Class<?> klass = configuration.getClass(); Set<String> configNames = new HashSet<String>(); JSONObject jsonConfigs = JSONUtils.parse(json); for(Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); String configName = configField.getName(); Config configAnnotation = configField.getAnnotation(Config.class); if(configAnnotation == null) { continue; } configName = getConfigName(configField, configAnnotation, configNames); try { configField.set(configuration, configField.getType().newInstance()); } catch (Exception e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } JSONObject jsonInputs = (JSONObject) jsonConfigs.get(configField.getName()); if(jsonInputs == null) { continue; } Object configValue; try { configValue = configField.get(configuration); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } for(Field inputField : configField.getType().getDeclaredFields()) { inputField.setAccessible(true); String inputName = inputField.getName(); Input inputAnnotation = inputField.getAnnotation(Input.class); if(inputAnnotation == null || jsonInputs.get(inputName) == null) { try { inputField.set(configValue, null); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } continue; } Class<?> type = inputField.getType(); try { if(type == String.class) { inputField.set(configValue, jsonInputs.get(inputName)); } else if (type.isAssignableFrom(Map.class)) { Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = (JSONObject) jsonInputs.get(inputName); for(Object key : jsonObject.keySet()) { map.put((String)key, (String)jsonObject.get(key)); } inputField.set(configValue, map); } else if(type == Integer.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).intValue()); } else if(type == Long.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).longValue()); } else if(type.isEnum()) { inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName))); } else if(type == Boolean.class) { inputField.set(configValue, (Boolean) jsonInputs.get(inputName)); }else { throw new SqoopException(ModelError.MODEL_004, "Unsupported type " + type.getName() + " for input " + configName + "." + inputName); } } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } } } }  static List<MConfig> toConfigs(Object configuration); static List<MConfig> toConfigs(Class<?> klass); static List<MConfig> toConfigs(Class<?> klass, Object configuration); static ConfigValidationResult validateConfigs(List<MConfig> configs, Class configClass); static Object fromConfigs(List<MConfig> configs, Class configClass); @SuppressWarnings("unchecked") static void fromConfigs(List<MConfig> configs, Object configuration); static void applyValidation(List<MConfig> configs, ConfigValidationResult result); static void applyValidation(MValidatedElement element, ConfigValidationResult result); @SuppressWarnings("unchecked") static String toJson(Object configuration); @SuppressWarnings("unchecked") static void fillValues(String json, Object configuration); static String getName(Field input, Input annotation); static String getName(Field config, Config annotation); static ConfigurationClass getConfigurationClassAnnotation(Object object, boolean strict); static ConfigClass getConfigClassAnnotation(Object object, boolean strict); static Config getConfigAnnotation(Field field, boolean strict); static Input getInputAnnotation(Field field, boolean strict); static Object getFieldValue(Field field, Object object); static void validateInputOverridesAttribute(MInput<?> currentInput, MConfig config); }
ConfigUtils { @SuppressWarnings("unchecked") public static void fillValues(String json, Object configuration) { Class<?> klass = configuration.getClass(); Set<String> configNames = new HashSet<String>(); JSONObject jsonConfigs = JSONUtils.parse(json); for(Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); String configName = configField.getName(); Config configAnnotation = configField.getAnnotation(Config.class); if(configAnnotation == null) { continue; } configName = getConfigName(configField, configAnnotation, configNames); try { configField.set(configuration, configField.getType().newInstance()); } catch (Exception e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } JSONObject jsonInputs = (JSONObject) jsonConfigs.get(configField.getName()); if(jsonInputs == null) { continue; } Object configValue; try { configValue = configField.get(configuration); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } for(Field inputField : configField.getType().getDeclaredFields()) { inputField.setAccessible(true); String inputName = inputField.getName(); Input inputAnnotation = inputField.getAnnotation(Input.class); if(inputAnnotation == null || jsonInputs.get(inputName) == null) { try { inputField.set(configValue, null); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } continue; } Class<?> type = inputField.getType(); try { if(type == String.class) { inputField.set(configValue, jsonInputs.get(inputName)); } else if (type.isAssignableFrom(Map.class)) { Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = (JSONObject) jsonInputs.get(inputName); for(Object key : jsonObject.keySet()) { map.put((String)key, (String)jsonObject.get(key)); } inputField.set(configValue, map); } else if(type == Integer.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).intValue()); } else if(type == Long.class) { inputField.set(configValue, ((Long)jsonInputs.get(inputName)).longValue()); } else if(type.isEnum()) { inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName))); } else if(type == Boolean.class) { inputField.set(configValue, (Boolean) jsonInputs.get(inputName)); }else { throw new SqoopException(ModelError.MODEL_004, "Unsupported type " + type.getName() + " for input " + configName + "." + inputName); } } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } } } }  static List<MConfig> toConfigs(Object configuration); static List<MConfig> toConfigs(Class<?> klass); static List<MConfig> toConfigs(Class<?> klass, Object configuration); static ConfigValidationResult validateConfigs(List<MConfig> configs, Class configClass); static Object fromConfigs(List<MConfig> configs, Class configClass); @SuppressWarnings("unchecked") static void fromConfigs(List<MConfig> configs, Object configuration); static void applyValidation(List<MConfig> configs, ConfigValidationResult result); static void applyValidation(MValidatedElement element, ConfigValidationResult result); @SuppressWarnings("unchecked") static String toJson(Object configuration); @SuppressWarnings("unchecked") static void fillValues(String json, Object configuration); static String getName(Field input, Input annotation); static String getName(Field config, Config annotation); static ConfigurationClass getConfigurationClassAnnotation(Object object, boolean strict); static ConfigClass getConfigClassAnnotation(Object object, boolean strict); static Config getConfigAnnotation(Field field, boolean strict); static Input getInputAnnotation(Field field, boolean strict); static Object getFieldValue(Field field, Object object); static void validateInputOverridesAttribute(MInput<?> currentInput, MConfig config);  }