1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.api.dom.java;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.ListIterator;
22
23 import org.mybatis.generator.api.dom.OutputUtilities;
24
25
26
27
28
29
30 public class Method extends JavaElement {
31
32
33 private List<String> bodyLines;
34
35
36 private boolean constructor;
37
38
39 private FullyQualifiedJavaType returnType;
40
41
42 private String name;
43
44
45 private List<Parameter> parameters;
46
47
48 private List<FullyQualifiedJavaType> exceptions;
49
50
51 private boolean isSynchronized;
52
53
54 private boolean isNative;
55
56
57
58
59 public Method() {
60
61 this("bar");
62 }
63
64
65
66
67
68
69
70 public Method(String name) {
71 super();
72 bodyLines = new ArrayList<String>();
73 parameters = new ArrayList<Parameter>();
74 exceptions = new ArrayList<FullyQualifiedJavaType>();
75 this.name = name;
76 }
77
78
79
80
81
82
83
84 public Method(Method original) {
85 super(original);
86 bodyLines = new ArrayList<String>();
87 parameters = new ArrayList<Parameter>();
88 exceptions = new ArrayList<FullyQualifiedJavaType>();
89 this.bodyLines.addAll(original.bodyLines);
90 this.constructor = original.constructor;
91 this.exceptions.addAll(original.exceptions);
92 this.name = original.name;
93 this.parameters.addAll(original.parameters);
94 this.returnType = original.returnType;
95 this.isNative = original.isNative;
96 this.isSynchronized = original.isSynchronized;
97 }
98
99
100
101
102
103
104 public List<String> getBodyLines() {
105 return bodyLines;
106 }
107
108
109
110
111
112
113
114 public void addBodyLine(String line) {
115 bodyLines.add(line);
116 }
117
118
119
120
121
122
123
124
125
126 public void addBodyLine(int index, String line) {
127 bodyLines.add(index, line);
128 }
129
130
131
132
133
134
135
136 public void addBodyLines(Collection<String> lines) {
137 bodyLines.addAll(lines);
138 }
139
140
141
142
143
144
145
146
147
148 public void addBodyLines(int index, Collection<String> lines) {
149 bodyLines.addAll(index, lines);
150 }
151
152
153
154
155
156
157
158
159
160
161
162 public String getFormattedContent(int indentLevel, boolean interfaceMethod, CompilationUnit compilationUnit) {
163 StringBuilder sb = new StringBuilder();
164
165 addFormattedJavadoc(sb, indentLevel);
166 addFormattedAnnotations(sb, indentLevel);
167
168 OutputUtilities.javaIndent(sb, indentLevel);
169
170 if (!interfaceMethod) {
171 sb.append(getVisibility().getValue());
172
173 if (isStatic()) {
174 sb.append("static ");
175 }
176
177 if (isFinal()) {
178 sb.append("final ");
179 }
180
181 if (isSynchronized()) {
182 sb.append("synchronized ");
183 }
184
185 if (isNative()) {
186 sb.append("native ");
187 } else if (bodyLines.size() == 0) {
188 sb.append("abstract ");
189 }
190 }
191
192 if (!constructor) {
193 if (getReturnType() == null) {
194 sb.append("void");
195 } else {
196 sb.append(JavaDomUtils.calculateTypeName(compilationUnit, getReturnType()));
197 }
198 sb.append(' ');
199 }
200
201 sb.append(getName());
202 sb.append('(');
203
204 boolean comma = false;
205 for (Parameter parameter : getParameters()) {
206 if (comma) {
207 sb.append(", ");
208 } else {
209 comma = true;
210 }
211
212 sb.append(parameter.getFormattedContent(compilationUnit));
213 }
214
215 sb.append(')');
216
217 if (getExceptions().size() > 0) {
218 sb.append(" throws ");
219 comma = false;
220 for (FullyQualifiedJavaType fqjt : getExceptions()) {
221 if (comma) {
222 sb.append(", ");
223 } else {
224 comma = true;
225 }
226
227 sb.append(JavaDomUtils.calculateTypeName(compilationUnit, fqjt));
228 }
229 }
230
231
232 if (bodyLines.size() == 0 || isNative()) {
233 sb.append(';');
234 } else {
235 sb.append(" {");
236 indentLevel++;
237
238 ListIterator<String> listIter = bodyLines.listIterator();
239 while (listIter.hasNext()) {
240 String line = listIter.next();
241 if (line.startsWith("}")) {
242 indentLevel--;
243 }
244
245 OutputUtilities.newLine(sb);
246 OutputUtilities.javaIndent(sb, indentLevel);
247 sb.append(line);
248
249 if ((line.endsWith("{") && !line.startsWith("switch"))
250 || line.endsWith(":")) {
251 indentLevel++;
252 }
253
254 if (line.startsWith("break")) {
255
256 if (listIter.hasNext()) {
257 String nextLine = listIter.next();
258 if (nextLine.startsWith("}")) {
259 indentLevel++;
260 }
261
262
263 listIter.previous();
264 }
265 indentLevel--;
266 }
267 }
268
269 indentLevel--;
270 OutputUtilities.newLine(sb);
271 OutputUtilities.javaIndent(sb, indentLevel);
272 sb.append('}');
273 }
274
275 return sb.toString();
276 }
277
278
279
280
281
282
283 public boolean isConstructor() {
284 return constructor;
285 }
286
287
288
289
290
291
292
293 public void setConstructor(boolean constructor) {
294 this.constructor = constructor;
295 }
296
297
298
299
300
301
302 public String getName() {
303 return name;
304 }
305
306
307
308
309
310
311
312 public void setName(String name) {
313 this.name = name;
314 }
315
316
317
318
319
320
321 public List<Parameter> getParameters() {
322 return parameters;
323 }
324
325
326
327
328
329
330
331 public void addParameter(Parameter parameter) {
332 parameters.add(parameter);
333 }
334
335
336
337
338
339
340
341
342
343 public void addParameter(int index, Parameter parameter) {
344 parameters.add(index, parameter);
345 }
346
347
348
349
350
351
352 public FullyQualifiedJavaType getReturnType() {
353 return returnType;
354 }
355
356
357
358
359
360
361
362 public void setReturnType(FullyQualifiedJavaType returnType) {
363 this.returnType = returnType;
364 }
365
366
367
368
369
370
371 public List<FullyQualifiedJavaType> getExceptions() {
372 return exceptions;
373 }
374
375
376
377
378
379
380
381 public void addException(FullyQualifiedJavaType exception) {
382 exceptions.add(exception);
383 }
384
385
386
387
388
389
390 public boolean isSynchronized() {
391 return isSynchronized;
392 }
393
394
395
396
397
398
399
400 public void setSynchronized(boolean isSynchronized) {
401 this.isSynchronized = isSynchronized;
402 }
403
404
405
406
407
408
409 public boolean isNative() {
410 return isNative;
411 }
412
413
414
415
416
417
418
419 public void setNative(boolean isNative) {
420 this.isNative = isNative;
421 }
422 }