Properly identify accessor methods

This commit fixes the binder so that it property identifies JavaBean
accessors. Previously an accessor named `get` or `is` was identified.
Similarly, a setter named `set` was identified.

Closes gh-12363
pull/12428/head
Stephane Nicoll 7 years ago
parent 36ed7ae699
commit 9b1003d9f6

@ -126,17 +126,17 @@ class JavaBeanBinder implements BeanBinder {
private void addMethod(Method method) {
String name = method.getName();
int parameterCount = method.getParameterCount();
if (name.startsWith("get") && parameterCount == 0) {
if (name.startsWith("get") && parameterCount == 0 && name.length() > 3) {
name = Introspector.decapitalize(name.substring(3));
this.properties.computeIfAbsent(name, this::getBeanProperty)
.addGetter(method);
}
else if (name.startsWith("is") && parameterCount == 0) {
else if (name.startsWith("is") && parameterCount == 0 && name.length() > 2) {
name = Introspector.decapitalize(name.substring(2));
this.properties.computeIfAbsent(name, this::getBeanProperty)
.addGetter(method);
}
else if (name.startsWith("set") && parameterCount == 1) {
else if (name.startsWith("set") && parameterCount == 1 && name.length() > 3) {
name = Introspector.decapitalize(name.substring(3));
this.properties.computeIfAbsent(name, this::getBeanProperty)
.addSetter(method);

@ -476,6 +476,16 @@ public class JavaBeanBinderTests {
assertThat(bean.getValue()).isEqualTo(RuntimeException.class);
}
@Test
public void bindToClassShouldIgnoreInvalidAccessors() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.name", "something");
this.sources.add(source);
ExampleWithInvalidAccessors bean = this.binder
.bind("foo", Bindable.of(ExampleWithInvalidAccessors.class)).get();
assertThat(bean.getName()).isEqualTo("something");
}
public static class ExampleValueBean {
private int intValue;
@ -813,6 +823,28 @@ public class JavaBeanBinderTests {
}
public static class ExampleWithInvalidAccessors {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String get() {
throw new IllegalArgumentException("should not be invoked");
}
public boolean is() {
throw new IllegalArgumentException("should not be invoked");
}
}
public enum ExampleEnum {
FOO_BAR,

Loading…
Cancel
Save