diff --git a/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java b/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java index 5bee903bb5..1a66a70de2 100644 --- a/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java +++ b/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java @@ -34,6 +34,9 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.util.ObjectUtils; /** @@ -72,6 +75,8 @@ import org.springframework.util.ObjectUtils; */ class ExtendedBeanInfo implements BeanInfo { + private static final Log logger = LogFactory.getLog(ExtendedBeanInfo.class); + private final BeanInfo delegate; private final Set propertyDescriptors = @@ -93,14 +98,30 @@ class ExtendedBeanInfo implements BeanInfo { public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException { this.delegate = delegate; for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { - this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ? - new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd) : - new SimplePropertyDescriptor(pd)); + try { + this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ? + new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd) : + new SimplePropertyDescriptor(pd)); + } + catch (IntrospectionException ex) { + // Probably simply a method that wasn't meant to follow the JavaBeans pattern... + if (logger.isDebugEnabled()) { + logger.debug("Ignoring invalid bean property '" + pd.getName() + "': " + ex.getMessage()); + } + } } MethodDescriptor[] methodDescriptors = delegate.getMethodDescriptors(); if (methodDescriptors != null) { for (Method method : findCandidateWriteMethods(methodDescriptors)) { - handleCandidateWriteMethod(method); + try { + handleCandidateWriteMethod(method); + } + catch (IntrospectionException ex) { + // We're only trying to find candidates, can easily ignore extra ones here... + if (logger.isDebugEnabled()) { + logger.debug("Ignoring candidate write method [" + method + "]: " + ex.getMessage()); + } + } } } } @@ -130,9 +151,9 @@ class ExtendedBeanInfo implements BeanInfo { String methodName = method.getName(); Class[] parameterTypes = method.getParameterTypes(); int nParams = parameterTypes.length; - return methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) && + return (methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) && (!void.class.isAssignableFrom(method.getReturnType()) || Modifier.isStatic(method.getModifiers())) && - (nParams == 1 || (nParams == 2 && parameterTypes[0].equals(int.class))); + (nParams == 1 || (nParams == 2 && parameterTypes[0].equals(int.class)))); } private void handleCandidateWriteMethod(Method method) throws IntrospectionException { diff --git a/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java b/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java index 8ce1779722..c2ced2102c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java @@ -875,6 +875,15 @@ public class ExtendedBeanInfoTests { } } + @Test // SPR-12434 + public void shouldDetectValidPropertiesAndIgnoreInvalidProperties() throws IntrospectionException { + BeanInfo bi = new ExtendedBeanInfo(Introspector.getBeanInfo(java.awt.Window.class)); + assertThat(hasReadMethodForProperty(bi, "locationByPlatform"), is(true)); + assertThat(hasWriteMethodForProperty(bi, "locationByPlatform"), is(true)); + assertThat(hasIndexedReadMethodForProperty(bi, "locationByPlatform"), is(false)); + assertThat(hasIndexedWriteMethodForProperty(bi, "locationByPlatform"), is(false)); + } + private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {