CachedIntrospectionResults completely traverses interface hierarchy

Issue: SPR-16978
This commit is contained in:
Juergen Hoeller 2018-06-28 13:30:18 +02:00
parent 81cb740e0a
commit bf5fe46fa9
2 changed files with 41 additions and 26 deletions

View File

@ -297,20 +297,10 @@ public final class CachedIntrospectionResults {
// Explicitly check implemented interfaces for setter/getter methods as well,
// in particular for Java 8 default methods...
Class<?> clazz = beanClass;
while (clazz != null && clazz != Object.class) {
Class<?>[] ifcs = clazz.getInterfaces();
for (Class<?> ifc : ifcs) {
if (!ClassUtils.isJavaLanguageInterface(ifc)) {
for (PropertyDescriptor pd : getBeanInfo(ifc).getPropertyDescriptors()) {
if (!this.propertyDescriptorCache.containsKey(pd.getName())) {
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
this.propertyDescriptorCache.put(pd.getName(), pd);
}
}
}
}
clazz = clazz.getSuperclass();
Class<?> currClass = beanClass;
while (currClass != null && currClass != Object.class) {
introspectInterfaces(beanClass, currClass);
currClass = currClass.getSuperclass();
}
this.typeDescriptorCache = new ConcurrentReferenceHashMap<>();
@ -320,6 +310,24 @@ public final class CachedIntrospectionResults {
}
}
private void introspectInterfaces(Class<?> beanClass, Class<?> currClass) throws IntrospectionException {
for (Class<?> ifc : currClass.getInterfaces()) {
if (!ClassUtils.isJavaLanguageInterface(ifc)) {
for (PropertyDescriptor pd : getBeanInfo(ifc).getPropertyDescriptors()) {
PropertyDescriptor existingPd = this.propertyDescriptorCache.get(pd.getName());
if (existingPd == null ||
(existingPd.getReadMethod() == null && pd.getReadMethod() != null)) {
// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
// against a declared read method, so we prefer read method descriptors here.
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
this.propertyDescriptorCache.put(pd.getName(), pd);
}
}
introspectInterfaces(ifc, ifc);
}
}
}
BeanInfo getBeanInfo() {
return this.beanInfo;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -49,7 +49,8 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
GetterBean target = new GetterBean();
BeanWrapper accessor = createAccessor(target);
accessor.setPropertyValue("name", "tom");
assertTrue("Set name to tom", target.getName().equals("tom"));
assertEquals("tom", target.getAliasedName());
assertEquals("tom", accessor.getPropertyValue("aliasedName"));
}
@Test
@ -58,7 +59,8 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
BeanWrapper accessor = createAccessor(target);
accessor.setExtractOldValueForEditor(true); // This will call the getter
accessor.setPropertyValue("name", "tom");
assertTrue("Set name to tom", target.getName().equals("tom"));
assertEquals("tom", target.getAliasedName());
assertEquals("tom", accessor.getPropertyValue("aliasedName"));
}
@Test
@ -66,7 +68,8 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
GetterBean target = new GetterBean();
BeanWrapper accessor = createAccessor(target);
accessor.setPropertyValue("aliasedName", "tom");
assertTrue("Set name to tom", target.getAliasedName().equals("tom"));
assertEquals("tom", target.getAliasedName());
assertEquals("tom", accessor.getPropertyValue("aliasedName"));
}
@Test
@ -216,23 +219,27 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
}
@SuppressWarnings("unused")
private interface AliasedProperty {
default void setAliasedName(String name) {
setName(name);
}
private interface BaseProperty {
default String getAliasedName() {
return getName();
}
void setName(String name);
String getName();
}
@SuppressWarnings("unused")
private interface AliasedProperty extends BaseProperty {
default void setAliasedName(String name) {
setName(name);
}
void setName(String name);
}
@SuppressWarnings("unused")
private static class GetterBean implements AliasedProperty {