PropertyTypeDescriptor detects underlying field with same lenient naming rules as CachedIntrospectionResults (SPR-7102)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3253 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-04-16 15:39:23 +00:00
parent e8cfe6e573
commit 7c04f12f73
2 changed files with 19 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -269,6 +269,7 @@ public class CachedIntrospectionResults {
PropertyDescriptor getPropertyDescriptor(String name) { PropertyDescriptor getPropertyDescriptor(String name) {
PropertyDescriptor pd = this.propertyDescriptorCache.get(name); PropertyDescriptor pd = this.propertyDescriptorCache.get(name);
if (pd == null && StringUtils.hasLength(name)) { if (pd == null && StringUtils.hasLength(name)) {
// Same lenient fallback checking as in PropertyTypeDescriptor...
pd = this.propertyDescriptorCache.get(name.substring(0, 1).toLowerCase() + name.substring(1)); pd = this.propertyDescriptorCache.get(name.substring(0, 1).toLowerCase() + name.substring(1));
if (pd == null) { if (pd == null) {
pd = this.propertyDescriptorCache.get(name.substring(0, 1).toUpperCase() + name.substring(1)); pd = this.propertyDescriptorCache.get(name.substring(0, 1).toUpperCase() + name.substring(1));

View File

@ -26,6 +26,7 @@ import java.util.Map;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/** /**
* {@link TypeDescriptor} extension that exposes additional annotations * {@link TypeDescriptor} extension that exposes additional annotations
@ -33,7 +34,7 @@ import org.springframework.util.ReflectionUtils;
* (getter/setter) and on the underlying field, if found. * (getter/setter) and on the underlying field, if found.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.0 * @since 3.0.2
*/ */
public class PropertyTypeDescriptor extends TypeDescriptor { public class PropertyTypeDescriptor extends TypeDescriptor {
@ -74,12 +75,22 @@ public class PropertyTypeDescriptor extends TypeDescriptor {
public Annotation[] getAnnotations() { public Annotation[] getAnnotations() {
Annotation[] anns = this.cachedAnnotations; Annotation[] anns = this.cachedAnnotations;
if (anns == null) { if (anns == null) {
Field underlyingField = ReflectionUtils.findField(
getMethodParameter().getMethod().getDeclaringClass(), this.propertyDescriptor.getName());
Map<Class, Annotation> annMap = new LinkedHashMap<Class, Annotation>(); Map<Class, Annotation> annMap = new LinkedHashMap<Class, Annotation>();
if (underlyingField != null) { String name = this.propertyDescriptor.getName();
for (Annotation ann : underlyingField.getAnnotations()) { if (StringUtils.hasLength(name)) {
annMap.put(ann.annotationType(), ann); Class clazz = getMethodParameter().getMethod().getDeclaringClass();
Field field = ReflectionUtils.findField(clazz, name);
if (field == null) {
// Same lenient fallback checking as in CachedIntrospectionResults...
field = ReflectionUtils.findField(clazz, name.substring(0, 1).toLowerCase() + name.substring(1));
if (field == null) {
field = ReflectionUtils.findField(clazz, name.substring(0, 1).toUpperCase() + name.substring(1));
}
}
if (field != null) {
for (Annotation ann : field.getAnnotations()) {
annMap.put(ann.annotationType(), ann);
}
} }
} }
Method writeMethod = this.propertyDescriptor.getWriteMethod(); Method writeMethod = this.propertyDescriptor.getWriteMethod();