From 158a392d80c183ae39bba8783c16c27b64dfbff4 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 5 Apr 2011 03:45:38 +0000 Subject: [PATCH] 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 --- .../beans/ExtendedBeanInfo.java | 6 ++++++ .../beans/ExtendedBeanInfoTests.java | 21 +++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java b/org.springframework.beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java index 32709cb427..aeb98114ea 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java @@ -82,6 +82,9 @@ public class ExtendedBeanInfo implements BeanInfo { // is the method a NON-INDEXED setter? ignore return type in order to capture non-void signatures if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { String propertyName = propertyNameFor(method); + if(propertyName.length() == 0) { + continue ALL_METHODS; + } for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { Method readMethod = pd.getReadMethod(); Method writeMethod = pd.getWriteMethod(); @@ -109,6 +112,9 @@ public class ExtendedBeanInfo implements BeanInfo { // is the method an INDEXED setter? ignore return type in order to capture non-void signatures if (method.getName().startsWith("set") && method.getParameterTypes().length == 2 && method.getParameterTypes()[0].equals(int.class)) { String propertyName = propertyNameFor(method); + if(propertyName.length() == 0) { + continue ALL_METHODS; + } DELEGATE_PD: for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { if (!(pd instanceof IndexedPropertyDescriptor)) { diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java index b0fc2aa6a3..942c14a4db 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java @@ -19,8 +19,6 @@ package org.springframework.beans; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.lessThan; import static org.junit.Assert.assertThat; @@ -32,7 +30,6 @@ import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; -import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.ExtendedBeanInfo.PropertyDescriptorComparator; @@ -485,6 +482,23 @@ public class ExtendedBeanInfoTests { assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); } + /** + * Ensures that an empty string is not passed into a PropertyDescriptor constructor. This + * could occur when handling ArrayList.set(int,Object) + */ + @Test + public void emptyPropertiesIgnored() throws IntrospectionException { + @SuppressWarnings("unused") class C { + public Object set(Object o) { return null; } + public Object set(int i, Object o) { return null; } + } + + BeanInfo bi = Introspector.getBeanInfo(C.class); + ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); + + assertThat(ebi.getPropertyDescriptors(), equalTo(bi.getPropertyDescriptors())); + } + @Test public void propertyCountsMatch() throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(TestBean.class); @@ -545,7 +559,6 @@ public class ExtendedBeanInfoTests { assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("A", null, null)), greaterThan(0)); } - private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) {