001/*
002 * Copyright 2002-2019 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.beans;
018
019import java.lang.reflect.Field;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.core.convert.ConversionException;
023import org.springframework.core.convert.ConverterNotFoundException;
024import org.springframework.core.convert.TypeDescriptor;
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * Base implementation of the {@link TypeConverter} interface, using a package-private delegate.
030 * Mainly serves as base class for {@link BeanWrapperImpl}.
031 *
032 * @author Juergen Hoeller
033 * @since 3.2
034 * @see SimpleTypeConverter
035 */
036public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport implements TypeConverter {
037
038        @Nullable
039        TypeConverterDelegate typeConverterDelegate;
040
041
042        @Override
043        @Nullable
044        public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType) throws TypeMismatchException {
045                return convertIfNecessary(value, requiredType, TypeDescriptor.valueOf(requiredType));
046        }
047
048        @Override
049        @Nullable
050        public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,
051                        @Nullable MethodParameter methodParam) throws TypeMismatchException {
052
053                return convertIfNecessary(value, requiredType,
054                                (methodParam != null ? new TypeDescriptor(methodParam) : TypeDescriptor.valueOf(requiredType)));
055        }
056
057        @Override
058        @Nullable
059        public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType, @Nullable Field field)
060                        throws TypeMismatchException {
061
062                return convertIfNecessary(value, requiredType,
063                                (field != null ? new TypeDescriptor(field) : TypeDescriptor.valueOf(requiredType)));
064        }
065
066        @Nullable
067        @Override
068        public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,
069                        @Nullable TypeDescriptor typeDescriptor) throws TypeMismatchException {
070
071                Assert.state(this.typeConverterDelegate != null, "No TypeConverterDelegate");
072                try {
073                        return this.typeConverterDelegate.convertIfNecessary(null, null, value, requiredType, typeDescriptor);
074                }
075                catch (ConverterNotFoundException | IllegalStateException ex) {
076                        throw new ConversionNotSupportedException(value, requiredType, ex);
077                }
078                catch (ConversionException | IllegalArgumentException ex) {
079                        throw new TypeMismatchException(value, requiredType, ex);
080                }
081        }
082
083}