Skip to content

Commit

Permalink
Polish ExtendedBeanInfo and tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cbeams committed Feb 13, 2012
1 parent e25f1cb commit ef143d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -282,7 +276,7 @@ private void addOrUpdatePropertyDescriptor(PropertyDescriptor pd, String propert
}
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
Expand All @@ -293,7 +287,7 @@ private void addOrUpdatePropertyDescriptor(PropertyDescriptor pd, String propert
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,17 +698,31 @@ class LawLibrary extends Library implements TextBookOperations {
}


/**
* 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!
Expand All @@ -717,11 +731,8 @@ public void demonstrateCauseSpr8949() throws IntrospectionException {
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));
Expand All @@ -731,13 +742,13 @@ public void cornerSpr8949() throws IntrospectionException {
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;
Expand Down

0 comments on commit ef143d3

Please sign in to comment.