Skip to content

Commit

Permalink
Merge branch '2.3.x'
Browse files Browse the repository at this point in the history
Closes gh-24425
  • Loading branch information
philwebb committed Dec 10, 2020
2 parents 1f26aff + 0bfa9cf commit 1125030
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Expand Up @@ -21,9 +21,12 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.springframework.beans.BeanUtils;
Expand Down Expand Up @@ -126,13 +129,19 @@ static class Bean<T> {

private void addProperties(Class<?> type) {
while (type != null && !Object.class.equals(type)) {
Method[] declaredMethods = type.getDeclaredMethods();
Field[] declaredFields = type.getDeclaredFields();
Method[] declaredMethods = getSorted(type, Class::getDeclaredMethods, Method::getName);
Field[] declaredFields = getSorted(type, Class::getDeclaredFields, Field::getName);
addProperties(declaredMethods, declaredFields);
type = type.getSuperclass();
}
}

private <S, E> E[] getSorted(S source, Function<S, E[]> elements, Function<E, String> name) {
E[] result = elements.apply(source);
Arrays.sort(result, Comparator.comparing(name));
return result;
}

protected void addProperties(Method[] declaredMethods, Field[] declaredFields) {
for (int i = 0; i < declaredMethods.length; i++) {
if (!isCandidate(declaredMethods[i])) {
Expand Down
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -540,6 +541,19 @@ void bindWhenHasPackagePrivateSetterShouldBind() {
assertThat(bean.getProperty()).isEqualTo("test");
}

@Test
void bindUsesConsistentPropertyOrder() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.gamma", "0");
source.put("foo.alpha", "0");
source.put("foo.beta", "0");
this.sources.add(source);
PropertyOrderBean bean = this.binder.bind("foo", Bindable.of(PropertyOrderBean.class)).get();
assertThat(bean.getAlpha()).isEqualTo(0);
assertThat(bean.getBeta()).isEqualTo(1);
assertThat(bean.getGamma()).isEqualTo(2);
}

@Test // gh-23007
void bindWhenBeanWithGetSetIsMethodsFoundUsesGetterThatMatchesSetter() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
Expand Down Expand Up @@ -1086,4 +1100,40 @@ List<String> getNames() {

}

static class PropertyOrderBean {

static AtomicInteger atomic = new AtomicInteger();

private int alpha;

private int beta;

private int gamma;

int getAlpha() {
return this.alpha;
}

void setAlpha(int alpha) {
this.alpha = alpha + atomic.getAndIncrement();
}

int getBeta() {
return this.beta;
}

void setBeta(int beta) {
this.beta = beta + atomic.getAndIncrement();
}

int getGamma() {
return this.gamma;
}

void setGamma(int gamma) {
this.gamma = gamma + atomic.getAndIncrement();
}

}

}

0 comments on commit 1125030

Please sign in to comment.