Skip to content

Method: getInnerClasses()

1: package de.fhdw.wtf.generator.java.generatorModel;
2:
3: import java.util.ArrayList;
4: import java.util.Collection;
5: import java.util.Iterator;
6: import java.util.Vector;
7:
8: import de.fhdw.wtf.generator.java.visitor.GenClassVisitorException;
9: import de.fhdw.wtf.generator.java.visitor.GenClassVisitorReturn;
10: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitorReturn;
11: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitor;
12: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitorException;
13: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitorReturn;
14:
15: public abstract class GenClass extends GenType {
16:         
17:         @Override
18:         public String toString() {
19:                 return this.getName();
20:         }
21:         
22:         /**
23:          * The comment that describes this class.
24:          */
25:         private GenComment comment;
26:         
27:         /**
28:          * The operations the class has.
29:          */
30:         private final Collection<GenJavaOperation> operations;
31:         
32:         /**
33:          * The interfaces the class implements.
34:          */
35:         private final Collection<GenInterfaceClass> implement;
36:         
37:         /**
38:          * The package the class will be generated in.
39:          */
40:         private GenPackage packag;
41:         
42:         /**
43:          * Additional lines of code.
44:          */
45:         private String nonGeneratedPart;
46:         
47:         /**
48:          * The generics of the class.
49:          */
50:         private final ArrayList<Generic> generics;
51:         
52:         /**
53:          * Field for import block.
54:          */
55:         private String imports = "";
56:         
57:         /**
58:          * The inner-classes of the class.
59:          */
60:         private final Collection<GenClassClass> innerClasses;
61:         
62:         /**
63:          * Calls the handle method in the given visitor.
64:          *
65:          * @param visitor
66:          * The visitor to handle this type.
67:          */
68:         public abstract void accept(de.fhdw.wtf.generator.java.visitor.GenClassVisitor visitor);
69:         
70:         /**
71:          * Calls the handle method in the given visitor.
72:          *
73:          * @param visitor
74:          * The visitor to handle this type.
75:          * @param <X>
76:          * The returntype of the handle and the accept method.
77:          * @return Returns the result of type <X> that is returned by the handle method.
78:          */
79:         public abstract <X> X accept(GenClassVisitorReturn<X> visitor);
80:         
81:         /**
82:          * Calls the handle method in the given visitor.
83:          *
84:          * @param visitor
85:          * The visitor to handle this type.
86:          * @param <Y>
87:          * The exception that can be thrown by the handle method.
88:          * @throws Y
89:          * Throws an exception of type <X> when the handle method throws it.
90:          */
91:         public abstract <Y extends java.lang.Exception> void accept(GenClassVisitorException<Y> visitor) throws Y;
92:         
93:         /**
94:          * Constructor for GenClass.
95:          *
96:          * @param name
97:          * The name of the GenClass and the java-file to generate.
98:          * @param operations
99:          * The operations the GenClass has.
100:          * @param implement
101:          * The interfaces the GenClass implements.
102:          * @param comment
103:          * The comment that describes this GenClass.
104:          * @param packag
105:          * The package the GenClass will be generated in.
106:          * @param nonGeneratedPart
107:          * Additional lines of code.
108:          */
109:         protected GenClass(final String name,
110:                         final Collection<GenJavaOperation> operations,
111:                         final Collection<GenInterfaceClass> implement,
112:                         final GenPackage packag,
113:                         final GenComment comment,
114:                         final String nonGeneratedPart) {
115:                 super(name);
116:                 this.comment = comment;
117:                 this.operations = operations;
118:                 this.implement = implement;
119:                 this.packag = packag;
120:                 this.nonGeneratedPart = nonGeneratedPart;
121:                 this.generics = new ArrayList<>();
122:                 this.innerClasses = new Vector<>();
123:         }
124:         
125:         @Override
126:         public void accept(final GenTypeVisitor visitor) {
127:                 visitor.handle(this);
128:         }
129:         
130:         @Override
131:         public <X> X accept(final GenTypeVisitorReturn<X> visitor) {
132:                 return visitor.handle(this);
133:         }
134:         
135:         @Override
136:         public <X extends java.lang.Exception> void accept(final GenTypeVisitorException<X> visitor) throws X {
137:                 visitor.handle(this);
138:         }
139:         
140:         /**
141:          * Returns a collection of all operations this GenClass has.
142:          *
143:          * BE CAREFUL! To make sure operations are properly added using "addOperation" this method returns a COPY of the
144:          * collection of the operations the GenClass has. The contained operations are the SAME, but the collection is a new
145:          * reference.
146:          *
147:          * @return collection of operations
148:          */
149:         public Collection<GenJavaOperation> getOperations() {
150:                 final Collection<GenJavaOperation> copy = new Vector<>();
151:                 copy.addAll(this.operations);
152:                 return copy;
153:         }
154:         
155:         /**
156:          * Returns a collection of all operations this GenClass has.
157:          *
158:          * @return operations
159:          */
160:         protected Collection<GenJavaOperation> getOperationsInner() {
161:                 return this.operations;
162:         }
163:         
164:         /**
165:          * Adds a GenJavaOperation to this GenClass.
166:          *
167:          * @param newOperation
168:          * The operation that shall be generated in this class.
169:          */
170:         public void addOperation(final GenJavaOperation newOperation) {
171:                 this.getOperationsInner().add(newOperation);
172:         }
173:         
174:         /**
175:          * Searches the given operation in the list of operations compared by the operation signature (
176:          * {@link GenOperationSignature}). If the operation is found it will be overriden by the new operation "op" and true
177:          * will be returned. If no operation with the signature is found the operation "op" will not be added and false will
178:          * be returned.
179:          *
180:          * @param op
181:          * - operation for override.
182:          * @return if an existing operation was overridden.
183:          */
184:         public boolean overrideExistingOperation(final GenJavaOperation op) {
185:                 final Iterator<GenJavaOperation> it = this.getOperationsInner().iterator();
186:                 while (it.hasNext()) {
187:                         final GenJavaOperation next = it.next();
188:                         if (op.getSignature().equals(next.getSignature())) {
189:                                 this.getOperationsInner().remove(next);
190:                                 this.getOperationsInner().add(op);
191:                                 return true;
192:                         }
193:                 }
194:                 return false;
195:         }
196:         
197:         /**
198:          * Searches the given operation in the list of operations compared by the operation signature (
199:          * {@link GenOperationSignature}) in all super classes. If the operation is found it will be added to the operations
200:          * of this class. If no operation with the signature is found the operation "m" will not be added.
201:          *
202:          * @param m
203:          * - operation for override abstract operation.
204:          */
205:         public void tryToOverrideAbstractOperation(final GenJavaOperation m) {
206:                 if (this.checkForAbstractOperation(m)) {
207:                         this.addOperation(m);
208:                         return;
209:                 }
210:                 
211:         }
212:         
213:         /**
214:          * Searches the given operation in the list of operations compared by the operation signature (
215:          * {@link GenOperationSignature}) in all super classes. If the operation is found true will be returned. If no
216:          * operation with the signature is found false will be returned.
217:          *
218:          * @param m
219:          * - operation for override abstract operation.
220:          * @return true if a operation with the same signature as m is found in this class or super classes.
221:          */
222:         public boolean checkForAbstractOperation(final GenJavaOperation m) {
223:                 final Iterator<GenJavaOperation> i = this.getOperations().iterator();
224:                 while (i.hasNext()) {
225:                         final GenJavaOperation genJavaOperation = i.next();
226:                         if (genJavaOperation.getSignature().equals(m.getSignature())) {
227:                                 return true;
228:                         }
229:                 }
230:                 final Iterator<GenInterfaceClass> interfacesIterator = this.getImplement().iterator();
231:                 while (interfacesIterator.hasNext()) {
232:                         final GenInterfaceClass genInterfaceClass = interfacesIterator.next();
233:                         if (genInterfaceClass.checkForAbstractOperation(m)) {
234:                                 return true;
235:                         }
236:                         
237:                 }
238:                 return false;
239:         }
240:         
241:         /**
242:          * Returns the Collection of GenInterfaceClass with the interfaces the class implements.
243:          *
244:          * @return implement
245:          */
246:         public Collection<GenInterfaceClass> getImplement() {
247:                 return this.implement;
248:         }
249:         
250:         /**
251:          * Returns the comment of the class.
252:          *
253:          * @return comment.
254:          */
255:         public GenComment getComment() {
256:                 return this.comment;
257:         }
258:         
259:         /**
260:          * Returns the package of the class.
261:          *
262:          * @return packag
263:          */
264:         public GenPackage getPackag() {
265:                 return this.packag;
266:         }
267:         
268:         /**
269:          * Returns the additional lines of code.
270:          *
271:          * @return nonGeneratedPart
272:          */
273:         public String getNonGeneratedPart() {
274:                 return this.nonGeneratedPart;
275:         }
276:         
277:         /**
278:          * Returns all inner classes of this GenClass.
279:          *
280:          * @return Collection<GenClassClass>
281:          */
282:         public Collection<GenClassClass> getInnerClasses() {
283:                 return this.innerClasses;
284:         }
285:         
286:         /**
287:          * Returns the imports block.
288:          *
289:          * @return imports block
290:          */
291:         public String getImports() {
292:                 return this.imports;
293:         }
294:         
295:         /**
296:          * Returns the generics of the class.
297:          *
298:          * @return generics
299:          */
300:         public ArrayList<Generic> getGenerics() {
301:                 return this.generics;
302:         }
303:         
304:         /**
305:          * Set the package of the class.
306:          *
307:          * @param packag
308:          * The new package for the class.
309:          */
310:         public void setPackag(final GenPackage packag) {
311:                 this.packag = packag;
312:         }
313:         
314:         /**
315:          * Set the comment of the class.
316:          *
317:          * @param comment
318:          * The new comment for the class.
319:          */
320:         public void setComment(final GenComment comment) {
321:                 this.comment = comment;
322:         }
323:         
324:         /**
325:          * Adds the {@link GenClassClass} <code>c</code> to the innerClasses collection.
326:          *
327:          * @param c
328:          * The inner GenClassClass for the GenClass.
329:          */
330:         public void addInnerClass(final GenClassClass c) {
331:                 this.getInnerClasses().add(c);
332:         }
333:         
334:         /**
335:          * Set the additional lines of code of the class.
336:          *
337:          * @param nonGeneratedPart
338:          * The new additional lines of code for the class.
339:          */
340:         public void setNonGeneratedPart(final String nonGeneratedPart) {
341:                 this.nonGeneratedPart = nonGeneratedPart;
342:         }
343:         
344:         /**
345:          * Sets field imports to imports.
346:          *
347:          * @param imports
348:          * block of imports
349:          */
350:         public void setImports(final String imports) {
351:                 this.imports = imports;
352:         }
353:         
354:         @Override
355:         public String getFullyQualifiedTypeName() {
356:                 String result = "";
357:                 if (this.getPackag().toString().equals("")) {
358:                         result = this.getName();
359:                 } else {
360:                         result = this.getPackag() + "." + this.getName();
361:                 }
362:                 return result;
363:         }
364:         
365:         @Override
366:         public String getFullyQualifiedTypeNameWithGenericArguments() {
367:                 return this.getFullyQualifiedTypeName();
368:         }
369:         
370:         /**
371:          * Returns the file ending to use in order to write this class to a file.
372:          *
373:          * @return The file ending, including the leading dot.
374:          */
375:         public String getFileEnding() {
376:                 return ".java";
377:         }
378:         
379:         /**
380:          * Returns the implementing class. This is defined for GenClassClass classes (which are their own implementing
381:          * classes) and for GenInterfaceWithClassImplClass interfaces (where the implementing class is determined by calling
382:          * getClassRepresentation()). In all other cases, null is returned.
383:          *
384:          * @return The implementing class or null if none available.
385:          */
386:         public GenClassClass getImplementor() {
387:                 return this.accept(new GenClassVisitorReturn<GenClassClass>() {
388:                         
389:                         @Override
390:                         public GenClassClass handle(final GenClassClass classClass) {
391:                                 return classClass;
392:                         }
393:                         
394:                         @Override
395:                         public GenClassClass handle(final GenInterfaceClass interfaceClass) {
396:                                 return interfaceClass.accept(new GenInterfaceClassVisitorReturn<GenClassClass>() {
397:                                         
398:                                         @Override
399:                                         public GenClassClass handle(final GenSimpleInterfaceClass simpleInterface) {
400:                                                 return null;
401:                                         }
402:                                         
403:                                         @Override
404:                                         public GenClassClass handle(final GenInterfaceWithClassImplClass interfaceWithImplClass) {
405:                                                 return interfaceWithImplClass.getClassRepresentation();
406:                                         }
407:                                         
408:                                         @Override
409:                                         public GenClassClass handle(final GenExternalInterfaceClass iface) {
410:                                                 return null;
411:                                         }
412:                                 });
413:                         }
414:                         
415:                         @Override
416:                         public GenClassClass handle(final GenPrimitiveClass primitiveClass) {
417:                                 return null;
418:                         }
419:                         
420:                 });
421:         }
422:         
423: }