TypeMismatchException.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.beans;
  17. import java.beans.PropertyChangeEvent;
  18. /**
  19.  * Exception thrown on a type mismatch when trying to set a bean property.
  20.  * @author Rod Johnson
  21.  * @author Juergen Hoeller
  22.  */
  23. public class TypeMismatchException extends PropertyAccessException {
  24. private final Class requiredType;
  25. /**
  26.  * Create a new TypeMismatchException.
  27.  * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
  28.  * @param requiredType the required target type
  29.  */
  30. public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, Class requiredType) {
  31. this(propertyChangeEvent, requiredType, null);
  32. }
  33. /**
  34.  * Create a new TypeMismatchException.
  35.  * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
  36.  * @param requiredType the required target type
  37.  * @param ex the root cause
  38.  */
  39. public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, Class requiredType, Throwable ex) {
  40. super(propertyChangeEvent,
  41. "Failed to convert property value of type [" +
  42. (propertyChangeEvent.getNewValue() != null ?
  43. propertyChangeEvent.getNewValue().getClass().getName() : null) +
  44. "] to required type [" + requiredType.getName() + "]" +
  45. (propertyChangeEvent.getPropertyName() != null ?
  46. " for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
  47. ex);
  48. this.requiredType = requiredType;
  49. }
  50. /**
  51.  * Return the required target type.
  52.  */
  53. public Class getRequiredType() {
  54. return requiredType;
  55. }
  56. public String getErrorCode() {
  57. return "typeMismatch";
  58. }
  59. }