ExtendedBeanInfo ignores invalid bean properties (analogous to the JavaBeans Introspector)
Issue: SPR-12434
This commit is contained in:
parent
f44217a0c2
commit
eacd4a181f
|
@ -34,6 +34,9 @@ import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,6 +75,8 @@ import org.springframework.util.ObjectUtils;
|
||||||
*/
|
*/
|
||||||
class ExtendedBeanInfo implements BeanInfo {
|
class ExtendedBeanInfo implements BeanInfo {
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(ExtendedBeanInfo.class);
|
||||||
|
|
||||||
private final BeanInfo delegate;
|
private final BeanInfo delegate;
|
||||||
|
|
||||||
private final Set<PropertyDescriptor> propertyDescriptors =
|
private final Set<PropertyDescriptor> propertyDescriptors =
|
||||||
|
@ -93,15 +98,31 @@ class ExtendedBeanInfo implements BeanInfo {
|
||||||
public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
|
public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) {
|
for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) {
|
||||||
|
try {
|
||||||
this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ?
|
this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ?
|
||||||
new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd) :
|
new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd) :
|
||||||
new SimplePropertyDescriptor(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();
|
MethodDescriptor[] methodDescriptors = delegate.getMethodDescriptors();
|
||||||
if (methodDescriptors != null) {
|
if (methodDescriptors != null) {
|
||||||
for (Method method : findCandidateWriteMethods(methodDescriptors)) {
|
for (Method method : findCandidateWriteMethods(methodDescriptors)) {
|
||||||
|
try {
|
||||||
handleCandidateWriteMethod(method);
|
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();
|
String methodName = method.getName();
|
||||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||||
int nParams = parameterTypes.length;
|
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())) &&
|
(!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 {
|
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
|
||||||
|
|
|
@ -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) {
|
private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) {
|
||||||
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
|
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
|
||||||
|
|
Loading…
Reference in New Issue