Move ConfigBeanFactory internals to impl.ConfigBeanImpl

This is because I want to make some changes that will use
impl-private API.
This commit is contained in:
Havoc Pennington 2015-02-27 09:23:51 -05:00
parent 49dcdcae20
commit 11efe879e8
2 changed files with 95 additions and 82 deletions

View File

@ -1,16 +1,6 @@
package com.typesafe.config;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.time.Duration;
import com.typesafe.config.impl.ConfigImplUtil;
import com.typesafe.config.impl.ConfigBeanImpl;
/**
* Factory for automatically creating a Java class from a {@link Config}.
@ -46,76 +36,6 @@ public class ConfigBeanFactory {
* Can throw the same exceptions as the getters on <code>Config</code>
*/
public static <T> T create(Config config, Class<T> clazz) {
return createInternal(config, clazz);
}
private static <T> T createInternal(Config config, Class<T> clazz) {
Map<String, ?> configAsMap = config.root().unwrapped();
Map<String, Object> configProps = new HashMap<String, Object>();
Map<String,String> originalNames = new HashMap<String, String>();
for (Map.Entry<String, ?> configProp : configAsMap.entrySet()) {
configProps.put(ConfigImplUtil.toCamelCase(configProp.getKey()), configProp.getValue());
originalNames.put(ConfigImplUtil.toCamelCase(configProp.getKey()),configProp.getKey());
}
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException e) {
throw new ConfigException.BadBean("Could not get bean information for class " + clazz.getName(), e);
}
try {
T bean = clazz.newInstance();
for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) {
if (beanProp.getReadMethod() == null || beanProp.getWriteMethod() == null) {
continue;
}
Method setter = beanProp.getWriteMethod();
Object configValue = configProps.get(beanProp.getName());
if (configValue == null) {
throw new ConfigException.Missing(beanProp.getName());
}
if (configValue instanceof Map) {
configValue = createInternal(config.getConfig(originalNames.get(beanProp.getDisplayName())), beanProp.getPropertyType());
} else {
Class parameterClass = setter.getParameterTypes()[0];
configValue = getValueWithAutoConversion(parameterClass, config, originalNames.get(beanProp.getDisplayName()));
}
setter.invoke(bean, configValue);
}
return bean;
} catch (InstantiationException e) {
throw new ConfigException.BadBean(clazz.getName() + " needs a public no-args constructor to be used as a bean", e);
} catch (IllegalAccessException e) {
throw new ConfigException.BadBean(clazz.getName() + " getters and setters are not accessible, they must be for use as a bean", e);
} catch (InvocationTargetException e) {
throw new ConfigException.BadBean("Calling bean method on " + clazz.getName() + " caused an exception", e);
}
}
private static Object getValueWithAutoConversion(Class parameterClass, Config config, String configPropName) {
if (parameterClass == Boolean.class || parameterClass == boolean.class) {
return config.getBoolean(configPropName);
} else if (parameterClass == Byte.class || parameterClass == byte.class) {
return Integer.valueOf(config.getInt(configPropName)).byteValue();
} else if (parameterClass == Short.class || parameterClass == short.class) {
return Integer.valueOf(config.getInt(configPropName)).shortValue();
} else if (parameterClass == Integer.class || parameterClass == int.class) {
return config.getInt(configPropName);
} else if (parameterClass == Double.class || parameterClass == double.class) {
return config.getDouble(configPropName);
} else if (parameterClass == Long.class || parameterClass == long.class) {
return config.getLong(configPropName);
} else if (parameterClass == String.class) {
return config.getString(configPropName);
} else if (parameterClass == Duration.class) {
return config.getDuration(configPropName);
} else if (parameterClass == ConfigMemorySize.class) {
return config.getMemorySize(configPropName);
}
return config.getAnyRef(configPropName);
return ConfigBeanImpl.createInternal(config, clazz);
}
}

View File

@ -0,0 +1,93 @@
package com.typesafe.config.impl;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.time.Duration;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigMemorySize;
public class ConfigBeanImpl {
/**
* This is public ONLY for use by the "config" package, DO NOT USE this ABI
* may change.
*/
public static <T> T createInternal(Config config, Class<T> clazz) {
Map<String, ?> configAsMap = config.root().unwrapped();
Map<String, Object> configProps = new HashMap<String, Object>();
Map<String,String> originalNames = new HashMap<String, String>();
for (Map.Entry<String, ?> configProp : configAsMap.entrySet()) {
configProps.put(ConfigImplUtil.toCamelCase(configProp.getKey()), configProp.getValue());
originalNames.put(ConfigImplUtil.toCamelCase(configProp.getKey()),configProp.getKey());
}
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException e) {
throw new ConfigException.BadBean("Could not get bean information for class " + clazz.getName(), e);
}
try {
T bean = clazz.newInstance();
for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) {
if (beanProp.getReadMethod() == null || beanProp.getWriteMethod() == null) {
continue;
}
Method setter = beanProp.getWriteMethod();
Object configValue = configProps.get(beanProp.getName());
if (configValue == null) {
throw new ConfigException.Missing(beanProp.getName());
}
if (configValue instanceof Map) {
configValue = createInternal(config.getConfig(originalNames.get(beanProp.getDisplayName())), beanProp.getPropertyType());
} else {
Class parameterClass = setter.getParameterTypes()[0];
configValue = getValueWithAutoConversion(parameterClass, config, originalNames.get(beanProp.getDisplayName()));
}
setter.invoke(bean, configValue);
}
return bean;
} catch (InstantiationException e) {
throw new ConfigException.BadBean(clazz.getName() + " needs a public no-args constructor to be used as a bean", e);
} catch (IllegalAccessException e) {
throw new ConfigException.BadBean(clazz.getName() + " getters and setters are not accessible, they must be for use as a bean", e);
} catch (InvocationTargetException e) {
throw new ConfigException.BadBean("Calling bean method on " + clazz.getName() + " caused an exception", e);
}
}
private static Object getValueWithAutoConversion(Class parameterClass, Config config, String configPropName) {
if (parameterClass == Boolean.class || parameterClass == boolean.class) {
return config.getBoolean(configPropName);
} else if (parameterClass == Byte.class || parameterClass == byte.class) {
return Integer.valueOf(config.getInt(configPropName)).byteValue();
} else if (parameterClass == Short.class || parameterClass == short.class) {
return Integer.valueOf(config.getInt(configPropName)).shortValue();
} else if (parameterClass == Integer.class || parameterClass == int.class) {
return config.getInt(configPropName);
} else if (parameterClass == Double.class || parameterClass == double.class) {
return config.getDouble(configPropName);
} else if (parameterClass == Long.class || parameterClass == long.class) {
return config.getLong(configPropName);
} else if (parameterClass == String.class) {
return config.getString(configPropName);
} else if (parameterClass == Duration.class) {
return config.getDuration(configPropName);
} else if (parameterClass == ConfigMemorySize.class) {
return config.getMemorySize(configPropName);
}
return config.getAnyRef(configPropName);
}
}