1 /* 2 * Copyright 2002-2011 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 * http://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.core.convert.support; 18 19 import org.springframework.core.convert.ConversionFailedException; 20 import org.springframework.core.convert.ConversionService; 21 import org.springframework.core.convert.TypeDescriptor; 22 import org.springframework.core.convert.converter.GenericConverter; 23 24 /** 25 * Internal utilities for the conversion package. 26 * 27 * @author Keith Donald 28 * @since 3.0 29 */ 30 abstract class ConversionUtils { 31 invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType, TypeDescriptor targetType)32 public static Object invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType, 33 TypeDescriptor targetType) { 34 try { 35 return converter.convert(source, sourceType, targetType); 36 } 37 catch (ConversionFailedException ex) { 38 throw ex; 39 } 40 catch (Exception ex) { 41 throw new ConversionFailedException(sourceType, targetType, source, ex); 42 } 43 } 44 canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType, ConversionService conversionService)45 public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType, ConversionService conversionService) { 46 if (targetElementType == null) { 47 // yes 48 return true; 49 } 50 if (sourceElementType == null) { 51 // maybe 52 return true; 53 } 54 if (conversionService.canConvert(sourceElementType, targetElementType)) { 55 // yes 56 return true; 57 } 58 else if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) { 59 // maybe; 60 return true; 61 } 62 else { 63 // no; 64 return false; 65 } 66 } 67 68 } 69