@Test public void testApplyCustomSettingsForFields() { DerivedModelWithFields model = new DerivedModelWithFields(); AppConfigStorageItem overrides = new AppConfigStorageItem(); overrides.putString("simpleEnumField", SimpleEnum.Simple.toString()); overrides.putString("complexEnumField", ComplexEnum.EnumValues.toString()); overrides.putString("stringField", "Override test"); overrides.putLong("longField", 83480); overrides.putInt("intField", -19028234); overrides.putBoolean("boolField", false); model.applyCustomSettings("", overrides); Assert.assertEquals(SimpleEnum.Simple, model.simpleEnumField); Assert.assertEquals(ComplexEnum.EnumValues, model.complexEnumField); Assert.assertEquals("Override test", model.stringField); Assert.assertEquals((long)83480, model.longField); Assert.assertEquals(-19028234, model.intField); Assert.assertEquals(false, model.boolField); }
public void applyCustomSettings(String configName, AppConfigStorageItem item) { Method[] methods = getClass().getDeclaredMethods(); boolean foundSetter = false; for (Method method : methods) { method.setAccessible(true); if (method.isAccessible()) { if (method.getName().startsWith("set") && method.getGenericParameterTypes().length == 1) { String value = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); if (value.equals("name")) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(String.class) && configName.length() > 0) { method.invoke(this, configName); } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } else if (item.get(value) != null) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { method.invoke(this, item.getBoolean(value)); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { method.invoke(this, item.getInt(value)); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { method.invoke(this, item.getLong(value)); } else if (parameterType.equals(String.class)) { method.invoke(this, item.getStringNotNull(value)); } else if (parameterType instanceof Class && ((Class)parameterType).isEnum()) { Object enumTypes[] = ((Class)parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(value); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { method.invoke(this, enumType); break; } } } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } foundSetter = true; } } } if (!foundSetter) { Field[] fields = getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.isAccessible()) { if (field.getName().equals("name")) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(String.class) && configName.length() > 0) { field.set(this, configName); } } catch (IllegalAccessException ignored) { } } if (item.get(field.getName()) != null) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { field.setBoolean(this, item.getBoolean(field.getName())); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { field.setInt(this, item.getInt(field.getName())); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { field.setLong(this, item.getLong(field.getName())); } else if (parameterType.equals(String.class)) { field.set(this, item.getStringNotNull(field.getName())); } else if (parameterType instanceof Class && ((Class) parameterType).isEnum()) { Object enumTypes[] = ((Class) parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(field.getName()); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { field.set(this, enumType); break; } } } } catch (IllegalAccessException ignored) { } } } } } }
AppConfigBaseModel { public void applyCustomSettings(String configName, AppConfigStorageItem item) { Method[] methods = getClass().getDeclaredMethods(); boolean foundSetter = false; for (Method method : methods) { method.setAccessible(true); if (method.isAccessible()) { if (method.getName().startsWith("set") && method.getGenericParameterTypes().length == 1) { String value = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); if (value.equals("name")) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(String.class) && configName.length() > 0) { method.invoke(this, configName); } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } else if (item.get(value) != null) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { method.invoke(this, item.getBoolean(value)); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { method.invoke(this, item.getInt(value)); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { method.invoke(this, item.getLong(value)); } else if (parameterType.equals(String.class)) { method.invoke(this, item.getStringNotNull(value)); } else if (parameterType instanceof Class && ((Class)parameterType).isEnum()) { Object enumTypes[] = ((Class)parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(value); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { method.invoke(this, enumType); break; } } } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } foundSetter = true; } } } if (!foundSetter) { Field[] fields = getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.isAccessible()) { if (field.getName().equals("name")) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(String.class) && configName.length() > 0) { field.set(this, configName); } } catch (IllegalAccessException ignored) { } } if (item.get(field.getName()) != null) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { field.setBoolean(this, item.getBoolean(field.getName())); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { field.setInt(this, item.getInt(field.getName())); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { field.setLong(this, item.getLong(field.getName())); } else if (parameterType.equals(String.class)) { field.set(this, item.getStringNotNull(field.getName())); } else if (parameterType instanceof Class && ((Class) parameterType).isEnum()) { Object enumTypes[] = ((Class) parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(field.getName()); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { field.set(this, enumType); break; } } } } catch (IllegalAccessException ignored) { } } } } } } }
AppConfigBaseModel { public void applyCustomSettings(String configName, AppConfigStorageItem item) { Method[] methods = getClass().getDeclaredMethods(); boolean foundSetter = false; for (Method method : methods) { method.setAccessible(true); if (method.isAccessible()) { if (method.getName().startsWith("set") && method.getGenericParameterTypes().length == 1) { String value = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); if (value.equals("name")) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(String.class) && configName.length() > 0) { method.invoke(this, configName); } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } else if (item.get(value) != null) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { method.invoke(this, item.getBoolean(value)); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { method.invoke(this, item.getInt(value)); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { method.invoke(this, item.getLong(value)); } else if (parameterType.equals(String.class)) { method.invoke(this, item.getStringNotNull(value)); } else if (parameterType instanceof Class && ((Class)parameterType).isEnum()) { Object enumTypes[] = ((Class)parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(value); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { method.invoke(this, enumType); break; } } } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } foundSetter = true; } } } if (!foundSetter) { Field[] fields = getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.isAccessible()) { if (field.getName().equals("name")) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(String.class) && configName.length() > 0) { field.set(this, configName); } } catch (IllegalAccessException ignored) { } } if (item.get(field.getName()) != null) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { field.setBoolean(this, item.getBoolean(field.getName())); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { field.setInt(this, item.getInt(field.getName())); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { field.setLong(this, item.getLong(field.getName())); } else if (parameterType.equals(String.class)) { field.set(this, item.getStringNotNull(field.getName())); } else if (parameterType instanceof Class && ((Class) parameterType).isEnum()) { Object enumTypes[] = ((Class) parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(field.getName()); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { field.set(this, enumType); break; } } } } catch (IllegalAccessException ignored) { } } } } } }  }
AppConfigBaseModel { public void applyCustomSettings(String configName, AppConfigStorageItem item) { Method[] methods = getClass().getDeclaredMethods(); boolean foundSetter = false; for (Method method : methods) { method.setAccessible(true); if (method.isAccessible()) { if (method.getName().startsWith("set") && method.getGenericParameterTypes().length == 1) { String value = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); if (value.equals("name")) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(String.class) && configName.length() > 0) { method.invoke(this, configName); } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } else if (item.get(value) != null) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { method.invoke(this, item.getBoolean(value)); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { method.invoke(this, item.getInt(value)); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { method.invoke(this, item.getLong(value)); } else if (parameterType.equals(String.class)) { method.invoke(this, item.getStringNotNull(value)); } else if (parameterType instanceof Class && ((Class)parameterType).isEnum()) { Object enumTypes[] = ((Class)parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(value); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { method.invoke(this, enumType); break; } } } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } foundSetter = true; } } } if (!foundSetter) { Field[] fields = getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.isAccessible()) { if (field.getName().equals("name")) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(String.class) && configName.length() > 0) { field.set(this, configName); } } catch (IllegalAccessException ignored) { } } if (item.get(field.getName()) != null) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { field.setBoolean(this, item.getBoolean(field.getName())); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { field.setInt(this, item.getInt(field.getName())); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { field.setLong(this, item.getLong(field.getName())); } else if (parameterType.equals(String.class)) { field.set(this, item.getStringNotNull(field.getName())); } else if (parameterType instanceof Class && ((Class) parameterType).isEnum()) { Object enumTypes[] = ((Class) parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(field.getName()); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { field.set(this, enumType); break; } } } } catch (IllegalAccessException ignored) { } } } } } }  ArrayList<String> configurationValueList(); ArrayList<String> globalValueList(); ArrayList<String> getConfigurationCategories(); ArrayList<String> getGlobalCategories(); boolean valueBelongsToCategory(String value, String category); Object getCurrentValue(String value); void applyCustomSettings(String configName, AppConfigStorageItem item); }
AppConfigBaseModel { public void applyCustomSettings(String configName, AppConfigStorageItem item) { Method[] methods = getClass().getDeclaredMethods(); boolean foundSetter = false; for (Method method : methods) { method.setAccessible(true); if (method.isAccessible()) { if (method.getName().startsWith("set") && method.getGenericParameterTypes().length == 1) { String value = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); if (value.equals("name")) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(String.class) && configName.length() > 0) { method.invoke(this, configName); } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } else if (item.get(value) != null) { try { Type parameterType = method.getGenericParameterTypes()[0]; if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { method.invoke(this, item.getBoolean(value)); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { method.invoke(this, item.getInt(value)); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { method.invoke(this, item.getLong(value)); } else if (parameterType.equals(String.class)) { method.invoke(this, item.getStringNotNull(value)); } else if (parameterType instanceof Class && ((Class)parameterType).isEnum()) { Object enumTypes[] = ((Class)parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(value); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { method.invoke(this, enumType); break; } } } } catch (InvocationTargetException ignored) { } catch (IllegalAccessException ignored) { } } foundSetter = true; } } } if (!foundSetter) { Field[] fields = getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.isAccessible()) { if (field.getName().equals("name")) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(String.class) && configName.length() > 0) { field.set(this, configName); } } catch (IllegalAccessException ignored) { } } if (item.get(field.getName()) != null) { try { Type parameterType = field.getGenericType(); if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { field.setBoolean(this, item.getBoolean(field.getName())); } else if (parameterType.equals(Integer.class) || parameterType.equals(int.class)) { field.setInt(this, item.getInt(field.getName())); } else if (parameterType.equals(Long.class) || parameterType.equals(long.class)) { field.setLong(this, item.getLong(field.getName())); } else if (parameterType.equals(String.class)) { field.set(this, item.getStringNotNull(field.getName())); } else if (parameterType instanceof Class && ((Class) parameterType).isEnum()) { Object enumTypes[] = ((Class) parameterType).getEnumConstants(); String stringValue = item.getStringNotNull(field.getName()); for (Object enumType : enumTypes) { if (enumType.toString().equals(stringValue)) { field.set(this, enumType); break; } } } } catch (IllegalAccessException ignored) { } } } } } }  ArrayList<String> configurationValueList(); ArrayList<String> globalValueList(); ArrayList<String> getConfigurationCategories(); ArrayList<String> getGlobalCategories(); boolean valueBelongsToCategory(String value, String category); Object getCurrentValue(String value); void applyCustomSettings(String configName, AppConfigStorageItem item);  }