Skip to content

Package: OperationAttributeTransformer$HandleAttributeGetOwnerGenClassVisitorException

OperationAttributeTransformer$HandleAttributeGetOwnerGenClassVisitorException

nameinstructionbranchcomplexitylinemethod
OperationAttributeTransformer.HandleAttributeGetOwnerGenClassVisitorException(OperationAttributeTransformer, GenJavaAttribute, Attribute)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
handle(GenClassClass)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handle(GenInterfaceClass)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handle(GenPrimitiveClass)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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