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.io.PrintStream;
  18. import java.io.PrintWriter;
  19. /**
  20. * Combined exception, composed of individual binding propertyAccessExceptions.
  21. * An object of this class is created at the beginning of the binding
  22. * process, and errors added to it as necessary.
  23. *
  24. * <p>The binding process continues when it encounters application-level
  25. * propertyAccessExceptions, applying those changes that can be applied and storing
  26. * rejected changes in an object of this class.
  27. *
  28. * @author Rod Johnson
  29. * @author Juergen Hoeller
  30. * @since 18 April 2001
  31. * @version $Id: PropertyAccessExceptionsException.java,v 1.6 2004/06/02 00:47:17 jhoeller Exp $
  32. */
  33. public class PropertyAccessExceptionsException extends BeansException {
  34. /** BeanWrapper wrapping the target object for binding */
  35. private final BeanWrapper beanWrapper;
  36. /** List of PropertyAccessException objects */
  37. private final PropertyAccessException[] propertyAccessExceptions;
  38. /**
  39. * Create a new PropertyAccessExceptionsException.
  40. * @param beanWrapper the BeanWrapper that wraps the target object
  41. * @param propertyAccessExceptions the List of PropertyAccessExceptions
  42. */
  43. public PropertyAccessExceptionsException(BeanWrapper beanWrapper,
  44. PropertyAccessException[] propertyAccessExceptions) {
  45. super("");
  46. this.beanWrapper = beanWrapper;
  47. this.propertyAccessExceptions = propertyAccessExceptions;
  48. }
  49. /**
  50. * Return the BeanWrapper that generated this exception.
  51. */
  52. public BeanWrapper getBeanWrapper() {
  53. return beanWrapper;
  54. }
  55. /**
  56. * Return the object we're binding to.
  57. */
  58. public Object getBindObject() {
  59. return this.beanWrapper.getWrappedInstance();
  60. }
  61. /**
  62. * If this returns 0, no errors were encountered during binding.
  63. */
  64. public int getExceptionCount() {
  65. return this.propertyAccessExceptions.length;
  66. }
  67. /**
  68. * Return an array of the propertyAccessExceptions stored in this object.
  69. * Will return the empty array (not null) if there were no errors.
  70. */
  71. public PropertyAccessException[] getPropertyAccessExceptions() {
  72. return this.propertyAccessExceptions;
  73. }
  74. /**
  75. * Return the exception for this field, or null if there isn't one.
  76. */
  77. public PropertyAccessException getPropertyAccessException(String propertyName) {
  78. for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
  79. PropertyAccessException pae = this.propertyAccessExceptions[i];
  80. if (propertyName.equals(pae.getPropertyChangeEvent().getPropertyName())) {
  81. return pae;
  82. }
  83. }
  84. return null;
  85. }
  86. public String getMessage() {
  87. StringBuffer sb = new StringBuffer();
  88. sb.append(this.toString());
  89. sb.append("; nested propertyAccessExceptions are: ");
  90. for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
  91. PropertyAccessException pae = this.propertyAccessExceptions[i];
  92. sb.append("[");
  93. sb.append(pae.getClass().getName());
  94. sb.append(": ");
  95. sb.append(pae.getMessage());
  96. sb.append(']');
  97. if (i < this.propertyAccessExceptions.length - 1) {
  98. sb.append(", ");
  99. }
  100. }
  101. return sb.toString();
  102. }
  103. public void printStackTrace(PrintStream ps) {
  104. ps.println(this);
  105. for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
  106. PropertyAccessException pae = this.propertyAccessExceptions[i];
  107. pae.printStackTrace(ps);
  108. }
  109. }
  110. public void printStackTrace(PrintWriter pw) {
  111. pw.println(this);
  112. for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
  113. PropertyAccessException pae = this.propertyAccessExceptions[i];
  114. pae.printStackTrace(pw);
  115. }
  116. }
  117. public String toString() {
  118. return "PropertyAccessExceptionsException (" + getExceptionCount() + " errors)";
  119. }
  120. }