|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.convert; |
| 18 | + |
| 19 | +import java.text.ParseException; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.springframework.context.i18n.LocaleContextHolder; |
| 24 | +import org.springframework.core.DecoratingProxy; |
| 25 | +import org.springframework.core.GenericTypeResolver; |
| 26 | +import org.springframework.core.convert.TypeDescriptor; |
| 27 | +import org.springframework.core.convert.converter.Converter; |
| 28 | +import org.springframework.core.convert.converter.GenericConverter; |
| 29 | +import org.springframework.format.Parser; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * {@link Converter} to convert from a {@link String} to {@code <T>} using the underlying |
| 35 | + * {@link Parser}{@code <T>}. |
| 36 | + * |
| 37 | + * @author Dmytro Nosan |
| 38 | + * @since 2.2.0 |
| 39 | + */ |
| 40 | +public class ParserConverter implements GenericConverter { |
| 41 | + |
| 42 | + private final Class<?> type; |
| 43 | + |
| 44 | + private final Parser<?> parser; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a {@code Converter} to convert {@code String} to a {@code T} via parser. |
| 48 | + * @param parser parses {@code String} to a {@code T} |
| 49 | + */ |
| 50 | + public ParserConverter(Parser<?> parser) { |
| 51 | + Assert.notNull(parser, "Parser must not be null"); |
| 52 | + this.type = getType(parser); |
| 53 | + this.parser = parser; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Set<ConvertiblePair> getConvertibleTypes() { |
| 58 | + return Collections.singleton(new ConvertiblePair(String.class, this.type)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 63 | + String value = (String) source; |
| 64 | + if (!StringUtils.hasText(value)) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + try { |
| 68 | + return this.parser.parse(value, LocaleContextHolder.getLocale()); |
| 69 | + } |
| 70 | + catch (ParseException ex) { |
| 71 | + throw new IllegalArgumentException("Value [" + value + "] can not be parsed", ex); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString() { |
| 77 | + return String.class.getName() + " -> " + this.type.getName() + " : " + this.parser; |
| 78 | + } |
| 79 | + |
| 80 | + private static Class<?> getType(Parser<?> parser) { |
| 81 | + Class<?> type = GenericTypeResolver.resolveTypeArgument(parser.getClass(), Parser.class); |
| 82 | + if (type == null && parser instanceof DecoratingProxy) { |
| 83 | + type = GenericTypeResolver.resolveTypeArgument(((DecoratingProxy) parser).getDecoratedClass(), |
| 84 | + Parser.class); |
| 85 | + } |
| 86 | + if (type == null) { |
| 87 | + throw new IllegalArgumentException("Unable to extract the parameterized type from Parser: '" |
| 88 | + + parser.getClass().getName() + "'. Does the class parameterize the <T> generic type?"); |
| 89 | + } |
| 90 | + return type; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments