Skip to content

Commit 158a392

Browse files
committed
Ignore non-prop 'set' methods in ExtendedBeanInfo
Previously, ExtendedBeanInfo would attempt to process methods named exactly 'set'. JavaBeans properties must have at least one character following the 'set' prefix in order to qualify, and this is now respected by EBI. Thanks to Rob Winch for the patch fixing this problem. Issue: SPR-8175
1 parent eba33b6 commit 158a392

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
8282
// is the method a NON-INDEXED setter? ignore return type in order to capture non-void signatures
8383
if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {
8484
String propertyName = propertyNameFor(method);
85+
if(propertyName.length() == 0) {
86+
continue ALL_METHODS;
87+
}
8588
for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) {
8689
Method readMethod = pd.getReadMethod();
8790
Method writeMethod = pd.getWriteMethod();
@@ -109,6 +112,9 @@ public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
109112
// is the method an INDEXED setter? ignore return type in order to capture non-void signatures
110113
if (method.getName().startsWith("set") && method.getParameterTypes().length == 2 && method.getParameterTypes()[0].equals(int.class)) {
111114
String propertyName = propertyNameFor(method);
115+
if(propertyName.length() == 0) {
116+
continue ALL_METHODS;
117+
}
112118
DELEGATE_PD:
113119
for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) {
114120
if (!(pd instanceof IndexedPropertyDescriptor)) {

org.springframework.beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import static org.hamcrest.CoreMatchers.equalTo;
2020
import static org.hamcrest.CoreMatchers.instanceOf;
2121
import static org.hamcrest.CoreMatchers.is;
22-
import static org.hamcrest.CoreMatchers.notNullValue;
23-
import static org.hamcrest.CoreMatchers.nullValue;
2422
import static org.hamcrest.Matchers.greaterThan;
2523
import static org.hamcrest.Matchers.lessThan;
2624
import static org.junit.Assert.assertThat;
@@ -32,7 +30,6 @@
3230
import java.beans.Introspector;
3331
import java.beans.PropertyDescriptor;
3432

35-
import org.junit.Ignore;
3633
import org.junit.Test;
3734
import org.springframework.beans.ExtendedBeanInfo.PropertyDescriptorComparator;
3835

@@ -485,6 +482,23 @@ public void setFoo(String foo) { }
485482
assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true));
486483
}
487484

485+
/**
486+
* Ensures that an empty string is not passed into a PropertyDescriptor constructor. This
487+
* could occur when handling ArrayList.set(int,Object)
488+
*/
489+
@Test
490+
public void emptyPropertiesIgnored() throws IntrospectionException {
491+
@SuppressWarnings("unused") class C {
492+
public Object set(Object o) { return null; }
493+
public Object set(int i, Object o) { return null; }
494+
}
495+
496+
BeanInfo bi = Introspector.getBeanInfo(C.class);
497+
ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi);
498+
499+
assertThat(ebi.getPropertyDescriptors(), equalTo(bi.getPropertyDescriptors()));
500+
}
501+
488502
@Test
489503
public void propertyCountsMatch() throws IntrospectionException {
490504
BeanInfo bi = Introspector.getBeanInfo(TestBean.class);
@@ -545,7 +559,6 @@ public void propertyDescriptorComparator() throws IntrospectionException {
545559
assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("A", null, null)), greaterThan(0));
546560
}
547561

548-
549562
private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) {
550563
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
551564
if (pd.getName().equals(propertyName)) {

0 commit comments

Comments
 (0)