Skip to content

Commit b45f1aa

Browse files
committed
ExtendedBeanInfo ignores invalid bean properties (analogous to the JavaBeans Introspector)
Issue: SPR-12434 (cherry picked from commit eacd4a1)
1 parent 0544647 commit b45f1aa

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,9 @@
3535
import java.util.Set;
3636
import java.util.TreeSet;
3737

38+
import org.apache.commons.logging.Log;
39+
import org.apache.commons.logging.LogFactory;
40+
3841
import static org.springframework.beans.PropertyDescriptorUtils.*;
3942

4043
/**
@@ -73,6 +76,8 @@
7376
*/
7477
class ExtendedBeanInfo implements BeanInfo {
7578

79+
private static final Log logger = LogFactory.getLog(ExtendedBeanInfo.class);
80+
7681
private final BeanInfo delegate;
7782

7883
private final Set<PropertyDescriptor> propertyDescriptors =
@@ -94,14 +99,30 @@ class ExtendedBeanInfo implements BeanInfo {
9499
public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
95100
this.delegate = delegate;
96101
for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) {
97-
this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ?
98-
new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd) :
99-
new SimplePropertyDescriptor(pd));
102+
try {
103+
this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ?
104+
new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd) :
105+
new SimplePropertyDescriptor(pd));
106+
}
107+
catch (IntrospectionException ex) {
108+
// Probably simply a method that wasn't meant to follow the JavaBeans pattern...
109+
if (logger.isDebugEnabled()) {
110+
logger.debug("Ignoring invalid bean property '" + pd.getName() + "': " + ex.getMessage());
111+
}
112+
}
100113
}
101114
MethodDescriptor[] methodDescriptors = delegate.getMethodDescriptors();
102115
if (methodDescriptors != null) {
103116
for (Method method : findCandidateWriteMethods(methodDescriptors)) {
104-
handleCandidateWriteMethod(method);
117+
try {
118+
handleCandidateWriteMethod(method);
119+
}
120+
catch (IntrospectionException ex) {
121+
// We're only trying to find candidates, can easily ignore extra ones here...
122+
if (logger.isDebugEnabled()) {
123+
logger.debug("Ignoring candidate write method [" + method + "]: " + ex.getMessage());
124+
}
125+
}
105126
}
106127
}
107128
}
@@ -131,15 +152,15 @@ public static boolean isCandidateWriteMethod(Method method) {
131152
String methodName = method.getName();
132153
Class<?>[] parameterTypes = method.getParameterTypes();
133154
int nParams = parameterTypes.length;
134-
return methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) &&
155+
return (methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) &&
135156
(!void.class.isAssignableFrom(method.getReturnType()) || Modifier.isStatic(method.getModifiers())) &&
136-
(nParams == 1 || (nParams == 2 && parameterTypes[0].equals(int.class)));
157+
(nParams == 1 || (nParams == 2 && parameterTypes[0].equals(int.class))));
137158
}
138159

139160
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
140161
int nParams = method.getParameterTypes().length;
141162
String propertyName = propertyNameFor(method);
142-
Class<?> propertyType = method.getParameterTypes()[nParams-1];
163+
Class<?> propertyType = method.getParameterTypes()[nParams - 1];
143164
PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
144165
if (nParams == 1) {
145166
if (existingPd == null) {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -922,6 +922,15 @@ public void shouldSupportStaticWriteMethod() throws IntrospectionException {
922922
}
923923
}
924924

925+
@Test // SPR-12434
926+
public void shouldDetectValidPropertiesAndIgnoreInvalidProperties() throws IntrospectionException {
927+
BeanInfo bi = new ExtendedBeanInfo(Introspector.getBeanInfo(java.awt.Window.class));
928+
assertThat(hasReadMethodForProperty(bi, "locationByPlatform"), is(true));
929+
assertThat(hasWriteMethodForProperty(bi, "locationByPlatform"), is(true));
930+
assertThat(hasIndexedReadMethodForProperty(bi, "locationByPlatform"), is(false));
931+
assertThat(hasIndexedWriteMethodForProperty(bi, "locationByPlatform"), is(false));
932+
}
933+
925934

926935
interface Spr9453<T> {
927936

0 commit comments

Comments
 (0)