Remember annotations when using withExistingValue

Update `Bindable` builder methods so that existing annotations are
retained.

Closes gh-18218
This commit is contained in:
Phillip Webb
2019-09-12 15:09:47 -07:00
parent 4f21b51a2b
commit 23174eb484
2 changed files with 16 additions and 2 deletions

View File

@@ -162,7 +162,7 @@ public final class Bindable<T> {
existingValue == null || this.type.isArray() || this.boxedType.resolve().isInstance(existingValue),
() -> "ExistingValue must be an instance of " + this.type);
Supplier<T> value = (existingValue != null) ? () -> existingValue : null;
return new Bindable<>(this.type, this.boxedType, value, NO_ANNOTATIONS);
return new Bindable<>(this.type, this.boxedType, value, this.annotations);
}
/**
@@ -171,7 +171,7 @@ public final class Bindable<T> {
* @return an updated {@link Bindable}
*/
public Bindable<T> withSuppliedValue(Supplier<T> suppliedValue) {
return new Bindable<>(this.type, this.boxedType, suppliedValue, NO_ANNOTATIONS);
return new Bindable<>(this.type, this.boxedType, suppliedValue, this.annotations);
}
/**

View File

@@ -160,6 +160,20 @@ public class BindableTests {
assertThat(bindable1).isEqualTo(bindable3);
}
@Test // gh-18218
public void withExistingValueDoesNotForgetAnnotations() {
Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class);
Bindable<?> bindable = Bindable.of(String.class).withAnnotations(annotation).withExistingValue("");
assertThat(bindable.getAnnotations()).containsExactly(annotation);
}
@Test // gh-18218
public void withSuppliedValueValueDoesNotForgetAnnotations() {
Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class);
Bindable<?> bindable = Bindable.of(String.class).withAnnotations(annotation).withSuppliedValue(() -> "");
assertThat(bindable.getAnnotations()).containsExactly(annotation);
}
@Retention(RetentionPolicy.RUNTIME)
@interface TestAnnotation {