1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.exolab.javasource;
34
35 import java.util.LinkedHashMap;
36 import java.util.Map;
37 import java.util.Vector;
38
39
40
41
42
43
44
45 public final class JConstructor extends JAnnotatedElementHelper {
46
47
48 private JModifiers _modifiers = new JModifiers();
49
50
51 private final Map<String, JParameter> _params = new LinkedHashMap<String, JParameter>();
52
53
54 private final AbstractJClass _declaringClass;
55
56
57 private JSourceCode _sourceCode = new JSourceCode();
58
59
60 private final Vector<JClass> _exceptions = new Vector<>(1);
61
62
63
64
65
66
67 protected JConstructor(final AbstractJClass declaringClass) {
68 _declaringClass = declaringClass;
69 }
70
71
72
73
74
75
76 public JClass[] getExceptions() {
77 return _exceptions.toArray(new JClass[_exceptions.size()]);
78 }
79
80
81
82
83
84
85 public void addException(final JClass exp) {
86 if (exp == null) {
87 return;
88 }
89
90
91 String expClassName = exp.getName();
92 for (JClass jClass : _exceptions) {
93 if (expClassName.equals(jClass.getName())) {
94 return;
95 }
96 }
97
98 _exceptions.add(exp);
99 }
100
101
102
103
104
105
106
107 public JParameter[] getParameters() {
108 return _params.values().toArray(new JParameter[_params.size()]);
109 }
110
111
112
113
114
115
116 public int getParameterCount() {
117 return _params.size();
118 }
119
120
121
122
123
124
125 public void addParameter(final JParameter parameter) {
126 if (parameter == null) {
127 return;
128 }
129
130
131 if (_params.get(parameter.getName()) != null) {
132 String err = new StringBuilder(64).append("A parameter already exists for the constructor, ")
133 .append(this._declaringClass.getName()).append(", with the name: ")
134 .append(parameter.getName()).toString();
135 throw new IllegalArgumentException(err);
136 }
137
138 _params.put(parameter.getName(), parameter);
139
140
141
142 if (_declaringClass != null) {
143 JType jType = parameter.getType();
144 if (!jType.isPrimitive()) {
145 _declaringClass.addImport(jType.getName());
146 }
147 }
148 }
149
150
151
152
153
154
155 public AbstractJClass getDeclaringClass() {
156 return _declaringClass;
157 }
158
159
160
161
162
163
164 public JModifiers getModifiers() {
165 return _modifiers;
166 }
167
168
169
170
171
172
173 public void setModifiers(final JModifiers modifiers) {
174 _modifiers = modifiers.copy();
175 _modifiers.setFinal(false);
176 }
177
178
179
180
181
182
183 public JSourceCode getSourceCode() {
184 return _sourceCode;
185 }
186
187
188
189
190
191
192 public void setSourceCode(final String sourceCode) {
193 setSourceCode(new JSourceCode(sourceCode));
194 }
195
196
197
198
199
200
201 public void setSourceCode(final JSourceCode sourceCode) {
202 _sourceCode = sourceCode;
203 }
204
205
206
207
208
209
210 public void print(final JSourceWriter jsw) {
211
212 printAnnotations(jsw);
213
214 if (_modifiers.isPrivate()) {
215 jsw.write("private");
216 } else if (_modifiers.isProtected()) {
217 jsw.write("protected");
218 } else {
219 jsw.write("public");
220 }
221 jsw.write(' ');
222 jsw.write(_declaringClass.getLocalName());
223 jsw.write('(');
224
225
226 boolean parameterAnnotations = false;
227 for (JParameter jParameter : _params.values()) {
228 if (jParameter.hasAnnotations()) {
229 parameterAnnotations = true;
230 break;
231 }
232 }
233
234
235 if (parameterAnnotations) {
236 jsw.indent();
237 }
238 int parameterCount = 0;
239 for (JParameter jParameter : _params.values()) {
240 if (parameterCount > 0) {
241 jsw.write(", ");
242 }
243 if (parameterAnnotations) {
244 jsw.writeln();
245 }
246 jParameter.printAnnotations(jsw);
247 String typeAndName = jParameter.toString();
248 jsw.write(typeAndName);
249 parameterCount++;
250 }
251 if (parameterAnnotations) {
252 jsw.unindent();
253 }
254
255 jsw.write(")");
256 if (!_exceptions.isEmpty()) {
257 jsw.writeln();
258 jsw.write("throws ");
259 for (int i = 0; i < _exceptions.size(); i++) {
260 if (i > 0) {
261 jsw.write(", ");
262 }
263 JClass jClass = _exceptions.elementAt(i);
264 jsw.write(jClass.getName());
265 }
266 }
267 jsw.writeln(" {");
268
269 _sourceCode.print(jsw);
270
271 if (!jsw.isNewline()) {
272 jsw.writeln();
273 }
274 jsw.writeln("}");
275 }
276
277
278
279
280 public String toString() {
281 StringBuilder sb = new StringBuilder(32).append(_declaringClass.getName()).append('(');
282
283
284 for (int i = 0; i < _params.size(); i++) {
285 JParameter jp = _params.get(i);
286 if (i > 0) {
287 sb.append(", ");
288 }
289 sb.append(jp.getType().getName());
290 }
291 sb.append(')');
292 return sb.toString();
293 }
294
295 }