Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2004-2008 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.binding.convert.service;
 import java.util.Map;
Base implementation of a conversion service. Initially empty, e.g. no converters are registered by default.

Keith Donald
 public class GenericConversionService implements ConversionService {

An indexed map of converters. Each entry key is a source class that can be converted from, and each entry value is a map of target classes that can be convertered to, ultimately mapping to a specific converter that can perform the source->target conversion.
 	private Map sourceClassConverters = new HashMap();

An optional parent conversion service.
Returns the parent of this conversion service. Could be null.
 		return ;

Set the parent of this conversion service. This is optional.
 	public void setParent(ConversionService parent) {
 		this. = parent;

Add given converter to this conversion service.

converter the converter
 	public void addConverter(Converter converter) {
 		Class[] sourceClasses = converter.getSourceClasses();
 		Class[] targetClasses = converter.getTargetClasses();
 		for (int i = 0; i < sourceClasses.lengthi++) {
 			Class sourceClass = sourceClasses[i];
 			Map sourceMap = (Map.get(sourceClass);
 			if (sourceMap == null) {
 				sourceMap = new HashMap();
 			for (int j = 0; j < targetClasses.lengthj++) {
 				Class targetClass = targetClasses[j];
 	public ConversionExecutor getConversionExecutor(Class sourceClassClass targetClassthrows ConversionException {
 		Assert.notNull(sourceClass"The source class to convert from is required");
 		Assert.notNull(targetClass"The target class to convert to is required");
 		if (this. == null || this..isEmpty()) {
 			throw new IllegalStateException("No converters have been added to this service's registry");
 		sourceClass = convertToWrapperClassIfNecessary(sourceClass);
 		targetClass = convertToWrapperClassIfNecessary(targetClass);
 		if (targetClass.isAssignableFrom(sourceClass)) {
 			return new StaticConversionExecutor(sourceClasstargetClassnew NoOpConverter(sourceClasstargetClass));
 		Map sourceTargetConverters = findConvertersForSource(sourceClass);
 		Converter converter = findTargetConverter(sourceTargetConverterstargetClass);
 		if (converter != null) {
 			// we found a converter
 			return new StaticConversionExecutor(sourceClasstargetClassconverter);
 		} else {
			if ( != null) {
				// try the parent
				return .getConversionExecutor(sourceClasstargetClass);
else {
				throw new ConversionException(sourceClasstargetClass,
						"No converter registered to convert from sourceClass '" + sourceClass + "' to target class '"
targetClass + "'");
	// subclassing support
Returns an indexed map of converters. Each entry key is a source class that can be converted from, and each entry value is a map of target classes that can be convertered to, ultimately mapping to a specific converter that can perform the source->target conversion.

Returns a registered converter object

sourceClass the source class
targetClass the target class
	protected Converter getConverter(Class sourceClassClass targetClass) {
		Map sourceTargetConverters = findConvertersForSource(sourceClass);
		return findTargetConverter(sourceTargetConverterstargetClass);
	// internal helpers
	private Map findConvertersForSource(Class sourceClass) {
		LinkedList classQueue = new LinkedList();
		while (!classQueue.isEmpty()) {
			sourceClass = (ClassclassQueue.removeLast();
			Map sourceTargetConverters = (Map.get(sourceClass);
			if (sourceTargetConverters != null && !sourceTargetConverters.isEmpty()) {
				return sourceTargetConverters;
			if (!sourceClass.isInterface() && (sourceClass.getSuperclass() != null)) {
			// queue up source class's implemented interfaces.
			Class[] interfaces = sourceClass.getInterfaces();
			for (int i = 0; i < interfaces.lengthi++) {
	private Converter findTargetConverter(Map sourceTargetConvertersClass targetClass) {
		LinkedList classQueue = new LinkedList();
		while (!classQueue.isEmpty()) {
			targetClass = (ClassclassQueue.removeLast();
			Converter converter = (ConvertersourceTargetConverters.get(targetClass);
			if (converter != null) {
				return converter;
			if (!targetClass.isInterface() && (targetClass.getSuperclass() != null)) {
			// queue up target class's implemented interfaces.
			Class[] interfaces = targetClass.getInterfaces();
			for (int i = 0; i < interfaces.lengthi++) {
		return null;
		if (targetType.isPrimitive()) {
			if (targetType.equals(int.class)) {
				return Integer.class;
else if (targetType.equals(short.class)) {
				return Short.class;
else if (targetType.equals(long.class)) {
				return Long.class;
else if (targetType.equals(float.class)) {
				return Float.class;
else if (targetType.equals(double.class)) {
				return Double.class;
else if (targetType.equals(byte.class)) {
				return Byte.class;
else if (targetType.equals(boolean.class)) {
				return Boolean.class;
else if (targetType.equals(char.class)) {
				return Character.class;
else {
				throw new IllegalStateException("Should never happen - primitive type is not a primitive?");
else {
			return targetType;
New to GrepCode? Check out our FAQ X