KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > TypeMismatchException


1 /*
2  * Copyright 2002-2006 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.beans;
18
19 import java.beans.PropertyChangeEvent JavaDoc;
20
21 import org.springframework.util.ClassUtils;
22
23 /**
24  * Exception thrown on a type mismatch when trying to set a bean property.
25  *
26  * @author Rod Johnson
27  * @author Juergen Hoeller
28  */

29 public class TypeMismatchException extends PropertyAccessException {
30
31     /**
32      * Error code that a type mismatch error will be registered with.
33      */

34     public static final String JavaDoc ERROR_CODE = "typeMismatch";
35
36
37     private Object JavaDoc value;
38
39     private Class JavaDoc requiredType;
40
41
42     /**
43      * Create a new TypeMismatchException.
44      * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
45      * @param requiredType the required target type
46      */

47     public TypeMismatchException(PropertyChangeEvent JavaDoc propertyChangeEvent, Class JavaDoc requiredType) {
48         this(propertyChangeEvent, requiredType, null);
49     }
50
51     /**
52      * Create a new TypeMismatchException.
53      * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
54      * @param requiredType the required target type (or <code>null</code> if not known)
55      * @param cause the root cause (may be <code>null</code>)
56      */

57     public TypeMismatchException(PropertyChangeEvent JavaDoc propertyChangeEvent, Class JavaDoc requiredType, Throwable JavaDoc cause) {
58         super(propertyChangeEvent,
59                 "Failed to convert property value of type [" +
60                 (propertyChangeEvent.getNewValue() != null ?
61                  ClassUtils.getQualifiedName(propertyChangeEvent.getNewValue().getClass()) : null) + "]" +
62                 (requiredType != null ?
63                  " to required type [" + ClassUtils.getQualifiedName(requiredType) + "]" : "") +
64                 (propertyChangeEvent.getPropertyName() != null ?
65                  " for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
66                 cause);
67         this.value = propertyChangeEvent.getNewValue();
68         this.requiredType = requiredType;
69     }
70
71     /**
72      * Create a new TypeMismatchException without PropertyChangeEvent.
73      * @param value the offending value that couldn't be converted (may be <code>null</code>)
74      * @param requiredType the required target type (or <code>null</code> if not known)
75      */

76     public TypeMismatchException(Object JavaDoc value, Class JavaDoc requiredType) {
77         this(value, requiredType, null);
78     }
79
80     /**
81      * Create a new TypeMismatchException without PropertyChangeEvent.
82      * @param value the offending value that couldn't be converted (may be <code>null</code>)
83      * @param requiredType the required target type (or <code>null</code> if not known)
84      * @param cause the root cause (may be <code>null</code>)
85      */

86     public TypeMismatchException(Object JavaDoc value, Class JavaDoc requiredType, Throwable JavaDoc cause) {
87         super("Failed to convert value of type [" +
88                 (value != null ?
89                  ClassUtils.getQualifiedName(value.getClass()) : null) + "]" +
90                 (requiredType != null ?
91                  " to required type [" + ClassUtils.getQualifiedName(requiredType) + "]" : ""),
92                 cause);
93         this.value = value;
94         this.requiredType = requiredType;
95     }
96
97
98     /**
99      * Return the offending value (may be <code>null</code>)
100      */

101     public Object JavaDoc getValue() {
102         return value;
103     }
104
105     /**
106      * Return the required target type, if any.
107      */

108     public Class JavaDoc getRequiredType() {
109         return requiredType;
110     }
111
112     public String JavaDoc getErrorCode() {
113         return ERROR_CODE;
114     }
115
116 }
117
Popular Tags