removed ConversionService/TypeConverter convenience methods in order to restore 3.0's SPI (for backwards compatibility with implementers)
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3983 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
1a751de04f
commit
4bd265f0a4
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -64,10 +64,4 @@ public interface ConversionService {
|
||||||
*/
|
*/
|
||||||
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
|
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
|
||||||
|
|
||||||
// 3.1 additions that encapsulate TypeDescriptor.forObject(source)
|
|
||||||
|
|
||||||
boolean canConvert(Object source, TypeDescriptor targetType);
|
|
||||||
|
|
||||||
Object convert(Object source, TypeDescriptor targetType);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -56,7 +56,7 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAsText(String text) throws IllegalArgumentException {
|
public void setAsText(String text) throws IllegalArgumentException {
|
||||||
setValue(this.conversionService.convert(text, this.targetDescriptor));
|
setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -188,13 +188,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canConvert(Object source, TypeDescriptor targetType) {
|
|
||||||
return canConvert(TypeDescriptor.forObject(source), targetType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object convert(Object source, TypeDescriptor targetType) {
|
|
||||||
return convert(source, TypeDescriptor.forObject(source), targetType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
List<String> converterStrings = new ArrayList<String>();
|
List<String> converterStrings = new ArrayList<String>();
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.core.convert.support;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.awt.*;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
|
|
@ -40,7 +34,9 @@ import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.core.convert.ConversionFailedException;
|
import org.springframework.core.convert.ConversionFailedException;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
|
@ -287,7 +283,8 @@ public class DefaultConversionTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
|
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
|
||||||
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, new TypeDescriptor(getClass().getDeclaredField("genericList")));
|
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor
|
||||||
|
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
|
||||||
assertEquals(new Integer("1"), result.get(0));
|
assertEquals(new Integer("1"), result.get(0));
|
||||||
assertEquals(new Integer("2"), result.get(1));
|
assertEquals(new Integer("2"), result.get(1));
|
||||||
assertEquals(new Integer("3"), result.get(2));
|
assertEquals(new Integer("3"), result.get(2));
|
||||||
|
|
@ -297,7 +294,7 @@ public class DefaultConversionTests {
|
||||||
public void testSpr7766() throws Exception {
|
public void testSpr7766() throws Exception {
|
||||||
ConverterRegistry registry = ((ConverterRegistry) conversionService);
|
ConverterRegistry registry = ((ConverterRegistry) conversionService);
|
||||||
registry.addConverter(new ColorConverter());
|
registry.addConverter(new ColorConverter());
|
||||||
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
|
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
|
||||||
assertEquals(2, colors.size());
|
assertEquals(2, colors.size());
|
||||||
assertEquals(Color.WHITE, colors.get(0));
|
assertEquals(Color.WHITE, colors.get(0));
|
||||||
assertEquals(Color.BLACK, colors.get(1));
|
assertEquals(Color.BLACK, colors.get(1));
|
||||||
|
|
@ -457,7 +454,8 @@ public class DefaultConversionTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertStringToCollectionWithElementConversion() throws Exception {
|
public void convertStringToCollectionWithElementConversion() throws Exception {
|
||||||
List result = (List) conversionService.convert("1,2,3", new TypeDescriptor(getClass().getField("genericList")));
|
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
|
||||||
|
new TypeDescriptor(getClass().getField("genericList")));
|
||||||
assertEquals(3, result.size());
|
assertEquals(3, result.size());
|
||||||
assertEquals(new Integer(1), result.get(0));
|
assertEquals(new Integer(1), result.get(0));
|
||||||
assertEquals(new Integer(2), result.get(1));
|
assertEquals(new Integer(2), result.get(1));
|
||||||
|
|
@ -493,7 +491,8 @@ public class DefaultConversionTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertObjectToCollectionWithElementConversion() throws Exception {
|
public void convertObjectToCollectionWithElementConversion() throws Exception {
|
||||||
List<Integer> result = (List<Integer>) conversionService.convert(3L, new TypeDescriptor(getClass().getField("genericList")));
|
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
|
||||||
|
new TypeDescriptor(getClass().getField("genericList")));
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals(new Integer(3), result.get(0));
|
assertEquals(new Integer(3), result.get(0));
|
||||||
}
|
}
|
||||||
|
|
@ -528,7 +527,8 @@ public class DefaultConversionTests {
|
||||||
foo.add("1");
|
foo.add("1");
|
||||||
foo.add("2");
|
foo.add("2");
|
||||||
foo.add("3");
|
foo.add("3");
|
||||||
List<Integer> bar = (List<Integer>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericList")));
|
List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo),
|
||||||
|
new TypeDescriptor(getClass().getField("genericList")));
|
||||||
assertEquals(new Integer(1), bar.get(0));
|
assertEquals(new Integer(1), bar.get(0));
|
||||||
assertEquals(new Integer(2), bar.get(1));
|
assertEquals(new Integer(2), bar.get(1));
|
||||||
assertEquals(new Integer(3), bar.get(2));
|
assertEquals(new Integer(3), bar.get(2));
|
||||||
|
|
@ -536,7 +536,8 @@ public class DefaultConversionTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertCollectionToCollectionNull() throws Exception {
|
public void convertCollectionToCollectionNull() throws Exception {
|
||||||
List<Integer> bar = (List<Integer>) conversionService.convert(null, new TypeDescriptor(getClass().getField("genericList")));
|
List<Integer> bar = (List<Integer>) conversionService.convert(null,
|
||||||
|
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList")));
|
||||||
assertNull(bar);
|
assertNull(bar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -546,7 +547,8 @@ public class DefaultConversionTests {
|
||||||
foo.add("1");
|
foo.add("1");
|
||||||
foo.add("2");
|
foo.add("2");
|
||||||
foo.add("3");
|
foo.add("3");
|
||||||
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(List.class));
|
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(LinkedHashSet.class), TypeDescriptor
|
||||||
|
.valueOf(List.class));
|
||||||
assertEquals("1", bar.get(0));
|
assertEquals("1", bar.get(0));
|
||||||
assertEquals("2", bar.get(1));
|
assertEquals("2", bar.get(1));
|
||||||
assertEquals("3", bar.get(2));
|
assertEquals("3", bar.get(2));
|
||||||
|
|
@ -559,7 +561,8 @@ public class DefaultConversionTests {
|
||||||
map.put("2", "2");
|
map.put("2", "2");
|
||||||
map.put("3", "3");
|
map.put("3", "3");
|
||||||
Collection values = map.values();
|
Collection values = map.values();
|
||||||
List<Integer> bar = (List<Integer>) conversionService.convert(values, new TypeDescriptor(getClass().getField("genericList")));
|
List<Integer> bar = (List<Integer>) conversionService.convert(values,
|
||||||
|
TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList")));
|
||||||
assertEquals(3, bar.size());
|
assertEquals(3, bar.size());
|
||||||
assertEquals(new Integer(1), bar.get(0));
|
assertEquals(new Integer(1), bar.get(0));
|
||||||
assertEquals(new Integer(2), bar.get(1));
|
assertEquals(new Integer(2), bar.get(1));
|
||||||
|
|
@ -573,7 +576,8 @@ public class DefaultConversionTests {
|
||||||
Map<String, String> foo = new HashMap<String, String>();
|
Map<String, String> foo = new HashMap<String, String>();
|
||||||
foo.put("1", "BAR");
|
foo.put("1", "BAR");
|
||||||
foo.put("2", "BAZ");
|
foo.put("2", "BAZ");
|
||||||
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
|
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo,
|
||||||
|
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap")));
|
||||||
assertEquals(FooEnum.BAR, map.get(1));
|
assertEquals(FooEnum.BAR, map.get(1));
|
||||||
assertEquals(FooEnum.BAZ, map.get(2));
|
assertEquals(FooEnum.BAZ, map.get(2));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.core.convert.support;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
@ -32,7 +24,9 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionFailedException;
|
import org.springframework.core.convert.ConversionFailedException;
|
||||||
import org.springframework.core.convert.ConverterNotFoundException;
|
import org.springframework.core.convert.ConverterNotFoundException;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
@ -111,12 +105,12 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
public void convertNullTargetClass() {
|
public void convertNullTargetClass() {
|
||||||
assertNull(conversionService.convert("3", (Class<?>) null));
|
assertNull(conversionService.convert("3", (Class<?>) null));
|
||||||
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
|
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertNullTypeDescriptor() {
|
public void convertNullTypeDescriptor() {
|
||||||
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
|
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -150,7 +144,7 @@ public class GenericConversionServiceTests {
|
||||||
Boolean b = conversionService.convert("true", boolean.class);
|
Boolean b = conversionService.convert("true", boolean.class);
|
||||||
assertEquals(Boolean.TRUE, b);
|
assertEquals(Boolean.TRUE, b);
|
||||||
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
|
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
|
||||||
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(boolean.class));
|
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class));
|
||||||
assertEquals(Boolean.TRUE, b);
|
assertEquals(Boolean.TRUE, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -260,7 +254,7 @@ public class GenericConversionServiceTests {
|
||||||
GenericConversionService conversionService = new DefaultConversionService();
|
GenericConversionService conversionService = new DefaultConversionService();
|
||||||
Map<String, String> input = new LinkedHashMap<String, String>();
|
Map<String, String> input = new LinkedHashMap<String, String>();
|
||||||
input.put("key", "value");
|
input.put("key", "value");
|
||||||
Object converted = conversionService.convert(input, new TypeDescriptor(getClass().getField("wildcardMap")));
|
Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardMap")));
|
||||||
assertEquals(input, converted);
|
assertEquals(input, converted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -323,7 +317,7 @@ public class GenericConversionServiceTests {
|
||||||
source.add("3");
|
source.add("3");
|
||||||
TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
|
TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
|
||||||
for (int i = 0; i < 1000000; i++) {
|
for (int i = 0; i < 1000000; i++) {
|
||||||
conversionService.convert(source, td);
|
conversionService.convert(source, TypeDescriptor.forObject(source), td);
|
||||||
}
|
}
|
||||||
watch.stop();
|
watch.stop();
|
||||||
watch.start("convert 4,000,000 manually");
|
watch.start("convert 4,000,000 manually");
|
||||||
|
|
@ -350,7 +344,7 @@ public class GenericConversionServiceTests {
|
||||||
source.put("3", "3");
|
source.put("3", "3");
|
||||||
TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
|
TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
|
||||||
for (int i = 0; i < 1000000; i++) {
|
for (int i = 0; i < 1000000; i++) {
|
||||||
conversionService.convert(source, td);
|
conversionService.convert(source, TypeDescriptor.forObject(source), td);
|
||||||
}
|
}
|
||||||
watch.stop();
|
watch.stop();
|
||||||
watch.start("convert 4,000,000 manually");
|
watch.start("convert 4,000,000 manually");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -41,7 +41,7 @@ public interface TypeConverter {
|
||||||
/**
|
/**
|
||||||
* Convert (may coerce) a value from one type to another, for example from a boolean to a string.
|
* Convert (may coerce) a value from one type to another, for example from a boolean to a string.
|
||||||
* The typeDescriptor parameter enables support for typed collections - if the caller really wishes they
|
* The typeDescriptor parameter enables support for typed collections - if the caller really wishes they
|
||||||
* can have a List<Integer> for example, rather than simply a List.
|
* can have a List<Integer> for example, rather than simply a List.
|
||||||
* @param value the value to be converted
|
* @param value the value to be converted
|
||||||
* @param sourceType a type descriptor that supplies extra information about the source object
|
* @param sourceType a type descriptor that supplies extra information about the source object
|
||||||
* @param targetType a type descriptor that supplies extra information about the requested result type
|
* @param targetType a type descriptor that supplies extra information about the requested result type
|
||||||
|
|
@ -50,8 +50,4 @@ public interface TypeConverter {
|
||||||
*/
|
*/
|
||||||
Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
|
Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
|
||||||
|
|
||||||
// 3.1 additions for encapsulation of TypeDescriptor.forObject(value);
|
|
||||||
|
|
||||||
Object convertValue(Object value, TypeDescriptor targetType);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -141,7 +141,8 @@ public class ExpressionState {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
|
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
|
||||||
return this.relatedContext.getTypeConverter().convertValue(value.getValue(), targetTypeDescriptor);
|
Object val = value.getValue();
|
||||||
|
return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -35,8 +35,9 @@ import org.springframework.expression.spel.SpelMessage;
|
||||||
import org.springframework.expression.spel.SpelNode;
|
import org.springframework.expression.spel.SpelNode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the invocation of a constructor. Either a constructor on a regular type or construction of an array. When
|
* Represents the invocation of a constructor. Either a constructor on a regular type or
|
||||||
* an array is constructed, an initializer can be specified.
|
* construction of an array. When an array is constructed, an initializer can be specified.
|
||||||
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Examples:<br>
|
* Examples:<br>
|
||||||
* new String('hello world')<br>
|
* new String('hello world')<br>
|
||||||
|
|
@ -322,7 +323,7 @@ public class ConstructorReference extends SpelNodeImpl {
|
||||||
for (int i = 0; i < newObjectArray.length; i++) {
|
for (int i = 0; i < newObjectArray.length; i++) {
|
||||||
SpelNode elementNode = initializer.getChild(i);
|
SpelNode elementNode = initializer.getChild(i);
|
||||||
Object arrayEntry = elementNode.getValue(state);
|
Object arrayEntry = elementNode.getValue(state);
|
||||||
newObjectArray[i] = typeConverter.convertValue(arrayEntry, toTypeDescriptor);
|
newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -241,7 +241,8 @@ public class ReflectionHelper {
|
||||||
else {
|
else {
|
||||||
targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, argPosition));
|
targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, argPosition));
|
||||||
}
|
}
|
||||||
arguments[argPosition] = converter.convertValue(arguments[argPosition], targetType);
|
Object argument = arguments[argPosition];
|
||||||
|
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -278,7 +279,7 @@ public class ReflectionHelper {
|
||||||
if (converter == null) {
|
if (converter == null) {
|
||||||
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
|
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
|
||||||
}
|
}
|
||||||
arguments[argPosition] = converter.convertValue(argument, targetType);
|
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (EvaluationException ex) {
|
catch (EvaluationException ex) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -218,7 +218,8 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
|
||||||
TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
|
TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
|
||||||
if (typeDescriptor != null) {
|
if (typeDescriptor != null) {
|
||||||
try {
|
try {
|
||||||
possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, typeDescriptor);
|
possiblyConvertedNewValue = context.getTypeConverter().convertValue(
|
||||||
|
newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
|
||||||
}
|
}
|
||||||
catch (EvaluationException evaluationException) {
|
catch (EvaluationException evaluationException) {
|
||||||
throw new AccessException("Type conversion failure",evaluationException);
|
throw new AccessException("Type conversion failure",evaluationException);
|
||||||
|
|
|
||||||
|
|
@ -75,8 +75,4 @@ public class StandardTypeConverter implements TypeConverter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertValue(Object value, TypeDescriptor targetType) {
|
|
||||||
return convertValue(value, TypeDescriptor.forObject(value), targetType);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,15 @@
|
||||||
|
|
||||||
package org.springframework.expression.spel;
|
package org.springframework.expression.spel;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
|
||||||
import static junit.framework.Assert.assertNotNull;
|
|
||||||
import static junit.framework.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.*;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
@ -75,14 +74,14 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||||
// ArrayList containing List<Integer> to List<String>
|
// ArrayList containing List<Integer> to List<String>
|
||||||
Class<?> clazz = typeDescriptorForListOfString.getElementType();
|
Class<?> clazz = typeDescriptorForListOfString.getElementType();
|
||||||
assertEquals(String.class,clazz);
|
assertEquals(String.class,clazz);
|
||||||
List l = (List) tcs.convertValue(listOfInteger, typeDescriptorForListOfString);
|
List l = (List) tcs.convertValue(listOfInteger, TypeDescriptor.forObject(listOfInteger), typeDescriptorForListOfString);
|
||||||
assertNotNull(l);
|
assertNotNull(l);
|
||||||
|
|
||||||
// ArrayList containing List<String> to List<Integer>
|
// ArrayList containing List<String> to List<Integer>
|
||||||
clazz = typeDescriptorForListOfInteger.getElementType();
|
clazz = typeDescriptorForListOfInteger.getElementType();
|
||||||
assertEquals(Integer.class,clazz);
|
assertEquals(Integer.class,clazz);
|
||||||
|
|
||||||
l = (List) tcs.convertValue(listOfString, typeDescriptorForListOfString);
|
l = (List) tcs.convertValue(listOfString, TypeDescriptor.forObject(listOfString), typeDescriptorForListOfString);
|
||||||
assertNotNull(l);
|
assertNotNull(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,8 +120,7 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||||
assertTrue(evaluationContext.getTypeConverter()
|
assertTrue(evaluationContext.getTypeConverter()
|
||||||
.canConvert(TypeDescriptor.valueOf(String.class), collectionType));
|
.canConvert(TypeDescriptor.valueOf(String.class), collectionType));
|
||||||
// ... and it can be done successfully
|
// ... and it can be done successfully
|
||||||
assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", collectionType).toString());
|
assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString());
|
||||||
|
|
||||||
|
|
||||||
evaluationContext.setVariable("target", new TestTarget());
|
evaluationContext.setVariable("target", new TestTarget());
|
||||||
|
|
||||||
|
|
@ -133,6 +131,47 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Foo {
|
||||||
|
|
||||||
|
private Collection<Foo> foos;
|
||||||
|
|
||||||
|
public final String value;
|
||||||
|
|
||||||
|
public Foo(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFoos(Collection<Foo> foos) {
|
||||||
|
this.foos = foos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Foo> getFoos() {
|
||||||
|
return this.foos;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvert() {
|
||||||
|
Foo root = new Foo("bar");
|
||||||
|
StandardEvaluationContext context = new StandardEvaluationContext(root);
|
||||||
|
|
||||||
|
Collection<String> foos = Collections.singletonList("baz");
|
||||||
|
|
||||||
|
// property access, works
|
||||||
|
Expression expression = parser.parseExpression("foos");
|
||||||
|
expression.setValue(context, foos);
|
||||||
|
Foo baz = root.getFoos().iterator().next();
|
||||||
|
assertEquals("baz", baz.value);
|
||||||
|
|
||||||
|
// method call, fails (ClassCastException)
|
||||||
|
expression = parser.parseExpression("setFoos(#foos)");
|
||||||
|
context.setVariable("foos", foos);
|
||||||
|
expression.getValue(context);
|
||||||
|
baz = root.getFoos().iterator().next();
|
||||||
|
assertEquals("baz", baz.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type converter that uses the core conversion service.
|
* Type converter that uses the core conversion service.
|
||||||
|
|
@ -141,10 +180,6 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||||
|
|
||||||
private final ConversionService service = new DefaultConversionService();
|
private final ConversionService service = new DefaultConversionService();
|
||||||
|
|
||||||
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
|
|
||||||
return this.service.convert(value, typeDescriptor);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
return this.service.canConvert(sourceType, targetType);
|
return this.service.canConvert(sourceType, targetType);
|
||||||
}
|
}
|
||||||
|
|
@ -152,7 +187,6 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||||
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) throws EvaluationException {
|
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) throws EvaluationException {
|
||||||
return this.service.convert(value, sourceType, targetType);
|
return this.service.convert(value, sourceType, targetType);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,10 +104,5 @@ class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware {
|
||||||
}
|
}
|
||||||
return delegate.convertIfNecessary(value, targetType.getType());
|
return delegate.convertIfNecessary(value, targetType.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertValue(Object value, TypeDescriptor targetType) {
|
|
||||||
return convertValue(value, TypeDescriptor.forObject(value), targetType);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue