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
34
35
36
37
38
39
40
41
42
43
44
45 package org.codehaus.modello.plugin.java.javasource;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 import java.util.ArrayList;
70 import java.util.Enumeration;
71 import java.util.LinkedHashMap;
72 import java.util.List;
73 import java.util.Map;
74
75
76
77
78
79
80
81
82
83
84
85 public final class JInterface extends JStructure {
86
87
88
89
90 private Map<String, JField> fields = null;
91
92
93
94
95 private List<JMethodSignature> methods = null;
96
97
98
99
100
101
102
103
104 public JInterface(String name) throws IllegalArgumentException {
105 super(name);
106 methods = new ArrayList<JMethodSignature>();
107
108
109 getJDocComment().appendComment("Interface " + getLocalName() + ".");
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public void addField(JField jField) throws IllegalArgumentException {
126 if (jField == null) {
127 throw new IllegalArgumentException("argument 'jField' cannot be null");
128 }
129
130 String name = jField.getName();
131
132
133 if ((fields != null) && (fields.get(name) != null)) {
134 String err = "duplicate name found: " + name;
135 throw new IllegalArgumentException(err);
136 }
137
138
139 JModifiers modifiers = jField.getModifiers();
140 if (!modifiers.isStatic()) {
141 throw new IllegalArgumentException("Fields added to a JInterface must be static.");
142 }
143 if (modifiers.isPrivate()) {
144 throw new IllegalArgumentException("Fields added to a JInterface must not be private.");
145 }
146
147
148
149 if (fields == null) {
150 fields = new LinkedHashMap<>(3);
151 }
152
153 fields.put(name, jField);
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public void addMember(JMember jMember) throws IllegalArgumentException {
170 if (jMember == null) {
171 throw new IllegalArgumentException("argument 'jMember' may not be null.");
172 }
173 if (jMember instanceof JField) {
174 addField((JField) jMember);
175 } else {
176 throw new IllegalArgumentException("invalid member for JInterface: " + jMember.toString());
177 }
178 }
179
180
181
182
183
184
185
186
187
188 public void addMethod(JMethodSignature jMethodSig) throws IllegalArgumentException {
189 if (jMethodSig == null) {
190 String err = "The JMethodSignature cannot be null.";
191 throw new IllegalArgumentException(err);
192 }
193
194
195
196
197
198 boolean added = false;
199
200 JModifiers modifiers = jMethodSig.getModifiers();
201 for (int i = 0; i < methods.size(); i++) {
202 JMethodSignature tmp = methods.get(i);
203
204 if (tmp.getModifiers().isProtected()) {
205 if (!modifiers.isProtected()) {
206 methods.add(i, jMethodSig);
207 added = true;
208 break;
209 }
210 }
211
212 if (jMethodSig.getName().compareTo(tmp.getName()) < 0) {
213 methods.add(i, jMethodSig);
214 added = true;
215 break;
216 }
217 }
218
219 if (!added) methods.add(jMethodSig);
220
221
222
223 JType jType = jMethodSig.getReturnType();
224 if (jType != null) {
225 while (jType.isArray()) jType = jType.getComponentType();
226
227 if (!jType.isPrimitive()) addImport(jType.getName());
228 }
229
230 JClass[] exceptions = jMethodSig.getExceptions();
231 for (JClass exception : exceptions) {
232 addImport(exception.getName());
233 }
234 }
235
236
237
238
239
240
241
242
243
244 public JField getField(String name) {
245 if (fields == null) return null;
246 return (JField) fields.get(name);
247 }
248
249
250
251
252
253
254 public JField[] getFields() {
255 if (fields == null) {
256 return new JField[0];
257 }
258 return fields.values().toArray(new JField[0]);
259 }
260
261
262
263
264
265
266 public JMethodSignature[] getMethods() {
267 return methods.toArray(new JMethodSignature[0]);
268 }
269
270
271
272
273
274
275
276
277
278
279 public JMethodSignature getMethod(String name, int startIndex) {
280 for (JMethodSignature jMethod : methods) {
281 if (jMethod.getName().equals(name)) return jMethod;
282 }
283 return null;
284 }
285
286
287
288
289
290
291
292 public JMethodSignature getMethod(int index) {
293 return methods.get(index);
294 }
295
296
297
298
299
300
301 public void print(JSourceWriter jsw) {
302 print(jsw, false);
303 }
304
305
306
307
308
309
310
311 public void print(JSourceWriter jsw, boolean classOnly) {
312
313 if (jsw == null) {
314 throw new IllegalArgumentException("argument 'jsw' should not be null.");
315 }
316
317 StringBuilder buffer = new StringBuilder();
318
319 if (!classOnly) {
320 printHeader(jsw);
321 printPackageDeclaration(jsw);
322 printImportDeclarations(jsw);
323 }
324
325
326
327
328
329 getJDocComment().print(jsw);
330
331 JAnnotations annotations = getAnnotations();
332 if (annotations != null) annotations.print(jsw);
333
334
335
336
337 buffer.setLength(0);
338
339 JModifiers modifiers = getModifiers();
340 if (modifiers.isPrivate()) {
341 buffer.append("private ");
342 } else if (modifiers.isPublic()) {
343 buffer.append("public ");
344 }
345
346 if (modifiers.isAbstract()) {
347 buffer.append("abstract ");
348 }
349
350 buffer.append("interface ");
351 buffer.append(getLocalName());
352 jsw.writeln(buffer.toString());
353 buffer.setLength(0);
354 jsw.indent();
355
356 if (getInterfaceCount() > 0) {
357 Enumeration<String> e = getInterfaces();
358 buffer.append("extends ");
359 while (e.hasMoreElements()) {
360 buffer.append(e.nextElement());
361 if (e.hasMoreElements()) buffer.append(", ");
362 }
363
364 jsw.writeln(buffer.toString());
365 buffer.setLength(0);
366 }
367
368 jsw.unindent();
369
370 jsw.writeln('{');
371
372 jsw.indent();
373
374
375
376 if (fields != null) {
377 if (fields.size() > 0) {
378 jsw.writeln();
379 jsw.writeln(" //--------------------------/");
380 jsw.writeln(" //- Class/Member Variables -/");
381 jsw.writeln("//--------------------------/");
382 jsw.writeln();
383 }
384
385 for (JField jField : fields.values()) {
386
387 JDocComment comment = jField.getComment();
388 if (comment != null) comment.print(jsw);
389
390
391 jsw.write(jField.getModifiers().toString());
392 jsw.write(' ');
393
394 JType type = jField.getType();
395 String typeName = type.toString();
396
397 if (typeName.equals(toString())) {
398 typeName = type.getLocalName();
399 }
400 jsw.write(typeName);
401 jsw.write(' ');
402 jsw.write(jField.getName());
403
404 String init = jField.getInitString();
405 if (init != null) {
406 jsw.write(" = ");
407 jsw.write(init);
408 }
409
410 jsw.writeln(';');
411 jsw.writeln();
412 }
413 }
414
415
416
417 if (methods.size() > 0) {
418 jsw.writeln();
419 jsw.writeln(" //-----------/");
420 jsw.writeln(" //- Methods -/");
421 jsw.writeln("//-----------/");
422 jsw.writeln();
423 }
424
425 for (JMethodSignature signature : methods) {
426 signature.print(jsw);
427 jsw.writeln(';');
428 }
429
430 for (String sourceCodeEntry : sourceCodeEntries) {
431 jsw.writeln(sourceCodeEntry);
432 }
433
434 jsw.unindent();
435 jsw.writeln('}');
436 jsw.flush();
437 jsw.close();
438 }
439
440 private List<String> sourceCodeEntries = new ArrayList<String>();
441
442 public void addSourceCode(String sourceCode) {
443 sourceCodeEntries.add(sourceCode);
444 }
445 }