Skip to content

Method: handle(GenInterfaceClass)

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:                 final String targetString = "target";
435:                 final String ownerString = "owner";
436:                 
437:                 GenType returnType;
438:                 String superMethodName;
439:                 Boolean isStarAssociation;
440:                 
441:                 // Entscheidung ob 0..1 oder * Assoziation
442:                 if (attribute.getAttrType() instanceof CompositeType) { // *
443:                         returnType = GenJavaUtilCollection.create(this.getGeneratorModel().getJavaClassForWTFClass(owner));
444:                         superMethodName = "inverseGetCollection";
445:                         isStarAssociation = true;
446:                 } else {
447:                         returnType = this.getGeneratorModel().getJavaClassForWTFClass(owner).getImplementor();
448:                         superMethodName = "<" + returnType.getFullyQualifiedTypeNameWithGenericArguments() + ">inverseGet";
449:                         isStarAssociation = false;
450:                 }
451:                 final Vector<GenParameter> registerSetterParameters = new Vector<>();
452:                 // TODO final GenParameter ownerParameter0 = GenParameter.create("setter", target.getSetter());
453:                 final GenParameter targetParameter0 =
454:                                 GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
455:                 // registerSetterParameters.add(ownerParameter0);
456:                 registerSetterParameters.add(targetParameter0);
457:                 final GenJavaOperation registerSetterMethod =
458:                                 GenJavaOperation.create(
459:                                                 "registerSetter" + attribute.getName(),
460:                                                 GenVisibility.PUBLIC,
461:                                                 registerSetterParameters,
462:                                                 new Vector<GenException>(),
463:                                                 "",
464:                                                 GenVoidType.getInstance(),
465:                                                 new Vector<GenOperationModifier>(),
466:                                                 GenComment.create(""));
467:                 symmetricRelationAccessClass.addOperation(registerSetterMethod);
468:                 
469:                 // Bei 1 Assoziation set erstellen
470:                 if (!isStarAssociation) {
471:                         final Vector<GenParameter> parameters = new Vector<>();
472:                         final GenParameter ownerParameter =
473:                                         GenParameter.create(ownerString, this.getGeneratorModel().getJavaClassForWTFClass(owner));
474:                         final GenParameter targetParameter =
475:                                         GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
476:                         parameters.add(ownerParameter);
477:                         parameters.add(targetParameter);
478:                         final String methodContent = "//TODO methodContent wird noch implementiert";
479:                         // TODO method
480:                         final GenJavaOperation setMethod =
481:                                         GenJavaOperation.create(
482:                                                         "set" + attribute.getName(),
483:                                                         GenVisibility.PUBLIC,
484:                                                         parameters,
485:                                                         new Vector<GenException>(),
486:                                                         methodContent,
487:                                                         GenVoidType.getInstance(),
488:                                                         new Vector<GenOperationModifier>(),
489:                                                         GenComment.create(""));
490:                         symmetricRelationAccessClass.addOperation(setMethod);
491:                         
492:                         // final Collection<GenAttributeModifier> genAttrModifier = null;
493:                         // final GenJavaAttribute hashMap =
494:                         // GenJavaAttribute.create(
495:                         // "mapHALLO" + attribute.getName(),
496:                         // GenVisibility.PRIVATE,
497:                         // GenVoidType.getInstance(),
498:                         // genAttrModifier);
499:                         //
500:                         // ((GenClassClass) symmetricRelationAccessClass).getAttributes().add(hashMap);
501:                         
502:                 } else {
503:                         // TODO hier muss noch add und remove für * Assoziation implementiert werden
504:                         final Vector<GenParameter> parameters1 = new Vector<>();
505:                         final GenParameter ownerParameter1 =
506:                                         GenParameter.create(ownerString, this.getGeneratorModel().getJavaClassForWTFClass(owner));
507:                         final GenParameter targetParameter1 =
508:                                         GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
509:                         parameters1.add(ownerParameter1);
510:                         parameters1.add(targetParameter1);
511:                         final String methodContentAdd = "//TODO methodContent wird noch implementiert.";
512:                         // TODO method
513:                         final GenJavaOperation addMethod =
514:                                         GenJavaOperation.create(
515:                                                         "add" + attribute.getName(),
516:                                                         GenVisibility.PUBLIC,
517:                                                         parameters1,
518:                                                         new Vector<GenException>(),
519:                                                         methodContentAdd,
520:                                                         GenVoidType.getInstance(),
521:                                                         new Vector<GenOperationModifier>(),
522:                                                         GenComment.create(""));
523:                         
524:                         final Vector<GenParameter> parameters2 = new Vector<>();
525:                         final GenParameter ownerParameter2 =
526:                                         GenParameter.create(ownerString, this.getGeneratorModel().getJavaClassForWTFClass(owner));
527:                         final GenParameter targetParameter2 =
528:                                         GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
529:                         parameters2.add(ownerParameter2);
530:                         parameters2.add(targetParameter2);
531:                         final String methodContentRemove = "//methodContent wird noch implementiert.";
532:                         // TODO method.
533:                         final GenJavaOperation removeMethod =
534:                                         GenJavaOperation.create(
535:                                                         "remove" + attribute.getName(),
536:                                                         GenVisibility.PUBLIC,
537:                                                         parameters2,
538:                                                         new Vector<GenException>(),
539:                                                         methodContentRemove,
540:                                                         GenVoidType.getInstance(),
541:                                                         new Vector<GenOperationModifier>(),
542:                                                         GenComment.create(""));
543:                         symmetricRelationAccessClass.addOperation(removeMethod);
544:                         symmetricRelationAccessClass.addOperation(addMethod);
545:                 }
546:                 
547:                 // TODO: Use constants/config-file instead of literals
548:                 final String associationName =
549:                                 "generated.model" + "." + owner.getName().toString().replace('>', '.') + "." + attribute.getName();
550:                 final String methodContent =
551:                                 "return this." + superMethodName + "(" + targetClassName + ", \"" + associationName + "\");";
552:                 
553:                 final GenJavaOperation inverseGetter =
554:                                 GenJavaOperation.create(
555:                                                 inverseGetterIdentifier,
556:                                                 GenVisibility.PUBLIC,
557:                                                 new Vector<GenParameter>(),
558:                                                 new Vector<GenException>(),
559:                                                 methodContent,
560:                                                 returnType,
561:                                                 new Vector<GenOperationModifier>(),
562:                                                 GenComment.create(""));
563:                 
564:                 // final GenJavaOperation
565:                 
566:                 final GenClass genClass = this.getGeneratorModel().getJavaClassForWTFClass(targetClass);
567:                 final GenClassClass genClassClass = genClass.getImplementor();
568:                 final GenParameter newParam = GenParameter.create(targetClassName, genClassClass);
569:                 inverseGetter.getParameters().add(newParam);
570:                 
571:                 symmetricRelationAccessClass.addOperation(inverseGetter);
572:         }
573:         
574:         private Vector<ClassType> getTargetClasses(final Type attrTypeOccurance) throws GenTypeNotReferencedException {
575:                 final Vector<ClassType> targetTypes = new Vector<>();
576:                 final Type attributeType = UtilTransformer.getTypeProxyFreePrototype(attrTypeOccurance);
577:                 attributeType.accept(new TypeVisitorException<GenTypeNotReferencedException>() {
578:                         
579:                         @Override
580:                         public void handle(final AtomicType s) throws GenTypeNotReferencedException {
581:                                 s.accept(new AtomicTypeVisitorException<GenTypeNotReferencedException>() {
582:                                         
583:                                         @Override
584:                                         public void handle(final BaseType baseType) throws GenTypeNotReferencedException {
585:                                                 // NOthing
586:                                         }
587:                                         
588:                                         @Override
589:                                         public void handle(final ClassType clazz) throws GenTypeNotReferencedException {
590:                                                 targetTypes.add(clazz);
591:                                         }
592:                                 });
593:                         }
594:                         
595:                         @Override
596:                         public void handle(final CompositeType c) throws GenTypeNotReferencedException {
597:                                 
598:                                 c.accept(new CompositeTypeVisitorException<GenTypeNotReferencedException>() {
599:                                         
600:                                         @Override
601:                                         public void handle(final MapType map) throws GenTypeNotReferencedException {
602:                                                 targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(map.getOf()));
603:                                                 
604:                                         }
605:                                         
606:                                         @Override
607:                                         public void handle(final ListType list) throws GenTypeNotReferencedException {
608:                                                 targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(list.getOf()));
609:                                                 
610:                                         }
611:                                         
612:                                         @Override
613:                                         public void handle(final ProductType product) throws GenTypeNotReferencedException {
614:                                                 for (final ProductElementType factorTypeOccurance : product.getElements()) {
615:                                                         targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(factorTypeOccurance
616:                                                                         .getType()));
617:                                                 }
618:                                         }
619:                                         
620:                                         @Override
621:                                         public void handle(final SumType sum) throws GenTypeNotReferencedException {
622:                                                 for (final Type summandTypeOccurance : sum.getElements()) {
623:                                                         targetTypes.addAll(OperationAttributeTransformer.this
624:                                                                         .getTargetClasses(summandTypeOccurance));
625:                                                 }
626:                                         }
627:                                         
628:                                         @Override
629:                                         public void handle(final ThrownType thrownType) throws GenTypeNotReferencedException {
630:                                                 // nothing to do
631:                                         }
632:                                 });
633:                         }
634:                         
635:                         @Override
636:                         public void handle(final TypeProxy s) throws GenTypeNotReferencedException {
637:                                 // nothing to do
638:                         }
639:                 });
640:                 return targetTypes;
641:         }
642:         
643:         private GenClass getSymmetricRelationAccessClassOfClass(final ClassType ownerClass, final ClassType targetClass) {
644:                 // Bsp.: A
645:                 final String ownerClassName = ownerClass.getName().getLastAddedName() + "";
646:                 
647:                 // Bsp.: B
648:                 final String targetClassName = targetClass.getName().getLastAddedName() + "";
649:                 
650:                 // Sortiere Klassen Lexikographisch
651:                 final String[] strings = { ownerClassName, targetClassName };
652:                 Arrays.sort(strings);
653:                 
654:                 // Name des Singletons der für die Relation verantwortlich ist, Bsp.: A_BSymmetricRelationAccess
655:                 final String symmetricRelationAccessName = strings[0] + "_" + strings[1] + "SymmetricRelationAccess";
656:                 
657:                 for (final GenClass knownClass : this.getGeneratorModel().getClasses()) {
658:                         if (knownClass.getName().equals(symmetricRelationAccessName)) {
659:                                 return knownClass;
660:                         }
661:                 }
662:                 
663:                 GenPackage superClassPackage = GenUnqualifiedPackage.create("de");
664:                 superClassPackage = superClassPackage.addName("fhdw");
665:                 superClassPackage = superClassPackage.addName("wtf");
666:                 superClassPackage = superClassPackage.addName("context");
667:                 superClassPackage = superClassPackage.addName("model");
668:                 
669:                 final GenUserClass symmetricRelationAccessSuperClass =
670:                                 GenUserClass.create(
671:                                                 "SymmetricRelationAccess",
672:                                                 new Vector<GenJavaOperation>(),
673:                                                 new Vector<GenInterfaceClass>(),
674:                                                 new Vector<GenJavaAttribute>(),
675:                                                 new Vector<GenClassModifier>(),
676:                                                 new Vector<GenJavaOperation>(),
677:                                                 null,
678:                                                 superClassPackage,
679:                                                 GenComment.createFromPlainText("", false),
680:                                                 "");
681:                 
682:                 GenPackage classPackage = GenUnqualifiedPackage.create("generated");
683:                 classPackage = classPackage.addName("symmetry");
684:                 
685:                 final GenUserClass symmetricRelationAccessClass =
686:                                 GenUserClass.create(
687:                                                 symmetricRelationAccessName,
688:                                                 new Vector<GenJavaOperation>(),
689:                                                 new Vector<GenInterfaceClass>(),
690:                                                 new Vector<GenJavaAttribute>(),
691:                                                 new Vector<GenClassModifier>(),
692:                                                 new Vector<GenJavaOperation>(),
693:                                                 symmetricRelationAccessSuperClass,
694:                                                 classPackage,
695:                                                 GenComment.createFromPlainText("", false),
696:                                                 "");
697:                 
698:                 final Vector<GenAttributeModifier> instanceAttributeModifiers = new Vector<>();
699:                 instanceAttributeModifiers.add(GenAttributeModifier.STATIC);
700:                 final GenJavaAttribute instanceAttribute =
701:                                 GenJavaAttribute.create(
702:                                                 "instance",
703:                                                 GenVisibility.PRIVATE,
704:                                                 symmetricRelationAccessClass,
705:                                                 instanceAttributeModifiers);
706:                 
707:                 final Vector<GenOperationModifier> getInstanceModifiers = new Vector<>();
708:                 getInstanceModifiers.add(GenOperationModifier.STATIC);
709:                 final GenJavaOperation getInstanceOperation =
710:                                 GenJavaOperation.create(
711:                                                 "getInstance",
712:                                                 GenVisibility.PUBLIC,
713:                                                 new Vector<GenParameter>(),
714:                                                 new Vector<GenException>(),
715:                                                 "if(instance == null) instance = new " + symmetricRelationAccessName + "(); return instance;",
716:                                                 symmetricRelationAccessClass,
717:                                                 getInstanceModifiers,
718:                                                 GenComment.create(""));
719:                 
720:                 final GenJavaOperation constructor =
721:                                 GenJavaOperation.createConstructor(
722:                                                 symmetricRelationAccessClass,
723:                                                 GenVisibility.PRIVATE,
724:                                                 new Vector<GenParameter>(),
725:                                                 new Vector<GenException>(),
726:                                                 "",
727:                                                 GenComment.create(""));
728:                 
729:                 symmetricRelationAccessClass.getAttributes().add(instanceAttribute);
730:                 symmetricRelationAccessClass.addOperation(getInstanceOperation);
731:                 symmetricRelationAccessClass.getConstructors().add(constructor);
732:                 
733:                 this.getGeneratorModel().addNonAstClass(symmetricRelationAccessClass);
734:                 this.getGeneratorModel().addSymmetricManager(symmetricRelationAccessClass, ownerClass);
735:                 this.getGeneratorModel().addSymmetricManager(symmetricRelationAccessClass, targetClass);
736:                 return symmetricRelationAccessClass;
737:         }
738:         
739:         public void handleOperation(final Operation o, final ClassType owner) throws TaskException {
740:                 final GenComment comment = GenComment.create(DEFAULT_OPERATION_COMMENT);
741:                 final String name = o.getName();
742:                 final Vector<GenOperationModifier> modifiers = this.getOperationModifiers(o);
743:                 final Collection<GenException> exceptions = this.getGeneratorModel().getGenExceptionsForType(o.getReturnType());
744:                 final GenType returnTyp = this.getGeneratorModel().getGenTypeForType(o.getReturnType());
745:                 final String method =
746:                                 DEFAULT_METHOD + (returnTyp.getFullyQualifiedTypeName().equals("void") ? "" : "\n\t\treturn null;");
747:                 final List<GenParameter> parameters = this.handleParameter(o.getParameters());
748:                 final GenJavaOperation result =
749:                                 GenJavaOperation.create(
750:                                                 name,
751:                                                 GenVisibility.PUBLIC,
752:                                                 parameters,
753:                                                 exceptions,
754:                                                 method,
755:                                                 returnTyp,
756:                                                 modifiers,
757:                                                 comment);
758:                 
759:                 final GenClass genOwner = this.getGeneratorModel().getClassMapping().get(owner);
760:                 genOwner.accept(new GenClassVisitor() {
761:                         
762:                         @Override
763:                         public void handle(final GenPrimitiveClass primitiveClass) {
764:                                 // primitiveClass will not be generated, adding operation is useless
765:                         }
766:                         
767:                         @Override
768:                         public void handle(final GenInterfaceClass interfaceClass) {
769:                                 
770:                                 interfaceClass.accept(new GenInterfaceClassVisitor() {
771:                                         
772:                                         @Override
773:                                         public void handle(final GenInterfaceWithClassImplClass interfaceWithImplClass) {
774:                                                 interfaceWithImplClass.addOperation(result);
775:                                                 interfaceWithImplClass.getClassRepresentation().addOperation(result);
776:                                         }
777:                                         
778:                                         @Override
779:                                         public void handle(final GenSimpleInterfaceClass simpleInterface) {
780:                                                 simpleInterface.addOperation(result);
781:                                         }
782:                                         
783:                                         @Override
784:                                         public void handle(final GenExternalInterfaceClass iface) {
785:                                                 throw new Error("Operation " + o.getName() + " cannot be added to external interface "
786:                                                                 + iface.getFullyQualifiedTypeName());
787:                                         }
788:                                 });
789:                         }
790:                         
791:                         @Override
792:                         public void handle(final GenClassClass classClass) {
793:                                 classClass.addOperation(result);
794:                         }
795:                 });
796:         }
797:         
798:         /**
799:          * Returns a {@link Vector} with {@link GenOperationModifier} for the {@link Operation} <code>o</code>.
800:          *
801:          * @param o
802:          * o
803:          * @return Vector<GenOperationModifier>
804:          */
805:         private Vector<GenOperationModifier> getOperationModifiers(final Operation o) {
806:                 final Vector<GenOperationModifier> result = new Vector<>();
807:                 final Iterator<de.fhdw.wtf.common.ast.OperationModifier> i = o.getModifiers().iterator();
808:                 while (i.hasNext()) {
809:                         final de.fhdw.wtf.common.ast.OperationModifier current = i.next();
810:                         current.accept(new OperationModifierVisitor() {
811:                                 @Override
812:                                 public boolean handle(final de.fhdw.wtf.common.ast.OperationModifier abstract1) {
813:                                         result.add(GenOperationModifier.ABSTRACT);
814:                                         return false;
815:                                 }
816:                         });
817:                 }
818:                 return result;
819:         }
820:         
821:         /**
822:          * TODO Janik / Tilmann soll das wirklich so?!
823:          *
824:          * @param o
825:          * @return
826:          * @throws TaskException
827:          */
828:         protected List<GenParameter> handleParameter(final ProductType o) throws TaskException {
829:                 final List<ProductElementType> elements = o.getElements();
830:                 final Vector<GenParameter> result = new Vector<>();
831:                 for (final ProductElementType productElement : elements) {
832:                         final GenType paramType = this.getGeneratorModel().getGenTypeForType(productElement.getType());
833:                         result.add(GenParameter.create(productElement.getName(), paramType));
834:                 }
835:                 return result;
836:         }
837:         
838:         // BEGIN PRODUCTS
839:         
840:         /**
841:          * For each element of <code>product</code> this methods adds an <code>protected</code> attribute to the
842:          * {@link GenClassClass} that represents the {@link ProductType}.
843:          *
844:          * @param product
845:          * given {@link ProductType} to add attributes for.
846:          * @param prod
847:          * The underlying {@link GenClassClass} for the product passed.
848:          * @throws TaskException
849:          * Thrown when the product or an element-Type is not referenced in the {@link GeneratorModel}.
850:          */
851:         private void handleProductAttributes(final ProductType product, final GenClassClass prod) throws TaskException {
852:                 final StringBuilder methodString = new StringBuilder();
853:                 final List<GenParameter> parameters = new Vector<>();
854:                 final Iterator<ProductElementType> i = product.getElements().iterator();
855:                 while (i.hasNext()) {
856:                         final ProductElementType element = i.next();
857:                         final Collection<GenAttributeModifier> modifiers = new Vector<>();
858:                         final GenType type = this.getGeneratorModel().getGenTypeForType(element.getType());
859:                         final GenJavaAttribute currentAttr =
860:                                         GenJavaAttribute.create(element.getName(), GenVisibility.PROTECTED, type, modifiers);
861:                         prod.getAttributes().add(currentAttr);
862:                         
863:                         final String methodStringInitialize =
864:                                         THIS_POINT + currentAttr.getName() + EQUAL_SYMBOL + currentAttr.getName() + JAVA_LINE_END;
865:                         if (methodString.toString().equals("")) {
866:                                 methodString.append(methodStringInitialize);
867:                         } else {
868:                                 methodString.append(NEW_LINE + TAB + TAB + methodStringInitialize);
869:                         }
870:                         final GenParameter parameter = GenParameter.create(currentAttr.getName(), currentAttr.getTyp());
871:                         parameters.add(parameter);
872:                 }
873:                 // constructor
874:                 final GenComment comment = GenComment.createFromPlainText(DEFAULT_PRODUCT_CONSTRUCTOR_COMMENT, true);
875:                 final GenJavaOperation constructor =
876:                                 GenJavaOperation.createConstructor(
877:                                                 prod,
878:                                                 GenVisibility.PUBLIC,
879:                                                 parameters,
880:                                                 new Vector<GenException>(),
881:                                                 methodString.toString(),
882:                                                 comment);
883:                 prod.getConstructors().add(constructor);
884:         }
885:         
886:         /**
887:          * For each element of <code>product</code> this operation adds an getter and an setter to the {@link GenClassClass}
888:          * that represents the {@link ProductType}. Furthermore this operation adds a constructor based on all attributes to
889:          * the {@link GenClassClass}.
890:          *
891:          * @param product
892:          * given {@link ProductType} to generate getters, setters and a constructor for.
893:          * @param prod
894:          * The underlying {@link GenClassClass} for the product passed.
895:          * @throws TaskException
896:          * Thrown when the product or an element-Type is not referenced in the {@link GeneratorModel}.
897:          */
898:         private void generateProductsGettersAndSetters(final ProductType product, final GenClassClass prod)
899:                         throws TaskException {
900:                 final Iterator<ProductElementType> i = product.getElements().iterator();
901:                 BigInteger currentElementCount = BigInteger.ZERO;
902:                 while (i.hasNext()) {
903:                         currentElementCount = currentElementCount.add(BigInteger.ONE);
904:                         final ProductElementType current = i.next();
905:                         final String name = current.getName();
906:                         final String capitalName = name.substring(0, 1).toUpperCase() + name.substring(1);
907:                         final Collection<GenException> exceptions = new Vector<>();
908:                         final GenType prodElementType = this.getGeneratorModel().getGenTypeForType(current.getType());
909:                         final Collection<GenOperationModifier> operationModifier = new Vector<>();
910:                         // getter
911:                         final List<GenParameter> paramsGetter = new ArrayList<>();
912:                         final String implementationGetter = "return this.p$" + currentElementCount.toString() + ";";
913:                         prod.addOperation(GenJavaOperation.create(
914:                                         "get" + capitalName,
915:                                         GenVisibility.PUBLIC,
916:                                         paramsGetter,
917:                                         exceptions,
918:                                         implementationGetter,
919:                                         prodElementType,
920:                                         operationModifier,
921:                                         GenComment.createFromPlainText("", true)));
922:                         // setter
923:                         final List<GenParameter> paramsSetter = new ArrayList<>();
924:                         final GenParameter setParam = GenParameter.create(name, prodElementType);
925:                         paramsSetter.add(setParam);
926:                         final String implementationSetter = "this.p$" + currentElementCount.toString() + " = " + name + ";";
927:                         prod.addOperation(GenJavaOperation.create(
928:                                         "set" + capitalName,
929:                                         GenVisibility.PUBLIC,
930:                                         paramsSetter,
931:                                         exceptions,
932:                                         implementationSetter,
933:                                         GenVoidType.getInstance(),
934:                                         operationModifier,
935:                                         GenComment.createFromPlainText("", false)));
936:                 }
937:         }
938:         
939:         // END PRODUCTS
940:         
941:         @Override
942:         public String toString() {
943:                 return "Operation and Attribute generation";
944:         }
945:         
946:         @Override
947:         public void beginTask() throws TaskException {
948:                 // Nothing
949:         }
950:         
951:         @Override
952:         public void finalizeTask() throws TaskException {
953:                 // Nothing
954:         }
955: }