All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tngtech.configbuilder.util.FieldValueExtractor Maven / Gradle / Ivy

Go to download

The Config Builder creates fully configured instances of config classes, using values from various sources like properties files, command line arguments etc.

There is a newer version: 1.8.1
Show newest version
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[] annotationOrderOfField = field.isAnnotationPresent(LoadingOrder.class) ? field.getAnnotation(LoadingOrder.class).value() : builderConfiguration.getAnnotationOrder();
        Class 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy