org.springframework.expression.TypeConverter Java Examples

The following examples show how to use org.springframework.expression.TypeConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Indexer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> T convertValue(TypeConverter converter, @Nullable Object value, Class<T> targetType) {
	T result = (T) converter.convertValue(
			value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(targetType));
	if (result == null) {
		throw new IllegalStateException("Null conversion result for index [" + value + "]");
	}
	return result;
}
 
Example #2
Source File: Indexer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public CollectionIndexingValueRef(Collection collection, int index, TypeDescriptor collectionEntryDescriptor,
		TypeConverter typeConverter, boolean growCollection, int maximumSize) {

	this.collection = collection;
	this.index = index;
	this.collectionEntryDescriptor = collectionEntryDescriptor;
	this.typeConverter = typeConverter;
	this.growCollection = growCollection;
	this.maximumSize = maximumSize;
}
 
Example #3
Source File: SimpleEvaluationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers,
		@Nullable TypeConverter converter, @Nullable TypedValue rootObject) {

	this.propertyAccessors = accessors;
	this.methodResolvers = resolvers;
	this.typeConverter = (converter != null ? converter : new StandardTypeConverter());
	this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL);
}
 
Example #4
Source File: StandardEvaluationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public TypeConverter getTypeConverter() {
	if (this.typeConverter == null) {
		 this.typeConverter = new StandardTypeConverter();
	}
	return this.typeConverter;
}
 
Example #5
Source File: ExpressionUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Class<T> targetType) {
	Object result = typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
			TypeDescriptor.valueOf(targetType));
	if (result == null) {
		throw new IllegalStateException("Null conversion result for value [" + typedValue.getValue() + "]");
	}
	return (T) result;
}
 
Example #6
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer, Class<?> componentType) {

	TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType);
	Object[] newObjectArray = (Object[]) newArray;
	for (int i = 0; i < newObjectArray.length; i++) {
		SpelNode elementNode = initializer.getChild(i);
		Object arrayEntry = elementNode.getValue(state);
		newObjectArray[i] = typeConverter.convertValue(arrayEntry,
				TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
	}
}
 
Example #7
Source File: ReflectionHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example #8
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void populateFloatArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	float[] newFloatArray = (float[]) newArray;
	for (int i = 0; i < newFloatArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newFloatArray[i] = ExpressionUtils.toFloat(typeConverter, typedValue);
	}
}
 
Example #9
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void populateDoubleArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	double[] newDoubleArray = (double[]) newArray;
	for (int i = 0; i < newDoubleArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newDoubleArray[i] = ExpressionUtils.toDouble(typeConverter, typedValue);
	}
}
 
Example #10
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void populateShortArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	short[] newShortArray = (short[]) newArray;
	for (int i = 0; i < newShortArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newShortArray[i] = ExpressionUtils.toShort(typeConverter, typedValue);
	}
}
 
Example #11
Source File: ConstructorReference.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer, Class<?> componentType) {

	TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType);
	Object[] newObjectArray = (Object[]) newArray;
	for (int i = 0; i < newObjectArray.length; i++) {
		SpelNode elementNode = initializer.getChild(i);
		Object arrayEntry = elementNode.getValue(state);
		newObjectArray[i] = typeConverter.convertValue(arrayEntry,
				TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
	}
}
 
Example #12
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void populateByteArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	byte[] newByteArray = (byte[]) newArray;
	for (int i = 0; i < newByteArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newByteArray[i] = ExpressionUtils.toByte(typeConverter, typedValue);
	}
}
 
Example #13
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void populateShortArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	short[] newShortArray = (short[]) newArray;
	for (int i = 0; i < newShortArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newShortArray[i] = ExpressionUtils.toShort(typeConverter, typedValue);
	}
}
 
Example #14
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void populateIntArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	int[] newIntArray = (int[]) newArray;
	for (int i = 0; i < newIntArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newIntArray[i] = ExpressionUtils.toInt(typeConverter, typedValue);
	}
}
 
Example #15
Source File: OpPlus.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Convert operand value to string using registered converter or using
 * {@code toString} method.
 * @param value typed value to be converted
 * @param state expression state
 * @return {@code TypedValue} instance converted to {@code String}
 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
	TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
	TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
	if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
		return String.valueOf(typeConverter.convertValue(value.getValue(),
				value.getTypeDescriptor(), typeDescriptor));
	}
	return String.valueOf(value.getValue());
}
 
