Implement in plain Java with no dependencies.

This commit is contained in:
Karthick Sankarachary 2016-05-31 10:06:00 -07:00
parent e1cf5dfc9c
commit 2b6452762f
5 changed files with 36 additions and 22 deletions

View File

@ -20,8 +20,6 @@ fork in run in Test := true
autoScalaLibrary := false
crossPaths := false
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.3.2"
libraryDependencies += "javax.annotation" % "javax.annotation-api" % "1.2"
libraryDependencies += "net.liftweb" %% "lift-json" % "2.5" % "test"
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"

View File

@ -1,7 +1,5 @@
package com.typesafe.config.impl;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
@ -134,11 +132,6 @@ public class ConfigBeanImpl {
}
}
private static boolean isOptionalProperty(Class clazz, PropertyDescriptor beanProp) {
Field field = FieldUtils.getField(clazz, beanProp.getName(), true);
return (field.getAnnotationsByType(Optional.class).length > 0);
}
// we could magically make this work in many cases by doing
// getAnyRef() (or getValue().unwrapped()), but anytime we
// rely on that, we aren't doing the type conversions Config
@ -273,4 +266,27 @@ public class ConfigBeanImpl {
return false;
}
private static boolean isOptionalProperty(Class beanClass, PropertyDescriptor beanProp) {
Field field = getField(beanClass, beanProp.getName());
if (field == null) {
throw new ConfigException.BadBean("Bean property '" + beanProp + "' of class " + beanClass.getName() + " does not exist");
}
return (field.getAnnotationsByType(Optional.class).length > 0);
}
private static Field getField(Class beanClass, String fieldName) {
try {
Field field = beanClass.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
// Don't give up yet. Try to look for field in super class, if any.
}
beanClass = beanClass.getSuperclass();
if (beanClass == null) {
return null;
}
return getField(beanClass, fieldName);
}
}

View File

@ -6,23 +6,23 @@ import com.typesafe.config.Optional;
public class ObjectsConfig {
public static class ValueObject {
@Optional
private String nullableValue;
private String nonNullableValue;
private String optionalValue;
private String mandatoryValue;
public String getNonNullableValue() {
return nonNullableValue;
public String getMandatoryValue() {
return mandatoryValue;
}
public void setNonNullableValue(String nonNullableValue) {
this.nonNullableValue = nonNullableValue;
public void setMandatoryValue(String mandatoryValue) {
this.mandatoryValue = mandatoryValue;
}
public String getNullableValue() {
return nullableValue;
public String getOptionalValue() {
return optionalValue;
}
public void setNullableValue(String nullableValue) {
this.nullableValue = nullableValue;
public void setOptionalValue(String optionalValue) {
this.optionalValue = optionalValue;
}
}

View File

@ -95,7 +95,7 @@
},
"objects" : {
"valueObject": {
"nonNullableValue": "nonNullValue"
"mandatoryValue": "notNull"
}
}
}

View File

@ -167,8 +167,8 @@ class ConfigBeanFactoryTest extends TestUtils {
val beanConfig: ObjectsConfig = ConfigBeanFactory.create(loadConfig().getConfig("objects"), classOf[ObjectsConfig])
assertNotNull(beanConfig)
assertNotNull(beanConfig.getValueObject)
assertNull(beanConfig.getValueObject.getNullableValue)
assertEquals("nonNullValue", beanConfig.getValueObject.getNonNullableValue)
assertNull(beanConfig.getValueObject.getOptionalValue)
assertEquals("notNull", beanConfig.getValueObject.getMandatoryValue)
}
@Test