Skip to content

Commit

Permalink
@optional in getters for properties without field
Browse files Browse the repository at this point in the history
  • Loading branch information
radist-nt committed Jul 14, 2018
1 parent c6d1ed4 commit 4c63190
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private static boolean hasAtLeastOneBeanProperty(Class<?> clazz) {

private static boolean isOptionalProperty(Class beanClass, PropertyDescriptor beanProp) {
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) {
Expand Down
10 changes: 10 additions & 0 deletions config/src/test/java/beanconfig/ObjectsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class ValueObject {
@Optional
private String optionalValue;
private String mandatoryValue;
private String Default;

public String getMandatoryValue() {
return mandatoryValue;
Expand All @@ -24,6 +25,15 @@ public String getOptionalValue() {
public void setOptionalValue(String optionalValue) {
this.optionalValue = optionalValue;
}

@Optional
public String getDefault() {
return Default;
}

public void setDefault(String Default) {
this.Default = Default;
}
}

private ValueObject valueObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class ConfigBeanFactoryTest extends TestUtils {
assertNotNull(beanConfig)
assertNotNull(beanConfig.getValueObject)
assertNull(beanConfig.getValueObject.getOptionalValue)
assertNull(beanConfig.getValueObject.getDefault)
assertEquals("notNull", beanConfig.getValueObject.getMandatoryValue)
}

Expand Down

0 comments on commit 4c63190

Please sign in to comment.