1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.beans.factory;
18
19 import java.io.PrintStream;
20 import java.io.PrintWriter;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import org.springframework.beans.FatalBeanException;
25 import org.springframework.core.NestedRuntimeException;
26
27
28
29
30
31
32
33 @SuppressWarnings("serial")
34 public class BeanCreationException extends FatalBeanException {
35
36 private String beanName;
37
38 private String resourceDescription;
39
40 private List<Throwable> relatedCauses;
41
42
43
44
45
46
47 public BeanCreationException(String msg) {
48 super(msg);
49 }
50
51
52
53
54
55
56 public BeanCreationException(String msg, Throwable cause) {
57 super(msg, cause);
58 }
59
60
61
62
63
64
65 public BeanCreationException(String beanName, String msg) {
66 super("Error creating bean with name '" + beanName + "': " + msg);
67 this.beanName = beanName;
68 }
69
70
71
72
73
74
75
76 public BeanCreationException(String beanName, String msg, Throwable cause) {
77 this(beanName, msg);
78 initCause(cause);
79 }
80
81
82
83
84
85
86
87
88 public BeanCreationException(String resourceDescription, String beanName, String msg) {
89 super("Error creating bean with name '" + beanName + "'" +
90 (resourceDescription != null ? " defined in " + resourceDescription : "") + ": " + msg);
91 this.resourceDescription = resourceDescription;
92 this.beanName = beanName;
93 }
94
95
96
97
98
99
100
101
102
103 public BeanCreationException(String resourceDescription, String beanName, String msg, Throwable cause) {
104 this(resourceDescription, beanName, msg);
105 initCause(cause);
106 }
107
108
109
110
111
112 public String getBeanName() {
113 return this.beanName;
114 }
115
116
117
118
119
120 public String getResourceDescription() {
121 return this.resourceDescription;
122 }
123
124
125
126
127
128
129
130 public void addRelatedCause(Throwable ex) {
131 if (this.relatedCauses == null) {
132 this.relatedCauses = new LinkedList<Throwable>();
133 }
134 this.relatedCauses.add(ex);
135 }
136
137
138
139
140
141 public Throwable[] getRelatedCauses() {
142 if (this.relatedCauses == null) {
143 return null;
144 }
145 return this.relatedCauses.toArray(new Throwable[this.relatedCauses.size()]);
146 }
147
148
149 @Override
150 public String toString() {
151 StringBuilder sb = new StringBuilder(super.toString());
152 if (this.relatedCauses != null) {
153 for (Throwable relatedCause : this.relatedCauses) {
154 sb.append("\nRelated cause: ");
155 sb.append(relatedCause);
156 }
157 }
158 return sb.toString();
159 }
160
161 @Override
162 public void printStackTrace(PrintStream ps) {
163 synchronized (ps) {
164 super.printStackTrace(ps);
165 if (this.relatedCauses != null) {
166 for (Throwable relatedCause : this.relatedCauses) {
167 ps.println("Related cause:");
168 relatedCause.printStackTrace(ps);
169 }
170 }
171 }
172 }
173
174 @Override
175 public void printStackTrace(PrintWriter pw) {
176 synchronized (pw) {
177 super.printStackTrace(pw);
178 if (this.relatedCauses != null) {
179 for (Throwable relatedCause : this.relatedCauses) {
180 pw.println("Related cause:");
181 relatedCause.printStackTrace(pw);
182 }
183 }
184 }
185 }
186
187 @Override
188 public boolean contains(Class<?> exClass) {
189 if (super.contains(exClass)) {
190 return true;
191 }
192 if (this.relatedCauses != null) {
193 for (Throwable relatedCause : this.relatedCauses) {
194 if (relatedCause instanceof NestedRuntimeException &&
195 ((NestedRuntimeException) relatedCause).contains(exClass)) {
196 return true;
197 }
198 }
199 }
200 return false;
201 }
202
203 }