Search in sources :

Example 1 with NamedDomainObjectContainer

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);
}
Also used : NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) Field(java.lang.reflect.Field)

Example 2 with NamedDomainObjectContainer

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(), "");
            }
        }
    }
}
Also used : NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) Field(java.lang.reflect.Field) Config(com.taobao.android.builder.extension.annotation.Config) ConfigGroup(com.taobao.android.builder.extension.annotation.ConfigGroup)

Aggregations

Field (java.lang.reflect.Field)2 NamedDomainObjectContainer (org.gradle.api.NamedDomainObjectContainer)2 Config (com.taobao.android.builder.extension.annotation.Config)1 ConfigGroup (com.taobao.android.builder.extension.annotation.ConfigGroup)1