001: /*
002: * Copyright 2004-2007 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: * http://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: package org.springframework.binding.convert;
017:
018: import org.springframework.core.NestedRuntimeException;
019:
020: /**
021: * Base class for exceptions thrown by the type conversion system.
022: *
023: * @author Keith Donald
024: */
025: public class ConversionException extends NestedRuntimeException {
026:
027: /**
028: * The source type we tried to convert from
029: */
030: private Class sourceClass;
031:
032: /**
033: * The value we tried to convert.
034: * Transient because we cannot guarantee that the value is Serializable.
035: */
036: private transient Object value;
037:
038: /**
039: * The target type we tried to convert to.
040: */
041: private Class targetClass;
042:
043: /**
044: * Creates a new conversion exception.
045: * @param value the value we tried to convert
046: * @param targetClass the target type
047: */
048: public ConversionException(Object value, Class targetClass) {
049: super ("Unable to convert value '" + value + "' of type '"
050: + (value != null ? value.getClass().getName() : null)
051: + "' to class '" + targetClass.getName() + "'");
052: this .value = value;
053: this .targetClass = targetClass;
054: }
055:
056: /**
057: * Creates a new conversion exception.
058: * @param value the value we tried to convert
059: * @param targetClass the target type
060: * @param cause underlying cause of this exception
061: */
062: public ConversionException(Object value, Class targetClass,
063: Throwable cause) {
064: super ("Unable to convert value '" + value + "' of type '"
065: + (value != null ? value.getClass().getName() : null)
066: + "' to class '" + targetClass.getName() + "'", cause);
067: this .value = value;
068: this .targetClass = targetClass;
069: }
070:
071: /**
072: * Creates a new conversion exception.
073: * @param value the value we tried to convert
074: * @param targetClass the target type
075: * @param message a descriptive message
076: * @param cause underlying cause of this exception
077: */
078: public ConversionException(Object value, Class targetClass,
079: String message, Throwable cause) {
080: super (message, cause);
081: this .value = value;
082: this .targetClass = targetClass;
083: }
084:
085: /**
086: * Creates a new conversion exception.
087: * @param sourceClass the source type
088: * @param targetClass the target type
089: * @param message a descriptive message
090: */
091: public ConversionException(Class sourceClass, Class targetClass,
092: String message) {
093: super (message);
094: this .sourceClass = sourceClass;
095: this .value = null; // not available
096: this .targetClass = targetClass;
097: }
098:
099: /**
100: * Creates a new conversion exception.
101: * @param sourceClass the source type
102: * @param message a descriptive message
103: */
104: public ConversionException(Class sourceClass, String message) {
105: super (message);
106: this .sourceClass = sourceClass;
107: this .value = null; // not available
108: this .targetClass = null; // not available
109: }
110:
111: /**
112: * Creates a new conversion exception.
113: * @param sourceClass the source type
114: * @param value the value we tried to convert
115: * @param targetClass the target type
116: * @param message a descriptive message
117: */
118: public ConversionException(Class sourceClass, Object value,
119: Class targetClass, String message) {
120: super (message);
121: this .sourceClass = sourceClass;
122: this .value = value;
123: this .targetClass = targetClass;
124: }
125:
126: /**
127: * Returns the source type.
128: */
129: public Class getSourceClass() {
130: return sourceClass;
131: }
132:
133: /**
134: * Returns the value we tried to convert.
135: */
136: public Object getValue() {
137: return value;
138: }
139:
140: /**
141: * Returns the target type.
142: */
143: public Class getTargetClass() {
144: return targetClass;
145: }
146: }
|