use of org.gradle.api.NamedDomainObjectContainer in project atlas by alibaba.
the class AtlasConfigHelper method setProperty.
public static void setProperty(Object object, String fieldName, String value) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException {
String[] fieldNames = fieldName.split("\\.");
Object last = object;
for (int i = 0; i < fieldNames.length - 1; i++) {
String field = fieldNames[i];
if (last instanceof NamedDomainObjectContainer) {
last = ((NamedDomainObjectContainer) last).maybeCreate(field);
} else {
Field declaredField = last.getClass().getField(field);
declaredField.setAccessible(true);
if (null == declaredField.get(last)) {
Object newInstance = declaredField.getType().getConstructors().getClass().newInstance();
declaredField.set(last, newInstance);
}
last = declaredField.get(last);
}
}
BeanUtils.setProperty(last, fieldNames[fieldNames.length - 1], value);
}
use of org.gradle.api.NamedDomainObjectContainer in project atlas by alibaba.
the class AtlasConfigHelper method readConfig.
private static void readConfig(Object object, String prefix, List<AtlasConfigField> configFieldList, int groupOrder, String variantName) throws IllegalAccessException {
if (null == object) {
return;
}
for (Field field : getAllFields(object.getClass())) {
field.setAccessible(true);
Config config = field.getAnnotation(Config.class);
if (null != config) {
AtlasConfigField configField = new AtlasConfigField();
configField.name = prefix + "." + field.getName();
configField.order = config.order();
configField.desc = config.message();
Object obj = field.get(object);
configField.value = (null == obj ? null : String.valueOf(obj));
configField.groupOrder = groupOrder;
configField.variantName = variantName;
configField.type = field.getType().getSimpleName();
configField.advanced = config.advance();
configFieldList.add(configField);
continue;
}
ConfigGroup configGroup = field.getAnnotation(ConfigGroup.class);
if (null != configGroup) {
Object nestedValue = field.get(object);
if (nestedValue instanceof NamedDomainObjectContainer) {
readConfig(((NamedDomainObjectContainer) nestedValue).maybeCreate("debug"), prefix + "." + field.getName() + ".debug", configFieldList, configGroup.order(), "debug");
readConfig(((NamedDomainObjectContainer) nestedValue).maybeCreate("release"), prefix + "." + field.getName() + ".release", configFieldList, configGroup.order(), "release");
} else {
readConfig(nestedValue, prefix + "." + field.getName(), configFieldList, configGroup.order(), "");
}
}
}
}
Aggregations