Skip to content

Commit 9b1003d

Browse files
committed
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
1 parent 36ed7ae commit 9b1003d

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,17 @@ private boolean isCandidate(Method method) {
126126
private void addMethod(Method method) {
127127
String name = method.getName();
128128
int parameterCount = method.getParameterCount();
129-
if (name.startsWith("get") && parameterCount == 0) {
129+
if (name.startsWith("get") && parameterCount == 0 && name.length() > 3) {
130130
name = Introspector.decapitalize(name.substring(3));
131131
this.properties.computeIfAbsent(name, this::getBeanProperty)
132132
.addGetter(method);
133133
}
134-
else if (name.startsWith("is") && parameterCount == 0) {
134+
else if (name.startsWith("is") && parameterCount == 0 && name.length() > 2) {
135135
name = Introspector.decapitalize(name.substring(2));
136136
this.properties.computeIfAbsent(name, this::getBeanProperty)
137137
.addGetter(method);
138138
}
139-
else if (name.startsWith("set") && parameterCount == 1) {
139+
else if (name.startsWith("set") && parameterCount == 1 && name.length() > 3) {
140140
name = Introspector.decapitalize(name.substring(3));
141141
this.properties.computeIfAbsent(name, this::getBeanProperty)
142142
.addSetter(method);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,16 @@ public void bindWhenValueIsConvertedWithPropertyEditorShouldBind() {
476476
assertThat(bean.getValue()).isEqualTo(RuntimeException.class);
477477
}
478478

479+
@Test
480+
public void bindToClassShouldIgnoreInvalidAccessors() {
481+
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
482+
source.put("foo.name", "something");
483+
this.sources.add(source);
484+
ExampleWithInvalidAccessors bean = this.binder
485+
.bind("foo", Bindable.of(ExampleWithInvalidAccessors.class)).get();
486+
assertThat(bean.getName()).isEqualTo("something");
487+
}
488+
479489
public static class ExampleValueBean {
480490

481491
private int intValue;
@@ -813,6 +823,28 @@ public void setSelf(ExampleWithSelfReference self) {
813823

814824
}
815825

826+
public static class ExampleWithInvalidAccessors {
827+
828+
private String name;
829+
830+
public String getName() {
831+
return this.name;
832+
}
833+
834+
public void setName(String name) {
835+
this.name = name;
836+
}
837+
838+
public String get() {
839+
throw new IllegalArgumentException("should not be invoked");
840+
}
841+
842+
public boolean is() {
843+
throw new IllegalArgumentException("should not be invoked");
844+
}
845+
846+
}
847+
816848
public enum ExampleEnum {
817849

818850
FOO_BAR,

0 commit comments

Comments
 (0)