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

cz.jalasoft.util.configuration.PropertyInvocationHandler Maven / Gradle / Ivy

The newest version!
package cz.jalasoft.util.configuration;

import cz.jalasoft.util.configuration.annotation.Key;
import cz.jalasoft.util.converter.string.StringConverter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author Honza Lastovicka ([email protected])
 * @since 2016-07-26.
 */
final class PropertyInvocationHandler implements InvocationHandler {

    private final LifeConfig config;

    PropertyInvocationHandler(LifeConfig config) {
        this.config = config;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (!methodOriginsInPretender(method)) {
            //handle toString
            return null;
        }

        if (config.isLife) {
            config.provider.reload(config.source);
        }

        String key = config.keyExtractor.key(method);
        String value = config.provider.readProperty(key);

        if (value == null) {
            return null;
        }

        Class returnType = method.getReturnType();
        StringConverter converter = config.converterRegistry.converter(returnType);

        Object typedValue = converter.convert(value);
        return typedValue;
    }

    private boolean methodOriginsInPretender(Method method) {
        return method.getDeclaringClass().equals(config.type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy