Skip to content

Commit

Permalink
#1901 - Use Optional.or(…) instead of custom method in PropertyUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Feb 15, 2023
1 parent 14036b1 commit c550f8c
Showing 1 changed file with 4 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ private static class AnnotatedProperty {
*
* @param property must not be {@literal null}.
*/
@SuppressWarnings("unchecked")
public AnnotatedProperty(Property property) {

Assert.notNull(property, "Property must not be null!");
Expand All @@ -340,10 +339,10 @@ public AnnotatedProperty(Property property) {

Field field = ReflectionUtils.findField(property.getObjectType(), property.getName());

this.type = firstNonEmpty( //
() -> Optional.ofNullable(property.getReadMethod()).map(ResolvableType::forMethodReturnType), //
() -> Optional.ofNullable(property.getWriteMethod()).map(it -> ResolvableType.forMethodParameter(it, 0)), //
() -> Optional.ofNullable(field).map(ResolvableType::forField));
this.type = Optional.ofNullable(property.getReadMethod()).map(ResolvableType::forMethodReturnType)
.or(() -> Optional.ofNullable(property.getWriteMethod()).map(it -> ResolvableType.forMethodParameter(it, 0))) //
.or(() -> Optional.ofNullable(field).map(ResolvableType::forField))
.orElseThrow(() -> new IllegalStateException("Could not resolve value!"));

this.annotations = Stream.of(property.getReadMethod(), property.getWriteMethod(), field) //
.filter(it -> it != null) //
Expand All @@ -353,17 +352,6 @@ public AnnotatedProperty(Property property) {
this.typeAnnotations = MergedAnnotations.from(this.type.resolve(Object.class));
}

@SuppressWarnings("unchecked")
private static <T> T firstNonEmpty(Supplier<Optional<T>>... suppliers) {

Assert.notNull(suppliers, "Suppliers must not be null!");

return Stream.of(suppliers) //
.map(Supplier::get).flatMap(it -> it.map(Stream::of).orElseGet(Stream::empty)) //
.findFirst() //
.orElseThrow(() -> new IllegalStateException("Could not resolve value!"));
}

/**
* Returns the name of the property.
*
Expand Down

0 comments on commit c550f8c

Please sign in to comment.