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