Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Optional in getters for properties without field #570

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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