Method setter = ReflectionUtil.findSetter(configClass, field);
if (setter == null) {
throw new ConfigException(String.format("FATAL: Unable to find setter for %s in class %s", field.getName(), configClass.getName()));
}
IParser parser = null;
if (field.isAnnotationPresent(Parser.class)) {
Class<? extends IParser> parserClass = field.getAnnotation(Parser.class).value();
parser = parserClass.newInstance();
}
Class<?> fieldType = field.getType();
hasSet.add(field.getName());
if (fieldType.isPrimitive() || String.class.equals(fieldType) || fieldType.isEnum()) {
setter.invoke(configObject,
parser != null ? parser.apply(rawConf.getAnyRef(field.getName())) : parseAndCheck(path + "." + field.getName(), rawConf.getAnyRef(field.getName()), fieldType));
}
else if (fieldType.isArray()) {
Class<?> elementType = fieldType.getComponentType();
List<?> rawValues = rawConf.getAnyRefList(field.getName());
Object values = Array.newInstance(elementType, rawValues.size());
int idx = 0;
for (Object rv: rawValues) {
Array.set(values, idx,
parser != null ? parser.apply(rv) : parseAndCheck(String.format("%s.[%d]", path, idx), rv, elementType));
idx++;
}
setter.invoke(configObject, values);
}
else if (field.isAnnotationPresent(MapParam.class)) {
HashMap<Object, Object> map = new HashMap<Object, Object>();
Class<?> valueClass = field.getAnnotation(MapParam.class).value();
for (Map.Entry<String, ConfigValue> entry : rawConf.getObject(field.getName()).entrySet()) {
map.put(entry.getKey(),
parser != null ? parser.apply(entry.getValue()) : parseAndCheck(path + "." + field.getName() + "." + entry.getKey(), entry.getValue(), valueClass));
}
setter.invoke(configObject, map);
}
else if (fieldType.isAnnotationPresent(ConfigObjectClass.class)) {
setter.invoke(configObject, parseAndCheck(path + "." + field.getName(), rawConf.getConfig(field.getName()), fieldType));
}
else if (parser != null) {
setter.invoke(configObject, parser.apply(rawConf.getAnyRef(field.getName())));
}
else {
throw new ConfigException(String.format("Field %s with unknown type %s at path %s", field.getName(), fieldType.getSimpleName(), path));
}
}