001: /*
002: * Copyright 2002-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:
017: package org.springframework.beans;
018:
019: import java.beans.PropertyChangeEvent;
020:
021: import org.springframework.util.ClassUtils;
022:
023: /**
024: * Exception thrown on a type mismatch when trying to set a bean property.
025: *
026: * @author Rod Johnson
027: * @author Juergen Hoeller
028: */
029: public class TypeMismatchException extends PropertyAccessException {
030:
031: /**
032: * Error code that a type mismatch error will be registered with.
033: */
034: public static final String ERROR_CODE = "typeMismatch";
035:
036: private transient Object value;
037:
038: private Class requiredType;
039:
040: /**
041: * Create a new TypeMismatchException.
042: * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
043: * @param requiredType the required target type
044: */
045: public TypeMismatchException(
046: PropertyChangeEvent propertyChangeEvent, Class requiredType) {
047: this (propertyChangeEvent, requiredType, null);
048: }
049:
050: /**
051: * Create a new TypeMismatchException.
052: * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
053: * @param requiredType the required target type (or <code>null</code> if not known)
054: * @param cause the root cause (may be <code>null</code>)
055: */
056: public TypeMismatchException(
057: PropertyChangeEvent propertyChangeEvent,
058: Class requiredType, Throwable cause) {
059: super (
060: propertyChangeEvent,
061: "Failed to convert property value of type ["
062: + (propertyChangeEvent.getNewValue() != null ? ClassUtils
063: .getQualifiedName(propertyChangeEvent
064: .getNewValue().getClass())
065: : null)
066: + "]"
067: + (requiredType != null ? " to required type ["
068: + ClassUtils
069: .getQualifiedName(requiredType)
070: + "]" : "")
071: + (propertyChangeEvent.getPropertyName() != null ? " for property '"
072: + propertyChangeEvent.getPropertyName()
073: + "'"
074: : ""), cause);
075: this .value = propertyChangeEvent.getNewValue();
076: this .requiredType = requiredType;
077: }
078:
079: /**
080: * Create a new TypeMismatchException without PropertyChangeEvent.
081: * @param value the offending value that couldn't be converted (may be <code>null</code>)
082: * @param requiredType the required target type (or <code>null</code> if not known)
083: */
084: public TypeMismatchException(Object value, Class requiredType) {
085: this (value, requiredType, null);
086: }
087:
088: /**
089: * Create a new TypeMismatchException without PropertyChangeEvent.
090: * @param value the offending value that couldn't be converted (may be <code>null</code>)
091: * @param requiredType the required target type (or <code>null</code> if not known)
092: * @param cause the root cause (may be <code>null</code>)
093: */
094: public TypeMismatchException(Object value, Class requiredType,
095: Throwable cause) {
096: super ("Failed to convert value of type ["
097: + (value != null ? ClassUtils.getQualifiedName(value
098: .getClass()) : null)
099: + "]"
100: + (requiredType != null ? " to required type ["
101: + ClassUtils.getQualifiedName(requiredType)
102: + "]" : ""), cause);
103: this .value = value;
104: this .requiredType = requiredType;
105: }
106:
107: /**
108: * Return the offending value (may be <code>null</code>)
109: */
110: public Object getValue() {
111: return this .value;
112: }
113:
114: /**
115: * Return the required target type, if any.
116: */
117: public Class getRequiredType() {
118: return this .requiredType;
119: }
120:
121: public String getErrorCode() {
122: return ERROR_CODE;
123: }
124:
125: }
|