com.tngtech.configbuilder.util.FieldValueExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of config-builder Show documentation
Show all versions of config-builder Show documentation
The Config Builder creates fully configured instances of config classes, using values from various sources like properties files, command line arguments etc.
package com.tngtech.configbuilder.util;
import com.tngtech.configbuilder.annotation.configuration.LoadingOrder;
import com.tngtech.configbuilder.annotation.valueextractor.ValueExtractorProcessor;
import com.tngtech.configbuilder.annotation.valueextractor.ValueExtractorAnnotation;
import com.tngtech.configbuilder.configuration.BuilderConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class FieldValueExtractor {
private final static Logger log = LoggerFactory.getLogger(FieldValueExtractor.class);
private final AnnotationHelper annotationHelper;
private final ConfigBuilderFactory configBuilderFactory;
public FieldValueExtractor(ConfigBuilderFactory configBuilderFactory) {
this.configBuilderFactory = configBuilderFactory;
this.annotationHelper = configBuilderFactory.getInstance(AnnotationHelper.class);
}
public Object extractValue(Field field, BuilderConfiguration builderConfiguration) {
Object value = null;
Class extends Annotation>[] annotationOrderOfField = field.isAnnotationPresent(LoadingOrder.class) ? field.getAnnotation(LoadingOrder.class).value() : builderConfiguration.getAnnotationOrder();
Class extends ValueExtractorProcessor> processor;
for (Annotation annotation : annotationHelper.getAnnotationsInOrder(field, annotationOrderOfField)) {
log.debug("trying to find a value for field {} with {} annotation", field.getName(), annotation.annotationType());
processor = annotation.annotationType().getAnnotation(ValueExtractorAnnotation.class).value();
value = configBuilderFactory.getInstance(processor).getValue(annotation, configBuilderFactory);
if (value != null) {
log.debug("found value \"{}\" for field {} from {} annotation", value, field.getName(), annotation.annotationType());
break;
}
}
return value;
}
}