Skip to content

Package: OperationAttributeTransformer$1$1

OperationAttributeTransformer$1$1

nameinstructionbranchcomplexitylinemethod
handle(BaseType)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handle(ClassType)
M: 0 C: 52
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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