Skip to content

Package: InheritanceTransformer$2$1

InheritanceTransformer$2$1

nameinstructionbranchcomplexitylinemethod
handle(GenIntegerType)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handle(GenStringType)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handle(GenVoidType)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.wtf.generator.transformer.transformers.classTransformer;
2:
3: import java.util.Collection;
4:
5: import de.fhdw.wtf.common.ast.Model;
6: import de.fhdw.wtf.common.ast.type.AtomicType;
7: import de.fhdw.wtf.common.ast.type.CompositeType;
8: import de.fhdw.wtf.common.ast.type.ListType;
9: import de.fhdw.wtf.common.ast.type.MapType;
10: import de.fhdw.wtf.common.ast.type.ProductType;
11: import de.fhdw.wtf.common.ast.type.SumType;
12: import de.fhdw.wtf.common.ast.type.ThrownType;
13: import de.fhdw.wtf.common.ast.type.Type;
14: import de.fhdw.wtf.common.ast.type.TypeProxy;
15: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorReturn;
16: import de.fhdw.wtf.common.ast.visitor.TypeVisitorReturn;
17: import de.fhdw.wtf.common.exception.walker.CyclicDependencyException;
18: import de.fhdw.wtf.common.exception.walker.TaskException;
19: import de.fhdw.wtf.common.task.TaskExecutor;
20: import de.fhdw.wtf.generator.java.generatorModel.GenAspect;
21: import de.fhdw.wtf.generator.java.generatorModel.GenClass;
22: import de.fhdw.wtf.generator.java.generatorModel.GenClassClass;
23: import de.fhdw.wtf.generator.java.generatorModel.GenCollectionType;
24: import de.fhdw.wtf.generator.java.generatorModel.GenDeclareInheritance;
25: import de.fhdw.wtf.generator.java.generatorModel.GenDummyType;
26: import de.fhdw.wtf.generator.java.generatorModel.GenExternalInterfaceClass;
27: import de.fhdw.wtf.generator.java.generatorModel.GenImportType;
28: import de.fhdw.wtf.generator.java.generatorModel.GenIntegerType;
29: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceClass;
30: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceWithClassImplClass;
31: import de.fhdw.wtf.generator.java.generatorModel.GenJavaUtilCollection;
32: import de.fhdw.wtf.generator.java.generatorModel.GenMapType;
33: import de.fhdw.wtf.generator.java.generatorModel.GenPrimitiveClass;
34: import de.fhdw.wtf.generator.java.generatorModel.GenPrimitiveType;
35: import de.fhdw.wtf.generator.java.generatorModel.GenSimpleInterfaceClass;
36: import de.fhdw.wtf.generator.java.generatorModel.GenStringType;
37: import de.fhdw.wtf.generator.java.generatorModel.GenType;
38: import de.fhdw.wtf.generator.java.generatorModel.GenVoidType;
39: import de.fhdw.wtf.generator.java.generatorModel.GeneratorModel;
40: import de.fhdw.wtf.generator.java.generatorModel.Generic;
41: import de.fhdw.wtf.generator.java.visitor.GenClassVisitor;
42: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitor;
43: import de.fhdw.wtf.generator.java.visitor.GenPrimitiveTypeVisitor;
44: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitor;
45: import de.fhdw.wtf.generator.transformer.exception.GenTypeNotReferencedException;
46: import de.fhdw.wtf.walker.walker.HelperUtils;
47: import de.fhdw.wtf.walker.walker.SimpleWalkerTaskForTypes;
48:
49: /**
50: * This Transformer for inheritance adds all implements or extends relations to the {@link GeneratorModel}. If a
51: * {@link Class} has more than one superclass declared in wtf only the last one will be recognized.
52: */
53: public class InheritanceTransformer extends SimpleWalkerTaskForTypes {
54:         
55:         private final GeneratorModel generatorModel;
56:         
57:         /**
58:          * Constructor of {@link InheritanceTransformer}.
59:          *
60:          * @param m
61:          * The model to traverse.
62:          * @param taskmanager
63:          * The task manager to register with.
64:          * @param generatorModel
65:          * the model, which will be checked.
66:          */
67:         public InheritanceTransformer(final Model m,
68:                         final TaskExecutor taskmanager,
69:                         final GeneratorModel generatorModel,
70:                         final OperationAttributeTransformer operationAttributeTransformer) {
71:                 super(m, taskmanager, true);
72:                 this.generatorModel = generatorModel;
73:                 
74:                 try {
75:                         this.addDependency(operationAttributeTransformer);
76:                 } catch (final CyclicDependencyException e) {
77:                         // should not happen
78:                         e.printStackTrace();
79:                         throw new Error("Dependency tasks are cyclic in InheritanceTransformer.");
80:                 }
81:         }
82:         
83:         /**
84:          * Handles inheritance for products, classes and interfaces.
85:          */
86:         @Override
87:         public void handleType(final Type c) throws TaskException {
88:                 // new realization
89:                 
90:                 if (this.isEmptySum(c)) {
91:                         // The EmptySum will not be generated, because "void" represents it
92:                         return;
93:                 }
94:                 
95:                 final Collection<Type> superTypes = c.getSuperTypes();
96:                 
97:                 for (final Type superType : superTypes) {
98:                         this.createInheritance(c, superType);
99:                 }
100:         }
101:         
102:         private boolean isEmptySum(final Type c) {
103:                 return c.accept(new TypeVisitorReturn<Boolean>() {
104:                         
105:                         @Override
106:                         public Boolean handle(final AtomicType atomicType) {
107:                                 return false;
108:                         }
109:                         
110:                         @Override
111:                         public Boolean handle(final CompositeType compositeType) {
112:                                 return compositeType.accept(new CompositeTypeVisitorReturn<Boolean>() {
113:                                         
114:                                         @Override
115:                                         public Boolean handle(final ListType list) {
116:                                                 return false;
117:                                         }
118:                                         
119:                                         @Override
120:                                         public Boolean handle(final MapType map) {
121:                                                 return false;
122:                                         }
123:                                         
124:                                         @Override
125:                                         public Boolean handle(final ProductType product) {
126:                                                 return false;
127:                                         }
128:                                         
129:                                         @Override
130:                                         public Boolean handle(final SumType sum) {
131:                                                 return Integer.valueOf(0).equals(sum.getElementsSizeWithoutThrownTypes()); // Hier vielleicht
132:                                                                                                                                                                                                         // doch normal size.
133:                                         }
134:                                         
135:                                         @Override
136:                                         public Boolean handle(final ThrownType thrownType) {
137:                                                 return false;
138:                                         }
139:                                 });
140:                         }
141:                         
142:                         @Override
143:                         public Boolean handle(final TypeProxy typeProxy) {
144:                                 return InheritanceTransformer.this.isEmptySum(HelperUtils.getTargetType(typeProxy));
145:                         }
146:                 });
147:         }
148:         
149:         /**
150:          * Creates the inheritance from the {@link GenType}-representation for <code>subType</code> to the {@link GenType}
151:          * -representation for <code>superType</code>.
152:          *
153:          * @param subType
154:          * The {@link Type} that is a specialization of superType.
155:          * @param superType
156:          * The {@link Type} that is a generalization of subType.
157:          * @throws GenTypeNotReferencedException
158:          * Thrown when sub- or superType is not referenced in the Type-mapping of the generatorModel.
159:          */
160:         private void createInheritance(final Type subType, final Type superType) throws GenTypeNotReferencedException {
161:                 final GenType genSubType = this.generatorModel.getGenTypeForType(subType);
162:                 final GenType genSuperType = this.generatorModel.getGenTypeForType(superType);
163:                 genSubType.accept(new GenTypeVisitor() {
164:                         
165:                         @Override
166:                         public void handle(final GenClass genSubClass) {
167:                                 InheritanceTransformer.this.createInheritanceFromGenClass(genSubClass, genSuperType);
168:                         }
169:                         
170:                         @Override
171:                         public void handle(final GenPrimitiveType primitiveType) {
172:                                 primitiveType.accept(new GenPrimitiveTypeVisitor() {
173:                                         
174:                                         @Override
175:                                         public void handle(final GenVoidType voidType) {
176:                                                 throw new Error("Inheritance shall not be generated for GenVoidType.");
177:                                         }
178:                                         
179:                                         @Override
180:                                         public void handle(final GenStringType stringType) {
181:                                                 InheritanceTransformer.this.createInheritanceFromGenClass(
182:                                                                 GenStringType.getCorrespondingClass(),
183:                                                                 genSuperType);
184:                                         }
185:                                         
186:                                         @Override
187:                                         public void handle(final GenIntegerType integerType) {
188:                                                 InheritanceTransformer.this.createInheritanceFromGenClass(
189:                                                                 GenIntegerType.getCorrespondingClass(),
190:                                                                 genSuperType);
191:                                         }
192:                                 });
193:                         }
194:                         
195:                         @Override
196:                         public void handle(final GenDummyType dummy) {
197:                                 throw new Error("Inheritance shall not be generated for GenDummyType.");
198:                         }
199:                         
200:                         @Override
201:                         public void handle(final GenJavaUtilCollection javaUtilCollection) {
202:                                 throw new Error("Inheritance shall not be generated for GenJavaUtilCollection.");
203:                         }
204:                         
205:                         @Override
206:                         public void handle(final Generic generic) {
207:                                 throw new Error("Inheritance shall not be generated for Generic.");
208:                         }
209:                         
210:                         @Override
211:                         public void handle(final GenImportType importType) {
212:                                 throw new Error("Inheritance shall not be generated for GenImportType.");
213:                         }
214:                         
215:                         @Override
216:                         public void handle(final GenMapType mapType) {
217:                                 // map -> ?
218:                                 // generate inheritance for maps
219:                                 // inheritance will not be generated since maps are not represented by own classes
220:                         }
221:                         
222:                         @Override
223:                         public void handle(final GenCollectionType collectionType) {
224:                                 // collection -> ?
225:                                 // generate inheritance for collections
226:                                 // inheritance will not be generated since maps are not represented by own classes
227:                         }
228:                 });
229:         }
230:         
231:         /**
232:          * Creates the inheritance from a {@link GenClass} to a {@link GenType}.
233:          *
234:          * @param genSubClass
235:          * The {@link GenClass} that shall be a specialization of genSuperType.
236:          * @param genSuperType
237:          * The {@link GenType} that shall be a generalization of genSubClass.
238:          */
239:         private void createInheritanceFromGenClass(final GenClass genSubClass, final GenType genSuperType) {
240:                 genSuperType.accept(new GenTypeVisitor() {
241:                         
242:                         @Override
243:                         public void handle(final GenClass genSuperClass) {
244:                                 InheritanceTransformer.this.createInheritanceFromGenClassToGenClass(genSubClass, genSuperClass);
245:                         }
246:                         
247:                         @Override
248:                         public void handle(final GenPrimitiveType primitiveType) {
249:                                 // The class-representation of primitive Types are GenClasses. Therefore the inheritance needs to be
250:                                 // realized on those class-representations.
251:                                 // throw new Error("Inheritance shall not be generated if the superType is a GenPrimitiveType.");
252:                         }
253:                         
254:                         @Override
255:                         public void handle(final GenDummyType dummy) {
256:                                 throw new Error("Inheritance shall not be generated for GenDummyType.");
257:                         }
258:                         
259:                         @Override
260:                         public void handle(final GenJavaUtilCollection javaUtilCollection) {
261:                                 throw new Error("Inheritance shall not be generated for GenJavaUtilCollection.");
262:                         }
263:                         
264:                         @Override
265:                         public void handle(final Generic generic) {
266:                                 throw new Error("Inheritance shall not be generated for Generic.");
267:                         }
268:                         
269:                         @Override
270:                         public void handle(final GenImportType importType) {
271:                                 throw new Error("Inheritance shall not be generated for GenImportType.");
272:                         }
273:                         
274:                         @Override
275:                         public void handle(final GenMapType mapType) {
276:                                 throw new Error("Inheritance shall not be generated for GenMapType.");
277:                         }
278:                         
279:                         @Override
280:                         public void handle(final GenCollectionType collectionType) {
281:                                 throw new Error("Inheritance shall not be generated for GenCollectionType.");
282:                         }
283:                 });
284:         }
285:         
286:         /**
287:          * Creates the inheritance from a {@link GenClass} to a {@link GenClass}.
288:          *
289:          * @param genSubClass
290:          * The {@link GenClass} that shall be a specialization of genSuperClass.
291:          * @param genSuperClass
292:          * The {@link GenClass} that shall be a generalization of genSubClass.
293:          */
294:         private void createInheritanceFromGenClassToGenClass(final GenClass genSubClass, final GenClass genSuperClass) {
295:                 genSuperClass.accept(new GenClassVisitor() {
296:                         
297:                         @Override
298:                         public void handle(final GenInterfaceClass genSuperInterfaceClass) {
299:                                 // class/interface -> interface
300:                                 genSubClass.accept(new GenClassVisitor() {
301:                                         
302:                                         @Override
303:                                         public void handle(final GenPrimitiveClass genSubPrimitiveClass) {
304:                                                 // class (of primitive type) -> interface
305:                                                 final GenAspect aspectForBaseTypeExpansion =
306:                                                                 InheritanceTransformer.this.generatorModel.getAspectForType(genSubPrimitiveClass);
307:                                                 
308:                                                 aspectForBaseTypeExpansion.addDeclareParents(GenDeclareInheritance.create(
309:                                                                 genSuperInterfaceClass,
310:                                                                 genSubPrimitiveClass));
311:                                         }
312:                                         
313:                                         @Override
314:                                         public void handle(final GenInterfaceClass genSubInterfaceClass) {
315:                                                 // interface -> interface
316:                                                 genSubInterfaceClass.getImplement().add(genSuperInterfaceClass);
317:                                                 genSubInterfaceClass.accept(new GenInterfaceClassVisitor() {
318:                                                         
319:                                                         @Override
320:                                                         public void handle(final GenInterfaceWithClassImplClass genSubInterfaceWithImplClass) {
321:                                                                 // interface with Impl -> interface
322:                                                                 genSuperInterfaceClass.accept(new GenInterfaceClassVisitor() {
323:                                                                         
324:                                                                         @Override
325:                                                                         public void handle(final GenInterfaceWithClassImplClass genSuperIntWImplCls) {
326:                                                                                 // interface with Impl -> interface with Impl
327:                                                                                 // also add inheritance from Impl-class to Impl-class
328:                                                                                 
329:                                                                         }
330:                                                                         
331:                                                                         @Override
332:                                                                         public void handle(final GenSimpleInterfaceClass genSuperSimpleInterface) {
333:                                                                                 // interface with Impl -> real interface
334:                                                                                 // nothing more
335:                                                                         }
336:                                                                         
337:                                                                         @Override
338:                                                                         public void handle(final GenExternalInterfaceClass iface) {
339:                                                                                 // interface with Impl --> external interface
340:                                                                                 // nothing to do
341:                                                                         }
342:                                                                 });
343:                                                         }
344:                                                         
345:                                                         @Override
346:                                                         public void handle(final GenSimpleInterfaceClass genSubSimpleInterface) {
347:                                                                 // real interface -> interface
348:                                                                 // nothing more
349:                                                         }
350:                                                         
351:                                                         @Override
352:                                                         public void handle(final GenExternalInterfaceClass iface) {
353:                                                                 throw new Error("Interface " + genSuperInterfaceClass.getFullyQualifiedTypeName()
354:                                                                                 + " cannot be added as superinterface to external interface "
355:                                                                                 + iface.getFullyQualifiedTypeName());
356:                                                         }
357:                                                 });
358:                                         }
359:                                         
360:                                         @Override
361:                                         public void handle(final GenClassClass genSubClassClass) {
362:                                                 // class -> interface
363:                                                 genSuperInterfaceClass.accept(new GenInterfaceClassVisitor() {
364:                                                         
365:                                                         @Override
366:                                                         public void handle(final GenInterfaceWithClassImplClass interfaceWithImplClass) {
367:                                                                 // class -> interface with Impl
368:                                                                 
369:                                                         }
370:                                                         
371:                                                         @Override
372:                                                         public void handle(final GenSimpleInterfaceClass simpleInterface) {
373:                                                                 // class -> real interface
374:                                                                 genSubClassClass.getImplement().add(genSuperInterfaceClass);
375:                                                         }
376:                                                         
377:                                                         @Override
378:                                                         public void handle(final GenExternalInterfaceClass iface) {
379:                                                                 // class -> external interface
380:                                                                 genSubClassClass.getImplement().add(iface);
381:                                                         }
382:                                                 });
383:                                         }
384:                                 });
385:                         }
386:                         
387:                         @Override
388:                         public void handle(final GenClassClass genSuperClassClass) {
389:                                 // class/interface -> class
390:                                 genSubClass.accept(new GenClassVisitor() {
391:                                         
392:                                         @Override
393:                                         public void handle(final GenInterfaceClass genSubInterfaceClass) {
394:                                                 // interface -> class
395:                                                 throw new Error("Inheritance transformation not possible! An "
396:                                                                 + "interface can not extend a class.");
397:                                         }
398:                                         
399:                                         @Override
400:                                         public void handle(final GenClassClass genSubClassClass) {
401:                                                 // class -> class
402:                                                 genSubClassClass.setExtend(genSuperClassClass);
403:                                                 // InheritanceTransformer.this.maybeAddSuperConstructorCall(genSubClassClass);
404:                                                 
405:                                         }
406:                                         
407:                                         @Override
408:                                         public void handle(final GenPrimitiveClass genSubPrimitiveClass) {
409:                                                 // class (of primitive Type) -> class
410:                                                 throw new Error("Inheritance transformation not possible!"
411:                                                                 + "An class for an primitive type can not extend a class.");
412:                                         }
413:                                 });
414:                         }
415:                         
416:                         @Override
417:                         public void handle(final GenPrimitiveClass genSuperPrimitiveClass) {
418:                                 // class/interface -> class (of primitive Type)
419:                                 throw new Error("Inheritance from classes for primitive types is not allowed!");
420:                         }
421:                 });
422:         }
423:         
424:         @Override
425:         public String toString() {
426:                 return "Inheritance generation";
427:         }
428:         
429:         @Override
430:         public void beginTask() throws TaskException {
431:                 // nothing to do
432:         }
433:         
434:         @Override
435:         public void finalizeTask() throws TaskException {
436:                 // nothing to do
437:         }
438: }