Skip to content

Method: addOperation(GenJavaOperation)

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