001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.expression.common; 018 019import org.springframework.core.convert.TypeDescriptor; 020import org.springframework.expression.EvaluationContext; 021import org.springframework.expression.EvaluationException; 022import org.springframework.expression.TypeConverter; 023import org.springframework.expression.TypedValue; 024import org.springframework.lang.Nullable; 025import org.springframework.util.ClassUtils; 026 027/** 028 * Common utility functions that may be used by any Expression Language provider. 029 * 030 * @author Andy Clement 031 * @author Juergen Hoeller 032 * @since 3.0 033 */ 034public abstract class ExpressionUtils { 035 036 /** 037 * Determines if there is a type converter available in the specified context and 038 * attempts to use it to convert the supplied value to the specified type. Throws an 039 * exception if conversion is not possible. 040 * @param context the evaluation context that may define a type converter 041 * @param typedValue the value to convert and a type descriptor describing it 042 * @param targetType the type to attempt conversion to 043 * @return the converted value 044 * @throws EvaluationException if there is a problem during conversion or conversion 045 * of the value to the specified type is not supported 046 */ 047 @SuppressWarnings("unchecked") 048 @Nullable 049 public static <T> T convertTypedValue( 050 @Nullable EvaluationContext context, TypedValue typedValue, @Nullable Class<T> targetType) { 051 052 Object value = typedValue.getValue(); 053 if (targetType == null) { 054 return (T) value; 055 } 056 if (context != null) { 057 return (T) context.getTypeConverter().convertValue( 058 value, typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType)); 059 } 060 if (ClassUtils.isAssignableValue(targetType, value)) { 061 return (T) value; 062 } 063 throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'"); 064 } 065 066 /** 067 * Attempt to convert a typed value to an int using the supplied type converter. 068 */ 069 public static int toInt(TypeConverter typeConverter, TypedValue typedValue) { 070 return convertValue(typeConverter, typedValue, Integer.class); 071 } 072 073 /** 074 * Attempt to convert a typed value to a boolean using the supplied type converter. 075 */ 076 public static boolean toBoolean(TypeConverter typeConverter, TypedValue typedValue) { 077 return convertValue(typeConverter, typedValue, Boolean.class); 078 } 079 080 /** 081 * Attempt to convert a typed value to a double using the supplied type converter. 082 */ 083 public static double toDouble(TypeConverter typeConverter, TypedValue typedValue) { 084 return convertValue(typeConverter, typedValue, Double.class); 085 } 086 087 /** 088 * Attempt to convert a typed value to a long using the supplied type converter. 089 */ 090 public static long toLong(TypeConverter typeConverter, TypedValue typedValue) { 091 return convertValue(typeConverter, typedValue, Long.class); 092 } 093 094 /** 095 * Attempt to convert a typed value to a char using the supplied type converter. 096 */ 097 public static char toChar(TypeConverter typeConverter, TypedValue typedValue) { 098 return convertValue(typeConverter, typedValue, Character.class); 099 } 100 101 /** 102 * Attempt to convert a typed value to a short using the supplied type converter. 103 */ 104 public static short toShort(TypeConverter typeConverter, TypedValue typedValue) { 105 return convertValue(typeConverter, typedValue, Short.class); 106 } 107 108 /** 109 * Attempt to convert a typed value to a float using the supplied type converter. 110 */ 111 public static float toFloat(TypeConverter typeConverter, TypedValue typedValue) { 112 return convertValue(typeConverter, typedValue, Float.class); 113 } 114 115 /** 116 * Attempt to convert a typed value to a byte using the supplied type converter. 117 */ 118 public static byte toByte(TypeConverter typeConverter, TypedValue typedValue) { 119 return convertValue(typeConverter, typedValue, Byte.class); 120 } 121 122 @SuppressWarnings("unchecked") 123 private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Class<T> targetType) { 124 Object result = typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), 125 TypeDescriptor.valueOf(targetType)); 126 if (result == null) { 127 throw new IllegalStateException("Null conversion result for value [" + typedValue.getValue() + "]"); 128 } 129 return (T) result; 130 } 131 132}