added TypeDescriptor resolveCollectionElement and Map key/value types
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.expression;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
@@ -31,12 +34,10 @@ public class TypedValue {
|
||||
|
||||
public static final TypedValue NULL = new TypedValue(null);
|
||||
|
||||
|
||||
private final Object value;
|
||||
|
||||
private TypeDescriptor typeDescriptor;
|
||||
|
||||
|
||||
/**
|
||||
* Create a TypedValue for a simple object. The type descriptor is inferred
|
||||
* from the object, so no generic information is preserved.
|
||||
@@ -44,7 +45,8 @@ public class TypedValue {
|
||||
*/
|
||||
public TypedValue(Object value) {
|
||||
this.value = value;
|
||||
this.typeDescriptor = null; // initialized when/if requested
|
||||
// initialized when/if requested
|
||||
this.typeDescriptor = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,10 +56,9 @@ public class TypedValue {
|
||||
*/
|
||||
public TypedValue(Object value, TypeDescriptor typeDescriptor) {
|
||||
this.value = value;
|
||||
this.typeDescriptor = typeDescriptor;
|
||||
this.typeDescriptor = initTypeDescriptor(value, typeDescriptor);
|
||||
}
|
||||
|
||||
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
@@ -69,12 +70,27 @@ public class TypedValue {
|
||||
return this.typeDescriptor;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append("TypedValue: '").append(this.value).append("' of [").append(getTypeDescriptor() + "]");
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
// interal helpers
|
||||
|
||||
private static TypeDescriptor initTypeDescriptor(Object value, TypeDescriptor typeDescriptor) {
|
||||
if (value == null) {
|
||||
return typeDescriptor;
|
||||
}
|
||||
if (typeDescriptor.isCollection() && Object.class.equals(typeDescriptor.getElementType())) {
|
||||
return typeDescriptor.resolveCollectionElementType((Collection<?>) value);
|
||||
} else if (typeDescriptor.isMap() && Object.class.equals(typeDescriptor.getMapKeyType())
|
||||
&& Object.class.equals(typeDescriptor.getMapValueType())){
|
||||
return typeDescriptor.resolveMapKeyValueTypes((Map<?, ?>) value);
|
||||
} else {
|
||||
return typeDescriptor;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -90,13 +90,9 @@ public class Indexer extends SpelNodeImpl {
|
||||
|
||||
// Indexing into a Map
|
||||
if (targetObject instanceof Map) {
|
||||
if (targetObjectTypeDescriptor.isMap()) {
|
||||
Object possiblyConvertedKey = state.convertValue(index, targetObjectTypeDescriptor.getMapKeyTypeDescriptor());
|
||||
Object o = ((Map<?, ?>) targetObject).get(possiblyConvertedKey);
|
||||
return new TypedValue(o, targetObjectTypeDescriptor.getMapValueTypeDescriptor());
|
||||
} else {
|
||||
return new TypedValue(((Map<?, ?>) targetObject).get(index));
|
||||
}
|
||||
Object possiblyConvertedKey = state.convertValue(index, targetObjectTypeDescriptor.getMapKeyTypeDescriptor());
|
||||
Object o = ((Map<?, ?>) targetObject).get(possiblyConvertedKey);
|
||||
return new TypedValue(o, targetObjectTypeDescriptor.getMapValueTypeDescriptor());
|
||||
}
|
||||
|
||||
if (targetObject == null) {
|
||||
|
||||
Reference in New Issue
Block a user