001/*
002 * Copyright 2002-2018 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.spel.support;
018
019import org.springframework.core.convert.ConversionException;
020import org.springframework.core.convert.ConversionService;
021import org.springframework.core.convert.TypeDescriptor;
022import org.springframework.core.convert.support.DefaultConversionService;
023import org.springframework.expression.TypeConverter;
024import org.springframework.expression.spel.SpelEvaluationException;
025import org.springframework.expression.spel.SpelMessage;
026import org.springframework.lang.Nullable;
027import org.springframework.util.Assert;
028
029/**
030 * Default implementation of the {@link TypeConverter} interface,
031 * delegating to a core Spring {@link ConversionService}.
032 *
033 * @author Juergen Hoeller
034 * @author Andy Clement
035 * @since 3.0
036 * @see org.springframework.core.convert.ConversionService
037 */
038public class StandardTypeConverter implements TypeConverter {
039
040        private final ConversionService conversionService;
041
042
043        /**
044         * Create a StandardTypeConverter for the default ConversionService.
045         * @see DefaultConversionService#getSharedInstance()
046         */
047        public StandardTypeConverter() {
048                this.conversionService = DefaultConversionService.getSharedInstance();
049        }
050
051        /**
052         * Create a StandardTypeConverter for the given ConversionService.
053         * @param conversionService the ConversionService to delegate to
054         */
055        public StandardTypeConverter(ConversionService conversionService) {
056                Assert.notNull(conversionService, "ConversionService must not be null");
057                this.conversionService = conversionService;
058        }
059
060
061        @Override
062        public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
063                return this.conversionService.canConvert(sourceType, targetType);
064        }
065
066        @Override
067        @Nullable
068        public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
069                try {
070                        return this.conversionService.convert(value, sourceType, targetType);
071                }
072                catch (ConversionException ex) {
073                        throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
074                                        (sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")),
075                                        targetType.toString());
076                }
077        }
078
079}