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