mirror of
https://github.com/lightbend/config.git
synced 2025-02-23 17:50:30 +08:00
Merge pull request #570 from radist-nt/issue_567
@Optional in getters for properties without field
This commit is contained in:
commit
4edef986f4
@ -285,7 +285,7 @@ public class ConfigBeanImpl {
|
|||||||
|
|
||||||
private static boolean isOptionalProperty(Class beanClass, PropertyDescriptor beanProp) {
|
private static boolean isOptionalProperty(Class beanClass, PropertyDescriptor beanProp) {
|
||||||
Field field = getField(beanClass, beanProp.getName());
|
Field field = getField(beanClass, beanProp.getName());
|
||||||
return field != null && (field.getAnnotationsByType(Optional.class).length > 0);
|
return field != null ? field.getAnnotationsByType(Optional.class).length > 0 : beanProp.getReadMethod().getAnnotationsByType(Optional.class).length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Field getField(Class beanClass, String fieldName) {
|
private static Field getField(Class beanClass, String fieldName) {
|
||||||
|
@ -8,6 +8,7 @@ public class ObjectsConfig {
|
|||||||
@Optional
|
@Optional
|
||||||
private String optionalValue;
|
private String optionalValue;
|
||||||
private String mandatoryValue;
|
private String mandatoryValue;
|
||||||
|
private String Default;
|
||||||
|
|
||||||
public String getMandatoryValue() {
|
public String getMandatoryValue() {
|
||||||
return mandatoryValue;
|
return mandatoryValue;
|
||||||
@ -24,6 +25,15 @@ public class ObjectsConfig {
|
|||||||
public void setOptionalValue(String optionalValue) {
|
public void setOptionalValue(String optionalValue) {
|
||||||
this.optionalValue = optionalValue;
|
this.optionalValue = optionalValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Optional
|
||||||
|
public String getDefault() {
|
||||||
|
return Default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefault(String Default) {
|
||||||
|
this.Default = Default;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ValueObject valueObject;
|
private ValueObject valueObject;
|
||||||
|
@ -211,6 +211,7 @@ class ConfigBeanFactoryTest extends TestUtils {
|
|||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
assertNotNull(beanConfig.getValueObject)
|
assertNotNull(beanConfig.getValueObject)
|
||||||
assertNull(beanConfig.getValueObject.getOptionalValue)
|
assertNull(beanConfig.getValueObject.getOptionalValue)
|
||||||
|
assertNull(beanConfig.getValueObject.getDefault)
|
||||||
assertEquals("notNull", beanConfig.getValueObject.getMandatoryValue)
|
assertEquals("notNull", beanConfig.getValueObject.getMandatoryValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user