From 2fb46e48c02130abc1ffd9b11a4774ed5295265f Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Wed, 1 Apr 2009 23:27:49 +0000 Subject: [PATCH] design change - no longer surfacing typeDescriptor through property accessor. conversion done internally in property write() code git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@914 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../BeanExpressionContextAccessor.java | 6 ---- .../expression/BeanFactoryAccessor.java | 6 ---- .../context/expression/MapAccessor.java | 6 ---- .../expression/PropertyAccessor.java | 10 ------ .../spel/ast/PropertyOrFieldReference.java | 33 ++----------------- .../support/ReflectivePropertyResolver.java | 17 ++++++++-- .../spel/ExpressionLanguageScenarioTests.java | 7 ---- .../expression/spel/MapAccessTests.java | 4 --- .../expression/spel/PropertyAccessTests.java | 5 --- .../spel/ScenariosForSpringSecurity.java | 6 ---- 10 files changed, 16 insertions(+), 84 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java b/org.springframework.context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java index 8f6a78eba18..d8b96ccfbbb 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java @@ -17,7 +17,6 @@ package org.springframework.context.expression; import org.springframework.beans.factory.config.BeanExpressionContext; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; import org.springframework.expression.PropertyAccessor; @@ -51,9 +50,4 @@ public class BeanExpressionContextAccessor implements PropertyAccessor { return new Class[] {BeanExpressionContext.class}; } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, - Object target, String name) { - return null; - } - } \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java b/org.springframework.context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java index 022ad2b2f78..973981a8caa 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java @@ -17,7 +17,6 @@ package org.springframework.context.expression; import org.springframework.beans.factory.BeanFactory; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; import org.springframework.expression.PropertyAccessor; @@ -51,9 +50,4 @@ public class BeanFactoryAccessor implements PropertyAccessor { return new Class[] {BeanFactory.class}; } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, - Object target, String name) { - return null; - } - } diff --git a/org.springframework.context/src/main/java/org/springframework/context/expression/MapAccessor.java b/org.springframework.context/src/main/java/org/springframework/context/expression/MapAccessor.java index ee5713a7d85..8567bbd1a17 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/expression/MapAccessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/expression/MapAccessor.java @@ -18,7 +18,6 @@ package org.springframework.context.expression; import java.util.Map; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; import org.springframework.expression.PropertyAccessor; @@ -53,9 +52,4 @@ public class MapAccessor implements PropertyAccessor { return new Class[] {Map.class}; } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, - Object target, String name) { - return null; - } - } diff --git a/org.springframework.expression/src/main/java/org/springframework/expression/PropertyAccessor.java b/org.springframework.expression/src/main/java/org/springframework/expression/PropertyAccessor.java index 0692e82500c..e59b84324ad 100644 --- a/org.springframework.expression/src/main/java/org/springframework/expression/PropertyAccessor.java +++ b/org.springframework.expression/src/main/java/org/springframework/expression/PropertyAccessor.java @@ -16,7 +16,6 @@ package org.springframework.expression; -import org.springframework.core.convert.TypeDescriptor; /** * A property accessor is able to read (and possibly write) to object properties. The interface places no restrictions @@ -43,15 +42,6 @@ public interface PropertyAccessor { * @return an array of classes that this resolver is suitable for (or null if a general resolver) */ Class[] getSpecificTargetClasses(); - - /** - * Called to retrieve a type descriptor that describes the type of the property. - * @param context the evaluation context in which the access is being attempted - * @param target the target object upon which the property is being accessed - * @param name the name of the property being accessed - * @return a type descriptor that describes the type of this property. - */ - TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name); /** * Called to determine if a resolver instance is able to access a specified property on a specified target object. diff --git a/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java b/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java index c4dbcfdf564..7cfe1c56a54 100644 --- a/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java +++ b/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java @@ -20,10 +20,8 @@ import java.util.ArrayList; import java.util.List; import org.antlr.runtime.Token; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; -import org.springframework.expression.EvaluationException; import org.springframework.expression.PropertyAccessor; import org.springframework.expression.spel.ExpressionState; import org.springframework.expression.spel.SpelException; @@ -44,9 +42,6 @@ public class PropertyOrFieldReference extends SpelNodeImpl { private volatile PropertyAccessor cachedWriteAccessor; - private volatile TypeDescriptor cachedTypeDescriptor; - - public PropertyOrFieldReference(Token payload) { super(payload); this.name = payload.getText(); @@ -126,19 +121,7 @@ public class PropertyOrFieldReference extends SpelNodeImpl { PropertyAccessor accessorToUse = this.cachedWriteAccessor; if (accessorToUse != null) { try { - Object possiblyConvertedValue = newValue; - if (cachedTypeDescriptor == null) { - cachedTypeDescriptor=accessorToUse.getTypeDescriptor(eContext, contextObject, name); - } - if (cachedTypeDescriptor != null) { - try { - possiblyConvertedValue = state.convertValue(newValue, cachedTypeDescriptor.getType()); - } catch (EvaluationException evaluationException) { - throw new SpelException(getCharPositionInLine(), evaluationException, SpelMessages.TYPE_CONVERSION_ERROR, - newValue.getClass(), cachedTypeDescriptor.getType()); - } - } - accessorToUse.write(state.getEvaluationContext(), contextObject, name, possiblyConvertedValue); + accessorToUse.write(state.getEvaluationContext(), contextObject, name, newValue); return; } catch (AccessException ae) { @@ -156,19 +139,7 @@ public class PropertyOrFieldReference extends SpelNodeImpl { for (PropertyAccessor accessor : accessorsToTry) { if (accessor.canWrite(eContext, contextObject, name)) { this.cachedWriteAccessor = accessor; - Object possiblyConvertedValue = newValue; - if (cachedTypeDescriptor == null) { - cachedTypeDescriptor=accessor.getTypeDescriptor(eContext, contextObject, name); - } - if (cachedTypeDescriptor != null) { - try { - possiblyConvertedValue = state.convertValue(newValue, cachedTypeDescriptor); - } catch (EvaluationException evaluationException) { - throw new SpelException(getCharPositionInLine(), evaluationException, SpelMessages.TYPE_CONVERSION_ERROR, - newValue.getClass(), cachedTypeDescriptor.getType()); - } - } - accessor.write(eContext, contextObject, name, possiblyConvertedValue); + accessor.write(eContext, contextObject, name, newValue); return; } } diff --git a/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java b/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java index 56cbf213014..7f06c4ea136 100644 --- a/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java +++ b/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java @@ -28,6 +28,7 @@ import org.springframework.core.MethodParameter; import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; +import org.springframework.expression.EvaluationException; import org.springframework.expression.PropertyAccessor; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -175,6 +176,16 @@ public class ReflectivePropertyResolver implements PropertyAccessor { } Class type = (target instanceof Class ? (Class) target : target.getClass()); + Object possiblyConvertedNewValue = newValue; + TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name); + if (typeDescriptor != null) { + try { + possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, typeDescriptor); + } catch (EvaluationException evaluationException) { + throw new AccessException("Type conversion failure",evaluationException); + } + } + CacheKey cacheKey = new CacheKey(type, name); Member cachedMember = this.writerCache.get(cacheKey); @@ -190,7 +201,7 @@ public class ReflectivePropertyResolver implements PropertyAccessor { if (method != null) { try { ReflectionUtils.makeAccessible(method); - method.invoke(target, newValue); + method.invoke(target, possiblyConvertedNewValue); return; } catch (Exception ex) { @@ -211,7 +222,7 @@ public class ReflectivePropertyResolver implements PropertyAccessor { if (field != null) { try { ReflectionUtils.makeAccessible(field); - field.set(target, newValue); + field.set(target, possiblyConvertedNewValue); return; } catch (Exception ex) { @@ -223,7 +234,7 @@ public class ReflectivePropertyResolver implements PropertyAccessor { throw new AccessException("Neither setter nor field found for property '" + name + "'"); } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { + private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { if (target == null) { return null; } diff --git a/org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionLanguageScenarioTests.java b/org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionLanguageScenarioTests.java index aa03b5731fb..51ab26092f0 100644 --- a/org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionLanguageScenarioTests.java +++ b/org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionLanguageScenarioTests.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationException; @@ -266,9 +265,6 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase { throws AccessException { } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - return null; - } } @@ -307,8 +303,5 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase { public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - return null; - } } } diff --git a/org.springframework.expression/src/test/java/org/springframework/expression/spel/MapAccessTests.java b/org.springframework.expression/src/test/java/org/springframework/expression/spel/MapAccessTests.java index 49e164df93d..ae97fa3ede4 100644 --- a/org.springframework.expression/src/test/java/org/springframework/expression/spel/MapAccessTests.java +++ b/org.springframework.expression/src/test/java/org/springframework/expression/spel/MapAccessTests.java @@ -18,7 +18,6 @@ package org.springframework.expression.spel; import java.util.Map; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; @@ -86,9 +85,6 @@ public class MapAccessTests extends ExpressionTestCase { return new Class[] { Map.class }; } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - return TypeDescriptor.valueOf(Map.class); - } } } diff --git a/org.springframework.expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java b/org.springframework.expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java index 3ce722a539e..def350ecd9c 100644 --- a/org.springframework.expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java +++ b/org.springframework.expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java @@ -16,7 +16,6 @@ package org.springframework.expression.spel; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationException; @@ -88,10 +87,6 @@ public class PropertyAccessTests extends ExpressionTestCase { int flibbles = 7; - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - return null; - } - public Class[] getSpecificTargetClasses() { return new Class[] { String.class }; } diff --git a/org.springframework.expression/src/test/java/org/springframework/expression/spel/ScenariosForSpringSecurity.java b/org.springframework.expression/src/test/java/org/springframework/expression/spel/ScenariosForSpringSecurity.java index 93fbc4fb013..7ac94f13972 100644 --- a/org.springframework.expression/src/test/java/org/springframework/expression/spel/ScenariosForSpringSecurity.java +++ b/org.springframework.expression/src/test/java/org/springframework/expression/spel/ScenariosForSpringSecurity.java @@ -226,9 +226,6 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase { return null; } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - return null; - } } @@ -259,9 +256,6 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase { return null; } - public TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - return null; - } }