Search in sources :

Example 1 with TypeStringify

use of com.haulmont.cuba.core.config.type.TypeStringify in project cuba by cuba-platform.

the class ConfigUtil method getDefaultValue.

/**
 * Get the default value of a configuration interface method. If a
 * {@link Default} annotation is present then that string is converted
 * to the appropriate type using the {@link TypeFactory} class.
 * Otherwise, for the type Foo, this searches for a FooDefault
 * annotation. If such an annotation is present then its value is
 * returned.
 *
 * @param configInterface The configuration interface.
 * @param method          The method.
 * @return The default value, or null.
 */
public static String getDefaultValue(Class<?> configInterface, Method method) {
    // TODO: returnType.cast()?
    try {
        Default defaultValue = method.getAnnotation(Default.class);
        if (defaultValue != null) {
            return defaultValue.value();
        } else {
            Class<?> type = method.getReturnType();
            if (EnumClass.class.isAssignableFrom(type)) {
                @SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) type;
                EnumStore mode = getAnnotation(configInterface, method, EnumStore.class, true);
                if (mode != null && EnumStoreMode.ID == mode.value()) {
                    Class<?> idType = getEnumIdType(enumeration);
                    String name = "Default" + StringUtils.capitalize(ClassUtils.getShortClassName(idType));
                    Object value = getAnnotationValue(method, name);
                    if (value != null) {
                        Method fromId = enumeration.getDeclaredMethod("fromId", idType);
                        TypeStringify stringConverter = TypeStringify.getInstance(configInterface, method);
                        return stringConverter.stringify(fromId.invoke(null, value));
                    }
                    return NO_DEFAULT;
                }
            }
            String name = "Default" + StringUtils.capitalize(ClassUtils.getShortClassName(type));
            Object value = getAnnotationValue(method, name);
            if (value != null) {
                TypeStringify stringConverter = TypeStringify.getInstance(configInterface, method);
                return stringConverter.stringify(value);
            }
        }
        return NO_DEFAULT;
    } catch (Exception ex) {
        throw new RuntimeException("Default value error", ex);
    }
}
Also used : Method(java.lang.reflect.Method) Default(com.haulmont.cuba.core.config.defaults.Default) TypeStringify(com.haulmont.cuba.core.config.type.TypeStringify) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Aggregations

EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 Default (com.haulmont.cuba.core.config.defaults.Default)1 TypeStringify (com.haulmont.cuba.core.config.type.TypeStringify)1 Method (java.lang.reflect.Method)1