org.aeonbits.owner.PropertiesMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of owner Show documentation
Show all versions of owner Show documentation
Get rid of the boilerplate code in Java properties based configuration.
/*
* Copyright (c) 2012-2015, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/
package org.aeonbits.owner;
import org.aeonbits.owner.Config.DefaultValue;
import org.aeonbits.owner.Config.EncryptedValue;
import org.aeonbits.owner.Config.Key;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* Maps methods to properties keys and defaultValues. Maps a class to default property values.
*
* @author Luigi R. Viggiano
*/
final class PropertiesMapper {
/** Don't let anyone instantiate this class */
private PropertiesMapper() {}
static boolean isEncryptedValue( Method method ) {
return ( method.getAnnotation( EncryptedValue.class ) ) != null;
}
static String key(Method method) {
Key key = method.getAnnotation(Key.class);
return (key == null) ? method.getName() : key.value();
}
static String defaultValue(Method method) {
DefaultValue defaultValue = method.getAnnotation(DefaultValue.class);
return defaultValue != null ? defaultValue.value() : null;
}
static void defaults(Properties properties, Class extends Config> clazz) {
Method[] methods = clazz.getMethods();
for (Method method : methods) {
String key = key(method);
String value = defaultValue(method);
if (value != null)
properties.put(key, value);
}
}
}