1 package org.catacomb.act;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6
7 import org.catacomb.report.E;
8
9 public class Classdef {
10
11 String info;
12 String packageName;
13 String className;
14 String subclassName;
15 String extendsName;
16
17 String qualifier;
18
19 SuperCallConstructor constructor;
20 SuperCallConstructor subConstructor;
21
22 DefaultConstructor econstructor;
23 DefaultConstructor esubConstructor;
24
25
26 ArrayList<FieldSignature> fields;
27 HashMap<String, ArrayDeclaration> arrayDecs;
28
29 ArrayList<MethodSignature> methods;
30
31 public ArrayList<String> imports;
32 ArrayList<String> implementsNames;
33
34 String subclassImports;
35
36 public Classdef() {
37 qualifier = "";
38 imports = new ArrayList<String>();
39 extendsName = null;
40 implementsNames = new ArrayList<String>();
41 fields = new ArrayList<FieldSignature>();
42 arrayDecs = new HashMap<String, ArrayDeclaration>();
43 methods = new ArrayList<MethodSignature>();
44 }
45
46 public void setPackage(String s) {
47 packageName = s;
48 }
49
50 public void setQualifier(String s) {
51 qualifier = s;
52 }
53
54 public void addImplements(String s) {
55 addImplementsName(s);
56 }
57
58 public void setClassName(String s) {
59 className = s;
60 }
61
62 public String getClassName() {
63 return className;
64 }
65
66 public void setSubclassName(String s) {
67 subclassName = s;
68 }
69
70 public void setSuperclassName(String s) {
71 extendsName = s;
72 }
73
74 public void addImplementsName(String s) {
75 implementsNames.add(s);
76 }
77
78 public void addField(FieldSignature ms) {
79 fields.add(ms);
80 }
81
82 public void addMethod(MethodSignature ms) {
83 methods.add(ms);
84 }
85
86 public MethodSignature getMethod(String mnm) {
87 MethodSignature ret = null;
88 for (MethodSignature ms : methods) {
89 if (ms.getName().equals(mnm)) {
90 ret = ms;
91 break;
92 }
93 }
94 return ret;
95 }
96
97 @SuppressWarnings("unchecked")
98 public void addImport(Object obj) {
99 if (obj instanceof Class) {
100 addStringImport(((Class)obj).getName());
101 } else {
102 addStringImport(obj.getClass().getName());
103 }
104 }
105
106
107 public void addStringImport(String s) {
108 if (imports.contains(s)) {
109
110 } else {
111 imports.add(s);
112 }
113 }
114
115
116 private String makePackageStatement() {
117 return "package " + packageName + ";\n";
118 }
119
120
121 private String makeJavaImports() {
122 StringBuffer sb = new StringBuffer();
123 for (String s : imports) {
124 if (s.equals(packageName + "." + className) ||
125 s.equals(packageName + ".*")) {
126
127
128 } else {
129 sb.append("import ");
130 sb.append(s);
131 sb.append(";\n");
132 }
133 }
134 return sb.toString();
135 }
136
137 public void setInfo(String msg) {
138 info = msg;
139 }
140
141
142 public MethodSignature newMethod(String mnm) {
143 MethodSignature ms = new MethodSignature(mnm);
144 addMethod(ms);
145 return ms;
146 }
147
148 public MethodSignature newShowMethod(String rtype, String mnm, String atype) {
149 MethodSignature ms = new MethodSignature(mnm, MethodSignature.SHOW);
150 ms.setReturnType(rtype);
151 if (atype != null) {
152 ms.addArgument(atype, "v");
153 }
154 addMethod(ms);
155 return ms;
156 }
157
158 public MethodSignature newReadMethod(String rtype, String mnm, String atype) {
159 MethodSignature ms = new MethodSignature(mnm, MethodSignature.READ);
160 ms.setReturnType(rtype);
161 addMethod(ms);
162 return ms;
163 }
164
165
166
167 public MethodSignature newSendMethod(String mnm, String atype) {
168 MethodSignature ms = new MethodSignature(mnm, MethodSignature.SEND);
169 ms.setReturnType("void");
170 if (atype != null) {
171 ms.addArgument(atype, "v");
172 }
173 addMethod(ms);
174 return ms;
175 }
176
177
178 public MethodSignature newReceiveMethod(String mnm, String atype) {
179 MethodSignature ms = new MethodSignature(mnm, MethodSignature.RECEIVE);
180 ms.setReturnType("void");
181 if (atype != null) {
182 ms.addArgument(atype, "v");
183 }
184 addMethod(ms);
185 return ms;
186 }
187
188
189 public FieldSignature newPrivateField(String typ, String nm) {
190 FieldSignature fsig = new FieldSignature(typ, nm);
191 fsig.setPrivate();
192 addField(fsig);
193 return fsig;
194 }
195
196
197 public FieldSignature newPublicField(String typ, String nm) {
198 FieldSignature fsig = new FieldSignature(typ, nm);
199 fsig.setPublic();
200 addField(fsig);
201 return fsig;
202 }
203
204 public FieldSignature newReflectableField(String typ, String nm) {
205 FieldSignature fsig = new FieldSignature(typ, nm);
206 fsig.setReflectable();
207 addField(fsig);
208 return fsig;
209 }
210
211 public FieldSignature newGetSetField(String typ, String nm) {
212 FieldSignature fsig = new FieldSignature(typ, nm);
213 fsig.addGetter();
214 fsig.addSetter();
215 addField(fsig);
216 return fsig;
217 }
218
219
220 public String writeSystemSource() {
221 StringBuffer sb = new StringBuffer();
222
223 sb.append(makePackageStatement());
224 sb.append("\n");
225
226 sb.append(makeJavaImports());
227 sb.append("\n");
228 sb.append("//IMPORTS\n");
229
230
231 if (info != null) {
232 sb.append("/*\n" + info + "\n*/\n");
233 }
234 sb.append("public " + qualifier + " class " + className + " ");
235 if (extendsName != null) {
236 sb.append("extends " + extendsName + " ");
237 }
238 if (implementsNames != null && implementsNames.size() > 0) {
239 sb.append("implements ");
240 boolean follower = false;
241 for (String s : implementsNames) {
242 if (follower) {
243 sb.append(",");
244 }
245 follower = true;
246 sb.append(" " + s);
247 }
248
249 }
250 sb.append(" {\n\n");
251
252 for (FieldSignature fsig : fields) {
253 sb.append(fsig.getJavaSource());
254 }
255
256 for (ArrayDeclaration ad : arrayDecs.values()) {
257 sb.append(ad.toJava());
258 }
259
260
261 sb.append("\n\n");
262
263
264 if (constructor != null) {
265 sb.append(constructor.toJavaSource(className));
266 sb.append("\n\n");
267 }
268 if (econstructor != null) {
269 sb.append(econstructor.toJavaSource(className));
270 sb.append("\n\n");
271 }
272
273
274 Collections.sort(methods, new SignatureComparator());
275
276 for (FieldSignature fsig : fields) {
277 sb.append(fsig.getJavaAccessors());
278 }
279
280 for (MethodSignature ms : methods) {
281 String qf = "";
282 if (ms.isRequiredUser()) {
283 qf = " abstract ";
284 }
285 if (ms.isSuper()) {
286
287
288
289 } else {
290 if (ms.superDefined() && ms.emptyBody()) {
291
292
293 } else {
294 sb.append(ms.toJavaSource(qf));
295 sb.append("\n\n");
296 }
297 }
298 }
299
300 sb.append("//LINE " + nlines(sb) + " start of user content\n");
301 sb.append("//CLASSBODY\n");
302 sb.append("}\n");
303 return sb.toString();
304 }
305
306
307
308
309
310 public String writeSubSource() {
311 StringBuffer sb = new StringBuffer();
312 sb.append(makePackageStatement());
313 sb.append("\n");
314 sb.append(makeJavaImports());
315 sb.append("\n");
316 if (subclassImports != null) {
317 sb.append(subclassImports);
318 sb.append("\n");
319 }
320
321 sb.append("//IMPORTS\n");
322
323 if (info != null) {
324 sb.append("/*\n" + info + "\n*/\n");
325 }
326 sb.append("public class " + subclassName + " ");
327 sb.append("extends " + className + " ");
328 sb.append(" {\n\n");
329
330
331 if (subConstructor != null) {
332 sb.append(subConstructor.toJavaSource(subclassName));
333 sb.append("\n\n");
334 }
335
336 if (esubConstructor != null) {
337 sb.append(esubConstructor.toJavaSource(subclassName));
338 sb.append("\n\n");
339 }
340 sb.append("//CLASSBODY\n");
341
342 sb.append("\n\n");
343
344 sb.append("}\n");
345 return sb.toString();
346 }
347
348 public String writeSystemFieldComments() {
349 return writeSystemFieldComments("p");
350 }
351
352 public String writeSystemFieldComments(String pname) {
353 StringBuffer sb = new StringBuffer();
354 int npf = 0;
355
356 if (fields != null) {
357 for (FieldSignature fsig : fields) {
358 if (fsig.isPublic() && fsig.isVisible()) {
359 sb.append("");
360 sb.append(fsig.getPrefixedInfo(pname));
361 sb.append("<br>\n");
362 npf += 1;
363 }
364 }
365 }
366 return sb.toString();
367 }
368
369
370
371 public int countAccessibleFields() {
372 int nf = 0;
373 for (FieldSignature fsig : fields) {
374 if (fsig.isPublic() && fsig.isVisible()) {
375 nf += 1;
376 }
377 }
378 return nf;
379 }
380
381
382
383 public String writeLocalFieldComments() {
384 StringBuffer sb = new StringBuffer();
385 for (FieldSignature fsig : fields) {
386 if (fsig.isPublic() && fsig.isVisible()) {
387 sb.append("");
388 sb.append(fsig.getLocalInfo());
389 sb.append("<br>\n");
390 }
391 }
392 if (sb.length() == 0) {
393 sb.append(" - no local fields - ");
394 }
395 return sb.toString();
396 }
397
398
399
400
401 public String writeSystemSignatures() {
402 StringBuffer sb = new StringBuffer();
403
404 for (MethodSignature msig : methods) {
405 if (msig.isSuper() || (msig.isSystem() && msig.isUserAccessible())) {
406 sb.append("");
407 sb.append(msig.writeCommentSignature());
408 sb.append("<br>\n");
409 }
410 }
411 if (sb.length() == 0) {
412 sb.append(" - no access methods - <br>");
413 }
414 sb.append("\n");
415
416 return sb.toString();
417 }
418
419
420
421 public String writeSubSourceMethods() {
422 StringBuffer sb = new StringBuffer();
423
424 for (MethodSignature msig : methods) {
425 if (msig.isUser() || msig.isRequiredUser()) {
426 sb.append(msig.toJavaSource());
427 sb.append("\n\n");
428 }
429 }
430 return sb.toString();
431 }
432
433
434 public ScriptStubs getSubSourceMethodStubs() {
435 ScriptStubs ret = new ScriptStubs();
436 for (MethodSignature msig : methods) {
437 if (msig.isUser() || msig.isRequiredUser()) {
438 ret.add(msig.getStub());
439 }
440 }
441 return ret;
442 }
443
444
445 public void addSuperCallConstructor() {
446 addSuperCallConstructor(null);
447 }
448
449 public void addSuperCallConstructor(String argtype) {
450 constructor = new SuperCallConstructor(argtype);
451 subConstructor = new SuperCallConstructor(argtype);
452 }
453
454 public void addSuperCallConstructor(String argtype, String body) {
455 constructor = new SuperCallConstructor(argtype, body);
456 subConstructor = new SuperCallConstructor(argtype);
457 }
458
459 public void addDefaultConstructor(String sbody) {
460 econstructor = new DefaultConstructor(sbody);
461 }
462
463 public void addDefaultConstructor() {
464 econstructor = new DefaultConstructor();
465 esubConstructor = new DefaultConstructor();
466 }
467
468
469
470 public void startArrayDeclaration(String sType, String sName) {
471 ArrayDeclaration arrDec = new ArrayDeclaration(sName);
472 arrDec.setType(sType);
473 arrayDecs.put(sName, arrDec);
474
475 }
476
477 public void addToArray(String anm, String aval) {
478 if (arrayDecs.containsKey(anm)) {
479 arrayDecs.get(anm).addValue(aval);
480 } else {
481 E.warning("no sucn array declaration : " + anm);
482 }
483 }
484
485
486 public FieldSignature addGetSetField(String st, String sn) {
487 FieldSignature fsig = new FieldSignature(st, sn);
488 fsig.addGetter();
489 fsig.addSetter();
490 fsig.setPublic();
491 addField(fsig);
492 return fsig;
493 }
494
495 public void setUserHidden(String methname) {
496 getMethod(methname).setUserHidden();
497 }
498
499 public void appendToBody(String methname, String line) {
500 MethodSignature msig = getMethod(methname);
501 if (msig == null) {
502 msig = new MethodSignature(methname);
503 addMethod(msig);
504 }
505
506 msig.appendToBody(line);
507 }
508
509 public void addSubclassImports(String s) {
510 if (subclassImports == null) {
511 subclassImports = s;
512 } else {
513 subclassImports += "\n" + s;
514 }
515
516 }
517
518
519 private int nlines(StringBuffer sb) {
520 String s = sb.toString();
521 int nocc = 0;
522 int ioff = s.indexOf("\n");
523 while (ioff >= 0) {
524 nocc++;
525 ioff = s.indexOf("\n", ioff+1);
526 }
527 return nocc;
528 }
529
530 }