|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
34 | 34 | import org.springframework.boot.convert.ApplicationConversionService;
|
35 | 35 | import org.springframework.core.ResolvableType;
|
36 | 36 | import org.springframework.core.convert.ConversionException;
|
| 37 | +import org.springframework.core.convert.ConversionFailedException; |
37 | 38 | import org.springframework.core.convert.ConversionService;
|
| 39 | +import org.springframework.core.convert.ConverterNotFoundException; |
38 | 40 | import org.springframework.core.convert.TypeDescriptor;
|
39 | 41 | import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
40 | 42 | import org.springframework.core.convert.support.GenericConversionService;
|
@@ -157,17 +159,20 @@ public <T> T convert(Object source, Class<T> targetType) {
|
157 | 159 |
|
158 | 160 | @Override
|
159 | 161 | public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
160 |
| - for (int i = 0; i < this.delegates.size() - 1; i++) { |
| 162 | + ConversionException failure = null; |
| 163 | + for (ConversionService delegate : this.delegates) { |
161 | 164 | try {
|
162 |
| - ConversionService delegate = this.delegates.get(i); |
163 | 165 | if (delegate.canConvert(sourceType, targetType)) {
|
164 | 166 | return delegate.convert(source, sourceType, targetType);
|
165 | 167 | }
|
166 | 168 | }
|
167 | 169 | catch (ConversionException ex) {
|
| 170 | + if (failure == null && ex instanceof ConversionFailedException) { |
| 171 | + failure = ex; |
| 172 | + } |
168 | 173 | }
|
169 | 174 | }
|
170 |
| - return this.delegates.get(this.delegates.size() - 1).convert(source, sourceType, targetType); |
| 175 | + throw (failure != null) ? failure : new ConverterNotFoundException(sourceType, targetType); |
171 | 176 | }
|
172 | 177 |
|
173 | 178 | }
|
|
0 commit comments