001/*
002 * Copyright 2002-2014 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.util.ClassUtils;
025
026/**
027 * Common utility functions that may be used by any Expression Language provider.
028 *
029 * @author Andy Clement
030 * @author Juergen Hoeller
031 * @since 3.0
032 */
033public abstract class ExpressionUtils {
034
035        /**
036         * Determines if there is a type converter available in the specified context and
037         * attempts to use it to convert the supplied value to the specified type. Throws an
038         * exception if conversion is not possible.
039         * @param context the evaluation context that may define a type converter
040         * @param typedValue the value to convert and a type descriptor describing it
041         * @param targetType the type to attempt conversion to
042         * @return the converted value
043         * @throws EvaluationException if there is a problem during conversion or conversion
044         * of the value to the specified type is not supported
045         */
046        @SuppressWarnings("unchecked")
047        public static <T> T convertTypedValue(EvaluationContext context, TypedValue typedValue, Class<T> targetType) {
048                Object value = typedValue.getValue();
049                if (targetType == null) {
050                        return (T) value;
051                }
052                if (context != null) {
053                        return (T) context.getTypeConverter().convertValue(
054                                        value, typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType));
055                }
056                if (ClassUtils.isAssignableValue(targetType, value)) {
057                        return (T) value;
058                }
059                throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
060        }
061
062        /**
063         * Attempt to convert a typed value to an int using the supplied type converter.
064         */
065        public static int toInt(TypeConverter typeConverter, TypedValue typedValue) {
066                return (Integer) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
067                                TypeDescriptor.valueOf(Integer.class));
068        }
069
070        /**
071         * Attempt to convert a typed value to a boolean using the supplied type converter.
072         */
073        public static boolean toBoolean(TypeConverter typeConverter, TypedValue typedValue) {
074                return (Boolean) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
075                                TypeDescriptor.valueOf(Boolean.class));
076        }
077
078        /**
079         * Attempt to convert a typed value to a double using the supplied type converter.
080         */
081        public static double toDouble(TypeConverter typeConverter, TypedValue typedValue) {
082                return (Double) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
083                                TypeDescriptor.valueOf(Double.class));
084        }
085
086        /**
087         * Attempt to convert a typed value to a long using the supplied type converter.
088         */
089        public static long toLong(TypeConverter typeConverter, TypedValue typedValue) {
090                return (Long) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
091                                TypeDescriptor.valueOf(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 (Character) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
099                                TypeDescriptor.valueOf(Character.class));
100        }
101
102        /**
103         * Attempt to convert a typed value to a short using the supplied type converter.
104         */
105        public static short toShort(TypeConverter typeConverter, TypedValue typedValue) {
106                return (Short) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
107                                TypeDescriptor.valueOf(Short.class));
108        }
109
110        /**
111         * Attempt to convert a typed value to a float using the supplied type converter.
112         */
113        public static float toFloat(TypeConverter typeConverter, TypedValue typedValue) {
114                return (Float) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
115                                TypeDescriptor.valueOf(Float.class));
116        }
117
118        /**
119         * Attempt to convert a typed value to a byte using the supplied type converter.
120         */
121        public static byte toByte(TypeConverter typeConverter, TypedValue typedValue) {
122                return (Byte) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(),
123                                TypeDescriptor.valueOf(Byte.class));
124        }
125
126}