cz.jalasoft.util.configuration.PropertyInvocationHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JalasoftUtils Show documentation
Show all versions of JalasoftUtils Show documentation
A collection of utility classes that might be useful.
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);
}
}