提交 ef143d36 编写于 作者: C Chris Beams

Polish ExtendedBeanInfo and tests

ExtendedBeanInfo

 - Reduce log messages from warn to debug

 - Remove now-incorrect comment indicating underlying property
   descriptors are never modified (they actually are as of SPR-8806)

ExtendedBeanInfoTests

 - Consolidate SPR-8949 tests

 - Eliminate compiler warnings

Issue: SPR-8949, SPR-8806
上级 e25f1cbc
......@@ -75,12 +75,6 @@ class ExtendedBeanInfo implements BeanInfo {
public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
this.delegate = delegate;
// PropertyDescriptor instances from the delegate object are never added directly, but always
// copied to the local collection of #propertyDescriptors and returned by calls to
// #getPropertyDescriptors(). this algorithm iterates through all methods (method descriptors)
// in the wrapped BeanInfo object, copying any existing PropertyDescriptor or creating a new
// one for any non-standard setter methods found.
ALL_METHODS:
for (MethodDescriptor md : delegate.getMethodDescriptors()) {
Method method = md.getMethod();
......@@ -282,7 +276,7 @@ class ExtendedBeanInfo implements BeanInfo {
}
this.propertyDescriptors.add(pd);
} catch (IntrospectionException ex) {
logger.warn(format("Could not create new PropertyDescriptor for readMethod [%s] writeMethod [%s] " +
logger.debug(format("Could not create new PropertyDescriptor for readMethod [%s] writeMethod [%s] " +
"indexedReadMethod [%s] indexedWriteMethod [%s] for property [%s]. Reason: %s",
readMethod, writeMethod, indexedReadMethod, indexedWriteMethod, propertyName, ex.getMessage()));
// suppress exception and attempt to continue
......@@ -293,7 +287,7 @@ class ExtendedBeanInfo implements BeanInfo {
try {
pd.setWriteMethod(writeMethod);
} catch (IntrospectionException ex) {
logger.warn(format("Could not add write method [%s] for property [%s]. Reason: %s",
logger.debug(format("Could not add write method [%s] for property [%s]. Reason: %s",
writeMethod, propertyName, ex.getMessage()));
// fall through -> add property descriptor as best we can
}
......
......@@ -698,17 +698,31 @@ public class ExtendedBeanInfoTests {
}
/**
* java.beans.Introspector returns the "wrong" declaring class for overridden read
* methods, which in turn violates expectations in {@link ExtendedBeanInfo} regarding
* method equality. Spring's {@link ClassUtils#getMostSpecificMethod(Method, Class)}
* helps out here, and is now put into use in ExtendedBeanInfo as well
*/
@Test
public void demonstrateCauseSpr8949() throws IntrospectionException {
BeanInfo info = Introspector.getBeanInfo(B.class);
public void cornerSpr8949() throws IntrospectionException {
class A {
@SuppressWarnings("unused")
public boolean isTargetMethod() {
return false;
}
}
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
class B extends A {
@Override
public boolean isTargetMethod() {
return false;
}
}
BeanInfo bi = Introspector.getBeanInfo(B.class);
/* first, demonstrate the 'problem':
* java.beans.Introspector returns the "wrong" declaring class for overridden read
* methods, which in turn violates expectations in {@link ExtendedBeanInfo} regarding
* method equality. Spring's {@link ClassUtils#getMostSpecificMethod(Method, Class)}
* helps out here, and is now put into use in ExtendedBeanInfo as well
*/
for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
if ("targetMethod".equals(pd.getName())) {
Method readMethod = pd.getReadMethod();
assertTrue(readMethod.getDeclaringClass().equals(A.class)); // we expected B!
......@@ -717,11 +731,8 @@ public class ExtendedBeanInfoTests {
assertTrue(msReadMethod.getDeclaringClass().equals(B.class)); // and now we get it.
}
}
}
@Test
public void cornerSpr8949() throws IntrospectionException {
BeanInfo bi = Introspector.getBeanInfo(B.class);
// and now demonstrate that we've indeed fixed the problem
ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi);
assertThat(hasReadMethodForProperty(bi, "targetMethod"), is(true));
......@@ -731,13 +742,13 @@ public class ExtendedBeanInfoTests {
assertThat(hasWriteMethodForProperty(ebi, "targetMethod"), is(false));
}
static class A {
static class X {
public boolean isTargetMethod() {
return false;
}
}
static class B extends A {
static class Y extends X {
@Override
public boolean isTargetMethod() {
return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册