Skip to content

Package: OperationAttributeTransformer$4$1

OperationAttributeTransformer$4$1

nameinstructionbranchcomplexitylinemethod
handle(GenExternalInterfaceClass)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handle(GenInterfaceWithClassImplClass)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
handle(GenSimpleInterfaceClass)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
{...}
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.math.BigInteger;
4: import java.util.ArrayList;
5: import java.util.Arrays;
6: import java.util.Collection;
7: import java.util.HashSet;
8: import java.util.Iterator;
9: import java.util.List;
10: import java.util.Set;
11: import java.util.Vector;
12:
13: import de.fhdw.wtf.common.ast.Attribute;
14: import de.fhdw.wtf.common.ast.AttributeModifier;
15: import de.fhdw.wtf.common.ast.AttributeModifierFindable;
16: import de.fhdw.wtf.common.ast.AttributeModifierMutable;
17: import de.fhdw.wtf.common.ast.AttributeModifierPrior;
18: import de.fhdw.wtf.common.ast.AttributeModifierSymmetric;
19: import de.fhdw.wtf.common.ast.AttributeModifierTransient;
20: import de.fhdw.wtf.common.ast.Model;
21: import de.fhdw.wtf.common.ast.Operation;
22: import de.fhdw.wtf.common.ast.type.AtomicType;
23: import de.fhdw.wtf.common.ast.type.BaseType;
24: import de.fhdw.wtf.common.ast.type.ClassType;
25: import de.fhdw.wtf.common.ast.type.CompositeType;
26: import de.fhdw.wtf.common.ast.type.ListType;
27: import de.fhdw.wtf.common.ast.type.MapType;
28: import de.fhdw.wtf.common.ast.type.ProductElementType;
29: import de.fhdw.wtf.common.ast.type.ProductType;
30: import de.fhdw.wtf.common.ast.type.SumType;
31: import de.fhdw.wtf.common.ast.type.ThrownType;
32: import de.fhdw.wtf.common.ast.type.Type;
33: import de.fhdw.wtf.common.ast.type.TypeProxy;
34: import de.fhdw.wtf.common.ast.visitor.AtomicTypeVisitorException;
35: import de.fhdw.wtf.common.ast.visitor.AttributModifierVisitor;
36: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorException;
37: import de.fhdw.wtf.common.ast.visitor.OperationModifierVisitor;
38: import de.fhdw.wtf.common.ast.visitor.TypeVisitorException;
39: import de.fhdw.wtf.common.exception.walker.CyclicDependencyException;
40: import de.fhdw.wtf.common.exception.walker.TaskException;
41: import de.fhdw.wtf.common.task.TaskExecutor;
42: import de.fhdw.wtf.generator.java.generatorModel.GenAttributeModifier;
43: import de.fhdw.wtf.generator.java.generatorModel.GenClass;
44: import de.fhdw.wtf.generator.java.generatorModel.GenClassClass;
45: import de.fhdw.wtf.generator.java.generatorModel.GenClassModifier;
46: import de.fhdw.wtf.generator.java.generatorModel.GenComment;
47: import de.fhdw.wtf.generator.java.generatorModel.GenException;
48: import de.fhdw.wtf.generator.java.generatorModel.GenExternalClassClass;
49: import de.fhdw.wtf.generator.java.generatorModel.GenExternalInterfaceClass;
50: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceClass;
51: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceWithClassImplClass;
52: import de.fhdw.wtf.generator.java.generatorModel.GenJavaAttribute;
53: import de.fhdw.wtf.generator.java.generatorModel.GenJavaOperation;
54: import de.fhdw.wtf.generator.java.generatorModel.GenJavaUtilCollection;
55: import de.fhdw.wtf.generator.java.generatorModel.GenOperationModifier;
56: import de.fhdw.wtf.generator.java.generatorModel.GenPackage;
57: import de.fhdw.wtf.generator.java.generatorModel.GenParameter;
58: import de.fhdw.wtf.generator.java.generatorModel.GenPrimitiveClass;
59: import de.fhdw.wtf.generator.java.generatorModel.GenSimpleInterfaceClass;
60: import de.fhdw.wtf.generator.java.generatorModel.GenType;
61: import de.fhdw.wtf.generator.java.generatorModel.GenUnqualifiedPackage;
62: import de.fhdw.wtf.generator.java.generatorModel.GenUserClass;
63: import de.fhdw.wtf.generator.java.generatorModel.GenVisibility;
64: import de.fhdw.wtf.generator.java.generatorModel.GenVoidType;
65: import de.fhdw.wtf.generator.java.generatorModel.GeneratorModel;
66: import de.fhdw.wtf.generator.java.visitor.GenClassVisitor;
67: import de.fhdw.wtf.generator.java.visitor.GenClassVisitorException;
68: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitor;
69: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitorException;
70: import de.fhdw.wtf.generator.transformer.exception.GenTypeNotReferencedException;
71: import de.fhdw.wtf.generator.transformer.exception.NoAttributeAllowedInInterface;
72: import de.fhdw.wtf.walker.walker.SimpleWalkerTaskForTypes;
73:
74: public final class OperationAttributeTransformer extends SimpleWalkerTaskForTypes {
75:         
76:         private final GeneratorModel generatorModel;
77:         
78:         /**
79:          * String that represents the default method implementation.
80:          */
81:         private static final String DEFAULT_METHOD = "//TODO implement";
82:         
83:         /**
84:          * String that represents the name of the parameter of a load constructor.
85:          */
86:         public static final String LOAD_CONSTRUCTOR_PARAM_NAME = "userObject";
87:         
88:         /**
89:          * String that represents the type of the parameter of a load constructor.
90:          */
91:         public static final String LOAD_CONSTRUCTOR_PARAM_TYPE = "de.fhdw.wtf.persistence.meta.UserObject";
92:         
93:         /**
94:          * String that represents the default load constructor implementation.
95:          */
96:         private static final String DEFAULT_LOAD_CONSTRUCTOR = "super(" + LOAD_CONSTRUCTOR_PARAM_NAME + ");";
97:         
98:         /**
99:          * String that represents the default comment for an operation.
100:          */
101:         private static final String DEFAULT_OPERATION_COMMENT = "/** \n \t * TODO Comment Op \n\t */";
102:         
103:         /**
104:          * The default comment for constructor loading object from database.
105:          */
106:         private static final String DEFAULT_LOAD_CONSTRUCTOR_COMMENT =
107:                         "/** \n \t * Loads object from database.\n \t * @param userObject The underlying user object.\n\t */";
108:         
109:         /**
110:          * String that represents a java code line end.
111:          */
112:         private static final char JAVA_LINE_END = ';';
113:         
114:         /**
115:          * String that represents the default commit for a constructor of a product.
116:          */
117:         private static final String DEFAULT_PRODUCT_CONSTRUCTOR_COMMENT = "Create a anonym product.";
118:         
119:         /**
120:          * String that represents an equal Symbol.
121:          */
122:         private static final String EQUAL_SYMBOL = "=";
123:         
124:         /**
125:          * String that represents a this.
126:          */
127:         private static final String THIS_POINT = "this.";
128:         
129:         /**
130:          * String that represents a tab.
131:          */
132:         private static final String TAB = "\t";
133:         
134:         /**
135:          * String that represents a new line.
136:          */
137:         private static final String NEW_LINE = "\n";
138:         
139:         private OperationAttributeTransformer(final Model m,
140:                         final TaskExecutor taskmanager,
141:                         final GeneratorModel generatorModel,
142:                         final TypeTransformer typeTransformer) {
143:                 super(m, taskmanager, true);
144:                 this.generatorModel = generatorModel;
145:                 
146:                 try {
147:                         this.addDependency(typeTransformer);
148:                 } catch (final CyclicDependencyException e) {
149:                         // should not happen
150:                         e.printStackTrace();
151:                         throw new Error("Dependency tasks are cyclic in OperationAttributeTransformer.");
152:                 }
153:         }
154:         
155:         public static OperationAttributeTransformer create(final Model m,
156:                         final TaskExecutor taskmanager,
157:                         final GeneratorModel generatorModel,
158:                         final TypeTransformer typeTransformer) {
159:                 return new OperationAttributeTransformer(m, taskmanager, generatorModel, typeTransformer);
160:         }
161:         
162:         public GeneratorModel getGeneratorModel() {
163:                 return this.generatorModel;
164:         }
165:         
166:         @Override
167:         public void handleType(final Type c) throws TaskException {
168:                 c.accept(new TypeVisitorException<TaskException>() {
169:                         
170:                         @Override
171:                         public void handle(final AtomicType atomicType) throws TaskException {
172:                                 atomicType.accept(new AtomicTypeVisitorException<TaskException>() {
173:                                         
174:                                         @Override
175:                                         public void handle(final BaseType baseType) {
176:                                                 // BaseTypes are not generated
177:                                         }
178:                                         
179:                                         @Override
180:                                         public void handle(final ClassType clazz) throws TaskException {
181:                                                 final Iterator<Attribute> attributeIt = clazz.getAttributes().iterator();
182:                                                 while (attributeIt.hasNext()) {
183:                                                         final Attribute current = attributeIt.next();
184:                                                         OperationAttributeTransformer.this.handleAttribute(current, clazz);
185:                                                 }
186:                                                 
187:                                                 final Iterator<Operation> operationIt = clazz.getOperations().iterator();
188:                                                 while (operationIt.hasNext()) {
189:                                                         final Operation current = operationIt.next();
190:                                                         OperationAttributeTransformer.this.handleOperation(current, clazz);
191:                                                 }
192:                                                 final GenClass genOwner =
193:                                                                 OperationAttributeTransformer.this.getGeneratorModel().getClassMapping().get(clazz);
194:                                                 genOwner.accept(new GenClassVisitorException<TaskException>() {
195:                                                         
196:                                                         @Override
197:                                                         public void handle(final GenClassClass classClass) throws TaskException {
198:                                                                 OperationAttributeTransformer.this.createLoadConstructor(classClass);
199:                                                         }
200:                                                         
201:                                                         @Override
202:                                                         public void handle(final GenInterfaceClass interfaceClass) throws TaskException {
203:                                                                 interfaceClass.accept(new GenInterfaceClassVisitorException<TaskException>() {
204:                                                                         
205:                                                                         @Override
206:                                                                         public void handle(final GenSimpleInterfaceClass simpleInterface)
207:                                                                                         throws TaskException {
208:                                                                                 // Nothing to do.
209:                                                                                 // TODO Really nothing to do?
210:                                                                         }
211:                                                                         
212:                                                                         @Override
213:                                                                         public void handle(final GenInterfaceWithClassImplClass interfaceWithImplClass)
214:                                                                                         throws TaskException {
215:                                                                                 OperationAttributeTransformer.this.createLoadConstructor(interfaceWithImplClass
216:                                                                                                 .getImplementor());
217:                                                                         }
218:                                                                         
219:                                                                         @Override
220:                                                                         public void handle(final GenExternalInterfaceClass iface) throws TaskException {
221:                                                                                 // Nothing to do.
222:                                                                         }
223:                                                                         
224:                                                                 });
225:                                                         }
226:                                                         
227:                                                         @Override
228:                                                         public void handle(final GenPrimitiveClass primitiveClass) throws TaskException {
229:                                                                 OperationAttributeTransformer.this.createLoadConstructor(primitiveClass
230:                                                                                 .getImplementor());
231:                                                         }
232:                                                 });
233:                                         }
234:                                 });
235:                         }
236:                         
237:                         @Override
238:                         public void handle(final CompositeType compositeType) throws TaskException {
239:                                 compositeType.accept(new CompositeTypeVisitorException<TaskException>() {
240:                                         
241:                                         @Override
242:                                         public void handle(final SumType sum) {
243:                                                 // Sums do not have operations other than accept-Methods that are generated in
244:                                                 // VisitorTypeTransformer
245:                                         }
246:                                         
247:                                         @Override
248:                                         public void handle(final ProductType product) throws TaskException {
249:                                                 final GenClassClass prod =
250:                                                                 (GenClassClass) OperationAttributeTransformer.this.getGeneratorModel()
251:                                                                                 .getGenTypeForType(product);
252:                                                 if (product.equals(product.getAbstractPrototype())) {
253:                                                         // only add attributes to Products if they are the AbstractProductPrototype
254:                                                         OperationAttributeTransformer.this.handleProductAttributes(product, prod);
255:                                                 } else {
256:                                                         // only add getters and setters to Products if they are not the AbstractProductPrototype
257:                                                         OperationAttributeTransformer.this.generateProductsGettersAndSetters(product, prod);
258:                                                 }
259:                                                 // always create load constructor, regardless whether product type is or is not an abstract
260:                                                 // prototype or not
261:                                                 OperationAttributeTransformer.this.createLoadConstructor(prod);
262:                                         }
263:                                         
264:                                         @Override
265:                                         public void handle(final MapType map) {
266:                                                 // Maps are not generated
267:                                         }
268:                                         
269:                                         @Override
270:                                         public void handle(final ListType list) {
271:                                                 // Lists are not generated
272:                                         }
273:                                         
274:                                         @Override
275:                                         public void handle(final ThrownType thrownType) {
276:                                                 // ThrownTypes are not generated
277:                                         }
278:                                 });
279:                         }
280:                         
281:                         @Override
282:                         public void handle(final TypeProxy typeProxy) throws TaskException {
283:                                 // No need to handle TypeProxy
284:                                 
285:                         }
286:                 });
287:         }
288:         
289:         /**
290:          * Creates a special load constructor and adds it to a non-interface class.
291:          *
292:          * @param classClass
293:          * The GenClassClass object to add the constructor to.
294:          */
295:         private void createLoadConstructor(final GenClassClass classClass) {
296:                 // generate special constructor for loading object from database
297:                 final GenComment comment = GenComment.create(DEFAULT_LOAD_CONSTRUCTOR_COMMENT);
298:                 final GenType returnTyp = classClass;
299:                 final String method = DEFAULT_LOAD_CONSTRUCTOR;
300:                 final List<GenParameter> parameters = new Vector<>();
301:                 parameters.add(GenParameter.create(
302:                                 LOAD_CONSTRUCTOR_PARAM_NAME,
303:                                 GenExternalClassClass.getInstance(LOAD_CONSTRUCTOR_PARAM_TYPE)));
304:                 classClass.addConstructor(GenJavaOperation.create(
305:                                 "",
306:                                 GenVisibility.PUBLIC,
307:                                 parameters,
308:                                 new Vector<GenException>(),
309:                                 method,
310:                                 returnTyp,
311:                                 new Vector<GenOperationModifier>(),
312:                                 comment));
313:         }
314:         
315:         public void handleAttribute(final Attribute a, final ClassType owner) throws TaskException {
316:                 final String name = a.getName();
317:                 final GenType attrType = this.getGeneratorModel().getGenTypeForType(a.getAttrType());
318:                 final Collection<GenAttributeModifier> modifiers = this.handleAttributModifier(a, owner);
319:                 final GenJavaAttribute result = GenJavaAttribute.create(name, GenVisibility.PRIVATE, attrType, modifiers);
320:                 this.getGeneratorModel().getClassMapping().get(owner)
321:                                 .accept(new HandleAttributeGetOwnerGenClassVisitorException(result, a));
322:         }
323:         
324:         private final class HandleAttributeGetOwnerGenClassVisitorException implements
325:                         GenClassVisitorException<TaskException> {
326:                 private final GenJavaAttribute result;
327:                 private final Attribute a;
328:                 
329:                 private HandleAttributeGetOwnerGenClassVisitorException(final GenJavaAttribute result, final Attribute a) {
330:                         this.result = result;
331:                         this.a = a;
332:                 }
333:                 
334:                 @Override
335:                 public void handle(final GenClassClass classClass) throws TaskException {
336:                         classClass.getAttributes().add(this.result);
337:                         
338:                 }
339:                 
340:                 @Override
341:                 public void handle(final GenInterfaceClass interfaceClass) throws TaskException {
342:                         interfaceClass.accept(new GenInterfaceClassVisitorException<TaskException>() {
343:                                 
344:                                 @Override
345:                                 public void handle(final GenSimpleInterfaceClass simpleInterface) throws TaskException {
346:                                         throw NoAttributeAllowedInInterface.create();
347:                                 }
348:                                 
349:                                 @Override
350:                                 public void handle(final GenInterfaceWithClassImplClass interfaceWithImplClass) throws TaskException {
351:                                         HandleAttributeGetOwnerGenClassVisitorException.this.handle(interfaceWithImplClass
352:                                                         .getClassRepresentation());
353:                                 }
354:                                 
355:                                 @Override
356:                                 public void handle(final GenExternalInterfaceClass iface) throws TaskException {
357:                                         throw NoAttributeAllowedInInterface.create();
358:                                 }
359:                         });
360:                 }
361:                 
362:                 @Override
363:                 public void handle(final GenPrimitiveClass primitiveClass) throws NoAttributeAllowedInInterface {
364:                         // nothing to do
365:                 }
366:         }
367:         
368:         /**
369:          * Handles the occourence of {@link AttributeModifier}.
370:          *
371:          * @param a
372:          * @param owner
373:          * @return List of {@link AttributeModifier}
374:          */
375:         private Vector<GenAttributeModifier> handleAttributModifier(final Attribute a, final ClassType owner) {
376:                 final Vector<GenAttributeModifier> modifier = new Vector<>();
377:                 final GenAttributeModifier finalAttr = GenAttributeModifier.FINAL;
378:                 modifier.add(finalAttr);
379:                 final Iterator<AttributeModifier> i = a.getModifiers().iterator();
380:                 while (i.hasNext()) {
381:                         final AttributeModifier current = i.next();
382:                         current.accept(new AttributModifierVisitor() {
383:                                 @Override
384:                                 public boolean handle(final AttributeModifierMutable mutable) {
385:                                         return modifier.remove(finalAttr);
386:                                 }
387:                                 
388:                                 @Override
389:                                 public boolean handle(final AttributeModifierFindable findable) {
390:                                         return true;
391:                                 }
392:                                 
393:                                 @Override
394:                                 public boolean handle(final AttributeModifierTransient tranzient) {
395:                                         return modifier.add(GenAttributeModifier.TRANSIENT);
396:                                 }
397:                                 
398:                                 @Override
399:                                 public boolean handle(final AttributeModifierPrior prior) {
400:                                         // Final Attribute already in the list
401:                                         return true;
402:                                 }
403:                                 
404:                                 @Override
405:                                 public boolean handle(final AttributeModifierSymmetric symmetric) {
406:                                         return OperationAttributeTransformer.this.handleSymmetricAttributeModifier(a, symmetric, owner);
407:                                 }
408:                         });
409:                 }
410:                 modifier.remove(finalAttr);
411:                 return modifier;
412:         }
413:         
414:         private boolean handleSymmetricAttributeModifier(final Attribute a,
415:                         final AttributeModifierSymmetric modifier,
416:                         final ClassType owner) {
417:                 try {
418:                         final Vector<ClassType> targetClasses = this.getTargetClasses(a.getAttrType());
419:                         for (final ClassType targetClass : targetClasses) {
420:                                 this.addInverseGetterTo(a, targetClass, modifier, owner);
421:                         }
422:                 } catch (final GenTypeNotReferencedException e) {
423:                         e.printStackTrace();
424:                 }
425:                 return false;
426:         }
427:         
428:         private final Set<GenClass> singletonLst = new HashSet<GenClass>();
429:         
430:         private void addInverseGetterTo(final Attribute attribute,
431:                         final ClassType targetClass,
432:                         final AttributeModifierSymmetric modifier,
433:                         final ClassType owner) {
434:                 final GenClass symmetricRelationAccessClass = this.getSymmetricRelationAccessClassOfClass(owner, targetClass);
435:                 final String targetClassName = targetClass.getName().getLastAddedName().toString().toLowerCase();
436:                 final String inverseGetterIdentifier = modifier.getIdentifierToken().getIdentifier();
437:                 
438:                 GenType returnType;
439:                 String superMethodName;
440:                 Boolean isStarAssociation;
441:                 
442:                 // Entscheidung ob 0..1 oder * Assoziation
443:                 if (attribute.getAttrType() instanceof CompositeType) { // *
444:                         returnType = GenJavaUtilCollection.create(this.getGeneratorModel().getJavaClassForWTFClass(owner));
445:                         superMethodName = "inverseGetCollection";
446:                         isStarAssociation = true;
447:                 } else {
448:                         returnType = this.getGeneratorModel().getJavaClassForWTFClass(owner).getImplementor();
449:                         superMethodName = "<" + returnType.getFullyQualifiedTypeNameWithGenericArguments() + ">inverseGet";
450:                         isStarAssociation = false;
451:                 }
452:                 
453:                 final String[] strings =
454:                                 { owner.getName().getLastAddedName() + "", targetClass.getName().getLastAddedName() + "" };
455:                 Arrays.sort(strings);
456:                 String passNumber;
457:                 final String one = "1";
458:                 final String two = "2";
459:                 if (owner.equals(targetClass)) {
460:                         // reflexiv
461:                         if (this.singletonLst.contains(symmetricRelationAccessClass)) {
462:                                 passNumber = two;
463:                         } else {
464:                                 passNumber = one;
465:                                 this.singletonLst.add(symmetricRelationAccessClass);
466:                         }
467:                 } else if (strings[0].equals(owner.getName().getLastAddedName() + "")) {
468:                         passNumber = one;
469:                 } else {
470:                         passNumber = two;
471:                 }
472:                 
473:                 // Bei 1 Assoziation set1 und set2 erstellen
474:                 if (!isStarAssociation) {
475:                         final Vector<GenParameter> parameters = new Vector<>();
476:                         final GenParameter ownerParameter =
477:                                         GenParameter.create("owner", this.getGeneratorModel().getJavaClassForWTFClass(owner));
478:                         final GenParameter targetParameter =
479:                                         GenParameter.create("target", this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
480:                         parameters.add(ownerParameter);
481:                         parameters.add(targetParameter);
482:                         final String methodContent = "//TODO methodContent wird noch implementiert";
483:                         // TODO method
484:                         final GenJavaOperation setMethod =
485:                                         GenJavaOperation.create(
486:                                                         "set" + passNumber,
487:                                                         GenVisibility.PUBLIC,
488:                                                         parameters,
489:                                                         new Vector<GenException>(),
490:                                                         methodContent,
491:                                                         GenVoidType.getInstance(),
492:                                                         new Vector<GenOperationModifier>(),
493:                                                         GenComment.create(""));
494:                         symmetricRelationAccessClass.addOperation(setMethod);
495:                 } else {
496:                         // TODO hier muss noch add und remove für * Assoziation implementiert werden
497:                 }
498:                 
499:                 // TODO: Use constants/config-file instead of literals
500:                 final String associationName =
501:                                 "generated.model" + "." + owner.getName().toString().replace('>', '.') + "." + attribute.getName();
502:                 final String methodContent =
503:                                 "return this." + superMethodName + "(" + targetClassName + ", \"" + associationName + "\");";
504:                 
505:                 final GenJavaOperation inverseGetter =
506:                                 GenJavaOperation.create(
507:                                                 inverseGetterIdentifier,
508:                                                 GenVisibility.PUBLIC,
509:                                                 new Vector<GenParameter>(),
510:                                                 new Vector<GenException>(),
511:                                                 methodContent,
512:                                                 returnType,
513:                                                 new Vector<GenOperationModifier>(),
514:                                                 GenComment.create(""));
515:                 
516:                 // final GenJavaOperation
517:                 
518:                 final GenClass genClass = this.getGeneratorModel().getJavaClassForWTFClass(targetClass);
519:                 final GenClassClass genClassClass = genClass.getImplementor();
520:                 final GenParameter newParam = GenParameter.create(targetClassName, genClassClass);
521:                 inverseGetter.getParameters().add(newParam);
522:                 
523:                 symmetricRelationAccessClass.addOperation(inverseGetter);
524:         }
525:         
526:         private Vector<ClassType> getTargetClasses(final Type attrTypeOccurance) throws GenTypeNotReferencedException {
527:                 final Vector<ClassType> targetTypes = new Vector<>();
528:                 final Type attributeType = UtilTransformer.getTypeProxyFreePrototype(attrTypeOccurance);
529:                 attributeType.accept(new TypeVisitorException<GenTypeNotReferencedException>() {
530:                         
531:                         @Override
532:                         public void handle(final AtomicType s) throws GenTypeNotReferencedException {
533:                                 s.accept(new AtomicTypeVisitorException<GenTypeNotReferencedException>() {
534:                                         
535:                                         @Override
536:                                         public void handle(final BaseType baseType) throws GenTypeNotReferencedException {
537:                                                 // NOthing
538:                                         }
539:                                         
540:                                         @Override
541:                                         public void handle(final ClassType clazz) throws GenTypeNotReferencedException {
542:                                                 targetTypes.add(clazz);
543:                                         }
544:                                 });
545:                         }
546:                         
547:                         @Override
548:                         public void handle(final CompositeType c) throws GenTypeNotReferencedException {
549:                                 
550:                                 c.accept(new CompositeTypeVisitorException<GenTypeNotReferencedException>() {
551:                                         
552:                                         @Override
553:                                         public void handle(final MapType map) throws GenTypeNotReferencedException {
554:                                                 targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(map.getOf()));
555:                                                 
556:                                         }
557:                                         
558:                                         @Override
559:                                         public void handle(final ListType list) throws GenTypeNotReferencedException {
560:                                                 targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(list.getOf()));
561:                                                 
562:                                         }
563:                                         
564:                                         @Override
565:                                         public void handle(final ProductType product) throws GenTypeNotReferencedException {
566:                                                 for (final ProductElementType factorTypeOccurance : product.getElements()) {
567:                                                         targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(factorTypeOccurance
568:                                                                         .getType()));
569:                                                 }
570:                                         }
571:                                         
572:                                         @Override
573:                                         public void handle(final SumType sum) throws GenTypeNotReferencedException {
574:                                                 for (final Type summandTypeOccurance : sum.getElements()) {
575:                                                         targetTypes.addAll(OperationAttributeTransformer.this
576:                                                                         .getTargetClasses(summandTypeOccurance));
577:                                                 }
578:                                         }
579:                                         
580:                                         @Override
581:                                         public void handle(final ThrownType thrownType) throws GenTypeNotReferencedException {
582:                                                 // nothing to do
583:                                         }
584:                                 });
585:                         }
586:                         
587:                         @Override
588:                         public void handle(final TypeProxy s) throws GenTypeNotReferencedException {
589:                                 // nothing to do
590:                         }
591:                 });
592:                 return targetTypes;
593:         }
594:         
595:         private GenClass getSymmetricRelationAccessClassOfClass(final ClassType ownerClass, final ClassType targetClass) {
596:                 // Bsp.: A
597:                 final String ownerClassName = ownerClass.getName().getLastAddedName() + "";
598:                 
599:                 // Bsp.: B
600:                 final String targetClassName = targetClass.getName().getLastAddedName() + "";
601:                 
602:                 // Sortiere Klassen Lexikographisch
603:                 final String[] strings = { ownerClassName, targetClassName };
604:                 Arrays.sort(strings);
605:                 
606:                 // Name des Singletons der für die Relation verantwortlich ist, Bsp.: A_BSymmetricRelationAccess
607:                 final String symmetricRelationAccessName = strings[0] + "_" + strings[1] + "SymmetricRelationAccess";
608:                 
609:                 for (final GenClass knownClass : this.getGeneratorModel().getClasses()) {
610:                         if (knownClass.getName().equals(symmetricRelationAccessName)) {
611:                                 return knownClass;
612:                         }
613:                 }
614:                 
615:                 GenPackage superClassPackage = GenUnqualifiedPackage.create("de");
616:                 superClassPackage = superClassPackage.addName("fhdw");
617:                 superClassPackage = superClassPackage.addName("wtf");
618:                 superClassPackage = superClassPackage.addName("context");
619:                 superClassPackage = superClassPackage.addName("model");
620:                 
621:                 final GenUserClass symmetricRelationAccessSuperClass =
622:                                 GenUserClass.create(
623:                                                 "SymmetricRelationAccess",
624:                                                 new Vector<GenJavaOperation>(),
625:                                                 new Vector<GenInterfaceClass>(),
626:                                                 new Vector<GenJavaAttribute>(),
627:                                                 new Vector<GenClassModifier>(),
628:                                                 new Vector<GenJavaOperation>(),
629:                                                 null,
630:                                                 superClassPackage,
631:                                                 GenComment.createFromPlainText("", false),
632:                                                 "");
633:                 
634:                 GenPackage classPackage = GenUnqualifiedPackage.create("generated");
635:                 classPackage = classPackage.addName("symmetry");
636:                 
637:                 final GenUserClass symmetricRelationAccessClass =
638:                                 GenUserClass.create(
639:                                                 symmetricRelationAccessName,
640:                                                 new Vector<GenJavaOperation>(),
641:                                                 new Vector<GenInterfaceClass>(),
642:                                                 new Vector<GenJavaAttribute>(),
643:                                                 new Vector<GenClassModifier>(),
644:                                                 new Vector<GenJavaOperation>(),
645:                                                 symmetricRelationAccessSuperClass,
646:                                                 classPackage,
647:                                                 GenComment.createFromPlainText("", false),
648:                                                 "");
649:                 
650:                 final Vector<GenAttributeModifier> instanceAttributeModifiers = new Vector<>();
651:                 instanceAttributeModifiers.add(GenAttributeModifier.STATIC);
652:                 final GenJavaAttribute instanceAttribute =
653:                                 GenJavaAttribute.create(
654:                                                 "instance",
655:                                                 GenVisibility.PRIVATE,
656:                                                 symmetricRelationAccessClass,
657:                                                 instanceAttributeModifiers);
658:                 
659:                 final Vector<GenOperationModifier> getInstanceModifiers = new Vector<>();
660:                 getInstanceModifiers.add(GenOperationModifier.STATIC);
661:                 final GenJavaOperation getInstanceOperation =
662:                                 GenJavaOperation.create(
663:                                                 "getInstance",
664:                                                 GenVisibility.PUBLIC,
665:                                                 new Vector<GenParameter>(),
666:                                                 new Vector<GenException>(),
667:                                                 "if(instance == null) instance = new " + symmetricRelationAccessName + "(); return instance;",
668:                                                 symmetricRelationAccessClass,
669:                                                 getInstanceModifiers,
670:                                                 GenComment.create(""));
671:                 
672:                 final GenJavaOperation constructor =
673:                                 GenJavaOperation.createConstructor(
674:                                                 symmetricRelationAccessClass,
675:                                                 GenVisibility.PRIVATE,
676:                                                 new Vector<GenParameter>(),
677:                                                 new Vector<GenException>(),
678:                                                 "",
679:                                                 GenComment.create(""));
680:                 
681:                 symmetricRelationAccessClass.getAttributes().add(instanceAttribute);
682:                 symmetricRelationAccessClass.addOperation(getInstanceOperation);
683:                 symmetricRelationAccessClass.getConstructors().add(constructor);
684:                 
685:                 this.getGeneratorModel().addNonAstClass(symmetricRelationAccessClass);
686:                 this.getGeneratorModel().addSymmetricManager(symmetricRelationAccessClass, ownerClass);
687:                 this.getGeneratorModel().addSymmetricManager(symmetricRelationAccessClass, targetClass);
688:                 return symmetricRelationAccessClass;
689:         }
690:         
691:         public void handleOperation(final Operation o, final ClassType owner) throws TaskException {
692:                 final GenComment comment = GenComment.create(DEFAULT_OPERATION_COMMENT);
693:                 final String name = o.getName();
694:                 final Vector<GenOperationModifier> modifiers = this.getOperationModifiers(o);
695:                 final Collection<GenException> exceptions = this.getGeneratorModel().getGenExceptionsForType(o.getReturnType());
696:                 final GenType returnTyp = this.getGeneratorModel().getGenTypeForType(o.getReturnType());
697:                 final String method =
698:                                 DEFAULT_METHOD + (returnTyp.getFullyQualifiedTypeName().equals("void") ? "" : "\n\t\treturn null;");
699:                 final List<GenParameter> parameters = this.handleParameter(o.getParameters());
700:                 final GenJavaOperation result =
701:                                 GenJavaOperation.create(
702:                                                 name,
703:                                                 GenVisibility.PUBLIC,
704:                                                 parameters,
705:                                                 exceptions,
706:                                                 method,
707:                                                 returnTyp,
708:                                                 modifiers,
709:                                                 comment);
710:                 
711:                 final GenClass genOwner = this.getGeneratorModel().getClassMapping().get(owner);
712:                 genOwner.accept(new GenClassVisitor() {
713:                         
714:                         @Override
715:                         public void handle(final GenPrimitiveClass primitiveClass) {
716:                                 // primitiveClass will not be generated, adding operation is useless
717:                         }
718:                         
719:                         @Override
720:                         public void handle(final GenInterfaceClass interfaceClass) {
721:                                 
722:                                 interfaceClass.accept(new GenInterfaceClassVisitor() {
723:                                         
724:                                         @Override
725:                                         public void handle(final GenInterfaceWithClassImplClass interfaceWithImplClass) {
726:                                                 interfaceWithImplClass.addOperation(result);
727:                                                 interfaceWithImplClass.getClassRepresentation().addOperation(result);
728:                                         }
729:                                         
730:                                         @Override
731:                                         public void handle(final GenSimpleInterfaceClass simpleInterface) {
732:                                                 simpleInterface.addOperation(result);
733:                                         }
734:                                         
735:                                         @Override
736:                                         public void handle(final GenExternalInterfaceClass iface) {
737:                                                 throw new Error("Operation " + o.getName() + " cannot be added to external interface "
738:                                                                 + iface.getFullyQualifiedTypeName());
739:                                         }
740:                                 });
741:                         }
742:                         
743:                         @Override
744:                         public void handle(final GenClassClass classClass) {
745:                                 classClass.addOperation(result);
746:                         }
747:                 });
748:         }
749:         
750:         /**
751:          * Returns a {@link Vector} with {@link GenOperationModifier} for the {@link Operation} <code>o</code>.
752:          *
753:          * @param o
754:          * o
755:          * @return Vector<GenOperationModifier>
756:          */
757:         private Vector<GenOperationModifier> getOperationModifiers(final Operation o) {
758:                 final Vector<GenOperationModifier> result = new Vector<>();
759:                 final Iterator<de.fhdw.wtf.common.ast.OperationModifier> i = o.getModifiers().iterator();
760:                 while (i.hasNext()) {
761:                         final de.fhdw.wtf.common.ast.OperationModifier current = i.next();
762:                         current.accept(new OperationModifierVisitor() {
763:                                 @Override
764:                                 public boolean handle(final de.fhdw.wtf.common.ast.OperationModifier abstract1) {
765:                                         result.add(GenOperationModifier.ABSTRACT);
766:                                         return false;
767:                                 }
768:                         });
769:                 }
770:                 return result;
771:         }
772:         
773:         /**
774:          * TODO Janik / Tilmann soll das wirklich so?!
775:          *
776:          * @param o
777:          * @return
778:          * @throws TaskException
779:          */
780:         protected List<GenParameter> handleParameter(final ProductType o) throws TaskException {
781:                 final List<ProductElementType> elements = o.getElements();
782:                 final Vector<GenParameter> result = new Vector<>();
783:                 for (final ProductElementType productElement : elements) {
784:                         final GenType paramType = this.getGeneratorModel().getGenTypeForType(productElement.getType());
785:                         result.add(GenParameter.create(productElement.getName(), paramType));
786:                 }
787:                 return result;
788:         }
789:         
790:         // BEGIN PRODUCTS
791:         
792:         /**
793:          * For each element of <code>product</code> this methods adds an <code>protected</code> attribute to the
794:          * {@link GenClassClass} that represents the {@link ProductType}.
795:          *
796:          * @param product
797:          * given {@link ProductType} to add attributes for.
798:          * @param prod
799:          * The underlying {@link GenClassClass} for the product passed.
800:          * @throws TaskException
801:          * Thrown when the product or an element-Type is not referenced in the {@link GeneratorModel}.
802:          */
803:         private void handleProductAttributes(final ProductType product, final GenClassClass prod) throws TaskException {
804:                 final StringBuilder methodString = new StringBuilder();
805:                 final List<GenParameter> parameters = new Vector<>();
806:                 final Iterator<ProductElementType> i = product.getElements().iterator();
807:                 while (i.hasNext()) {
808:                         final ProductElementType element = i.next();
809:                         final Collection<GenAttributeModifier> modifiers = new Vector<>();
810:                         final GenType type = this.getGeneratorModel().getGenTypeForType(element.getType());
811:                         final GenJavaAttribute currentAttr =
812:                                         GenJavaAttribute.create(element.getName(), GenVisibility.PROTECTED, type, modifiers);
813:                         prod.getAttributes().add(currentAttr);
814:                         
815:                         final String methodStringInitialize =
816:                                         THIS_POINT + currentAttr.getName() + EQUAL_SYMBOL + currentAttr.getName() + JAVA_LINE_END;
817:                         if (methodString.toString().equals("")) {
818:                                 methodString.append(methodStringInitialize);
819:                         } else {
820:                                 methodString.append(NEW_LINE + TAB + TAB + methodStringInitialize);
821:                         }
822:                         final GenParameter parameter = GenParameter.create(currentAttr.getName(), currentAttr.getTyp());
823:                         parameters.add(parameter);
824:                 }
825:                 // constructor
826:                 final GenComment comment = GenComment.createFromPlainText(DEFAULT_PRODUCT_CONSTRUCTOR_COMMENT, true);
827:                 final GenJavaOperation constructor =
828:                                 GenJavaOperation.createConstructor(
829:                                                 prod,
830:                                                 GenVisibility.PUBLIC,
831:                                                 parameters,
832:                                                 new Vector<GenException>(),
833:                                                 methodString.toString(),
834:                                                 comment);
835:                 prod.getConstructors().add(constructor);
836:         }
837:         
838:         /**
839:          * For each element of <code>product</code> this operation adds an getter and an setter to the {@link GenClassClass}
840:          * that represents the {@link ProductType}. Furthermore this operation adds a constructor based on all attributes to
841:          * the {@link GenClassClass}.
842:          *
843:          * @param product
844:          * given {@link ProductType} to generate getters, setters and a constructor for.
845:          * @param prod
846:          * The underlying {@link GenClassClass} for the product passed.
847:          * @throws TaskException
848:          * Thrown when the product or an element-Type is not referenced in the {@link GeneratorModel}.
849:          */
850:         private void generateProductsGettersAndSetters(final ProductType product, final GenClassClass prod)
851:                         throws TaskException {
852:                 final Iterator<ProductElementType> i = product.getElements().iterator();
853:                 BigInteger currentElementCount = BigInteger.ZERO;
854:                 while (i.hasNext()) {
855:                         currentElementCount = currentElementCount.add(BigInteger.ONE);
856:                         final ProductElementType current = i.next();
857:                         final String name = current.getName();
858:                         final String capitalName = name.substring(0, 1).toUpperCase() + name.substring(1);
859:                         final Collection<GenException> exceptions = new Vector<>();
860:                         final GenType prodElementType = this.getGeneratorModel().getGenTypeForType(current.getType());
861:                         final Collection<GenOperationModifier> operationModifier = new Vector<>();
862:                         // getter
863:                         final List<GenParameter> paramsGetter = new ArrayList<>();
864:                         final String implementationGetter = "return this.p$" + currentElementCount.toString() + ";";
865:                         prod.addOperation(GenJavaOperation.create(
866:                                         "get" + capitalName,
867:                                         GenVisibility.PUBLIC,
868:                                         paramsGetter,
869:                                         exceptions,
870:                                         implementationGetter,
871:                                         prodElementType,
872:                                         operationModifier,
873:                                         GenComment.createFromPlainText("", true)));
874:                         // setter
875:                         final List<GenParameter> paramsSetter = new ArrayList<>();
876:                         final GenParameter setParam = GenParameter.create(name, prodElementType);
877:                         paramsSetter.add(setParam);
878:                         final String implementationSetter = "this.p$" + currentElementCount.toString() + " = " + name + ";";
879:                         prod.addOperation(GenJavaOperation.create(
880:                                         "set" + capitalName,
881:                                         GenVisibility.PUBLIC,
882:                                         paramsSetter,
883:                                         exceptions,
884:                                         implementationSetter,
885:                                         GenVoidType.getInstance(),
886:                                         operationModifier,
887:                                         GenComment.createFromPlainText("", false)));
888:                 }
889:         }
890:         
891:         // END PRODUCTS
892:         
893:         @Override
894:         public String toString() {
895:                 return "Operation and Attribute generation";
896:         }
897:         
898:         @Override
899:         public void beginTask() throws TaskException {
900:                 // Nothing
901:         }
902:         
903:         @Override
904:         public void finalizeTask() throws TaskException {
905:                 // Nothing
906:         }
907: }