Browse Source

ExtendedBeanInfo ignores invalid bean properties (analogous to the JavaBeans Introspector)

Issue: SPR-12434
pull/701/head
Juergen Hoeller 10 years ago
parent
commit
eacd4a181f
  1. 33
      spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java
  2. 9
      spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java

33
spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java

@ -34,6 +34,9 @@ import java.util.List; @@ -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; @@ -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<PropertyDescriptor> propertyDescriptors =
@ -93,14 +98,30 @@ class ExtendedBeanInfo implements BeanInfo { @@ -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 { @@ -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 {

9
spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java

@ -875,6 +875,15 @@ public class ExtendedBeanInfoTests { @@ -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()) {

Loading…
Cancel
Save