use of org.apache.knox.gateway.config.Optional in project knox by apache.
the class DefaultConfigurationInjector method injectFieldValue.
private void injectFieldValue(Field field, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding) throws ConfigurationException {
Configure annotation = field.getAnnotation(Configure.class);
if (annotation != null) {
Alias alias = field.getAnnotation(Alias.class);
String name = getConfigName(field, alias);
String bind = getBindName(target, name, binding);
Object value = retrieveValue(target, bind, name, field.getType(), adapter, binding);
if (value == null) {
Optional optional = field.getAnnotation(Optional.class);
if (optional == null) {
throw new ConfigurationException(String.format("Failed to find configuration for %s bound to %s of %s via %s", bind, name, target.getClass().getName(), adapter.getClass().getName()));
}
} else {
try {
if (!field.isAccessible()) {
field.setAccessible(true);
}
field.set(target, value);
} catch (Exception e) {
throw new ConfigurationException(String.format("Failed to inject field configuration property %s of %s", name, target.getClass().getName()), e);
}
}
}
}
Aggregations