Search in sources :

Example 1 with ConfigAttributeValue

use of com.thoughtworks.go.config.ConfigAttributeValue in project gocd by gocd.

the class GoConfigFieldLoader method setValue.

private void setValue(Object val) {
    try {
        ConfigAttributeValue configAttributeValue = field.getType().getAnnotation(ConfigAttributeValue.class);
        if (configAttributeValue != null) {
            if (val != null || configAttributeValue.createForNull()) {
                Constructor<?> constructor = field.getType().getConstructor(String.class);
                field.set(instance, constructor.newInstance(new Object[] { val }));
            }
        } else if (val != null) {
            Object convertedValue = typeConverter.convertIfNecessary(val, field.getType());
            field.set(instance, convertedValue);
        }
    } catch (IllegalAccessException e) {
        throw bomb("Error setting configField: " + field.getName(), e);
    } catch (TypeMismatchException e) {
        final String message = format("Could not set value [{0}] on Field [{1}] of type [{2}] ", val, field.getName(), field.getType());
        throw bomb(message, e);
    } catch (NoSuchMethodException e) {
        throw bomb("Error setting configField: " + field.getName() + " as " + field.getType(), e);
    } catch (InstantiationException e) {
        throw bomb("Error creating configAttribute: " + field.getName() + " as " + field.getType(), e);
    } catch (InvocationTargetException e) {
        throw bomb("Error creating configAttribute: " + field.getName() + " as " + field.getType(), e);
    }
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) ConfigAttributeValue(com.thoughtworks.go.config.ConfigAttributeValue) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ConfigAttributeValue (com.thoughtworks.go.config.ConfigAttributeValue)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1