Example #16
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void populateLongArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	long[] newLongArray = (long[]) newArray;
	for (int i = 0; i < newLongArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newLongArray[i] = ExpressionUtils.toLong(typeConverter, typedValue);
	}
}
 
Example #17
Source File: ReflectionHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example #18
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void populateBooleanArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	boolean[] newBooleanArray = (boolean[]) newArray;
	for (int i = 0; i < newBooleanArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newBooleanArray[i] = ExpressionUtils.toBoolean(typeConverter, typedValue);
	}
}
 
Example #19
Source File: ReflectionHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
@Nullable
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		// The user may supply null - and that will be ok unless a primitive is expected
		if (suppliedArg == null) {
			if (expectedArg.isPrimitive()) {
				match = null;
			}
		}
		else if (!expectedArg.equals(suppliedArg))  {
			if (suppliedArg.isAssignableTo(expectedArg)) {
				if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
					match = ArgumentsMatchKind.CLOSE;
				}
			}
			else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
				match = ArgumentsMatchKind.REQUIRES_CONVERSION;
			}
			else {
				match = null;
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example #20
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void populateFloatArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	float[] newFloatArray = (float[]) newArray;
	for (int i = 0; i < newFloatArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newFloatArray[i] = ExpressionUtils.toFloat(typeConverter, typedValue);
	}
}
 
Example #21
Source File: SimpleEvaluationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers,
		@Nullable TypeConverter converter, @Nullable TypedValue rootObject) {

	this.propertyAccessors = accessors;
	this.methodResolvers = resolvers;
	this.typeConverter = (converter != null ? converter : new StandardTypeConverter());
	this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL);
}
 
Example #22
Source File: StandardEvaluationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public TypeConverter getTypeConverter() {
	if (this.typeConverter == null) {
		this.typeConverter = new StandardTypeConverter();
	}
	return this.typeConverter;
}
 
Example #23
Source File: ExpressionUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Class<T> targetType) {
	Object result = typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
			TypeDescriptor.valueOf(targetType));
	if (result == null) {
		throw new IllegalStateException("Null conversion result for value [" + typedValue.getValue() + "]");
	}
	return (T) result;
}
 
Example #24
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void populateByteArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	byte[] newByteArray = (byte[]) newArray;
	for (int i = 0; i < newByteArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newByteArray[i] = ExpressionUtils.toByte(typeConverter, typedValue);
	}
}
 
Example #25
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void populateFloatArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	float[] newFloatArray = (float[]) newArray;
	for (int i = 0; i < newFloatArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newFloatArray[i] = ExpressionUtils.toFloat(typeConverter, typedValue);
	}
}
 
Example #26
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void populateLongArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	long[] newLongArray = (long[]) newArray;
	for (int i = 0; i < newLongArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newLongArray[i] = ExpressionUtils.toLong(typeConverter, typedValue);
	}
}
 
Example #27
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void populateShortArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	short[] newShortArray = (short[]) newArray;
	for (int i = 0; i < newShortArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newShortArray[i] = ExpressionUtils.toShort(typeConverter, typedValue);
	}
}
 
Example #28
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void populateLongArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	long[] newLongArray = (long[]) newArray;
	for (int i = 0; i < newLongArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newLongArray[i] = ExpressionUtils.toLong(typeConverter, typedValue);
	}
}
 
Example #29
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void populateBooleanArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	boolean[] newBooleanArray = (boolean[]) newArray;
	for (int i = 0; i < newBooleanArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newBooleanArray[i] = ExpressionUtils.toBoolean(typeConverter, typedValue);
	}
}
 
Example #30
Source File: OpPlus.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert operand value to string using registered converter or using
 * {@code toString} method.
 * @param value typed value to be converted
 * @param state expression state
 * @return {@code TypedValue} instance converted to {@code String}
 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
	TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
	TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
	if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
		return String.valueOf(typeConverter.convertValue(value.getValue(),
				value.getTypeDescriptor(), typeDescriptor));
	}
	return String.valueOf(value.getValue());
}