From 7c04f12f73662c4426a6f5c66f7560a502de19dd Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 16 Apr 2010 15:39:23 +0000 Subject: [PATCH] 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 --- .../beans/CachedIntrospectionResults.java | 3 ++- .../support/PropertyTypeDescriptor.java | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java b/org.springframework.beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java index 2b9927cd3f6..e69cebdb65f 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java @@ -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"); * you may not use this file except in compliance with the License. @@ -269,6 +269,7 @@ public class CachedIntrospectionResults { PropertyDescriptor getPropertyDescriptor(String name) { PropertyDescriptor pd = this.propertyDescriptorCache.get(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)); if (pd == null) { pd = this.propertyDescriptorCache.get(name.substring(0, 1).toUpperCase() + name.substring(1)); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java index 630579e6a52..66ec1141d6f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java @@ -26,6 +26,7 @@ import java.util.Map; import org.springframework.core.MethodParameter; import org.springframework.core.convert.TypeDescriptor; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; /** * {@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. * * @author Juergen Hoeller - * @since 3.0 + * @since 3.0.2 */ public class PropertyTypeDescriptor extends TypeDescriptor { @@ -74,12 +75,22 @@ public class PropertyTypeDescriptor extends TypeDescriptor { public Annotation[] getAnnotations() { Annotation[] anns = this.cachedAnnotations; if (anns == null) { - Field underlyingField = ReflectionUtils.findField( - getMethodParameter().getMethod().getDeclaringClass(), this.propertyDescriptor.getName()); Map annMap = new LinkedHashMap(); - if (underlyingField != null) { - for (Annotation ann : underlyingField.getAnnotations()) { - annMap.put(ann.annotationType(), ann); + String name = this.propertyDescriptor.getName(); + if (StringUtils.hasLength(name)) { + 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();