Skip to content

Package: OperationAttributeTransformer$3$2

OperationAttributeTransformer$3$2

nameinstructionbranchcomplexitylinemethod
handle(ListType)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
handle(MapType)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
handle(ProductType)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
handle(SumType)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
handle(ThrownType)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package de.fhdw.wtf.generator.transformer.transformers.classTransformer;
2:
3: import java.math.BigInteger;
4: import java.util.ArrayList;
5: import java.util.Arrays;
6: import java.util.Collection;
7: import java.util.Iterator;
8: import java.util.List;
9: import java.util.Vector;
10:
11: import de.fhdw.wtf.common.ast.Attribute;
12: import de.fhdw.wtf.common.ast.AttributeModifier;
13: import de.fhdw.wtf.common.ast.AttributeModifierFindable;
14: import de.fhdw.wtf.common.ast.AttributeModifierMutable;
15: import de.fhdw.wtf.common.ast.AttributeModifierPrior;
16: import de.fhdw.wtf.common.ast.AttributeModifierSymmetric;
17: import de.fhdw.wtf.common.ast.AttributeModifierTransient;
18: import de.fhdw.wtf.common.ast.Model;
19: import de.fhdw.wtf.common.ast.Operation;
20: import de.fhdw.wtf.common.ast.type.AtomicType;
21: import de.fhdw.wtf.common.ast.type.BaseType;
22: import de.fhdw.wtf.common.ast.type.ClassType;
23: import de.fhdw.wtf.common.ast.type.CompositeType;
24: import de.fhdw.wtf.common.ast.type.ListType;
25: import de.fhdw.wtf.common.ast.type.MapType;
26: import de.fhdw.wtf.common.ast.type.ProductElementType;
27: import de.fhdw.wtf.common.ast.type.ProductType;
28: import de.fhdw.wtf.common.ast.type.SumType;
29: import de.fhdw.wtf.common.ast.type.ThrownType;
30: import de.fhdw.wtf.common.ast.type.Type;
31: import de.fhdw.wtf.common.ast.type.TypeProxy;
32: import de.fhdw.wtf.common.ast.visitor.AtomicTypeVisitorException;
33: import de.fhdw.wtf.common.ast.visitor.AttributModifierVisitor;
34: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorException;
35: import de.fhdw.wtf.common.ast.visitor.OperationModifierVisitor;
36: import de.fhdw.wtf.common.ast.visitor.TypeVisitorException;
37: import de.fhdw.wtf.common.exception.walker.CyclicDependencyException;
38: import de.fhdw.wtf.common.exception.walker.TaskException;
39: import de.fhdw.wtf.common.task.TaskExecutor;
40: import de.fhdw.wtf.generator.java.generatorModel.GenAttributeModifier;
41: import de.fhdw.wtf.generator.java.generatorModel.GenClass;
42: import de.fhdw.wtf.generator.java.generatorModel.GenClassClass;
43: import de.fhdw.wtf.generator.java.generatorModel.GenClassModifier;
44: import de.fhdw.wtf.generator.java.generatorModel.GenComment;
45: import de.fhdw.wtf.generator.java.generatorModel.GenException;
46: import de.fhdw.wtf.generator.java.generatorModel.GenExternalClassClass;
47: import de.fhdw.wtf.generator.java.generatorModel.GenExternalInterfaceClass;
48: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceClass;
49: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceWithClassImplClass;
50: import de.fhdw.wtf.generator.java.generatorModel.GenJavaAttribute;
51: import de.fhdw.wtf.generator.java.generatorModel.GenJavaOperation;
52: import de.fhdw.wtf.generator.java.generatorModel.GenJavaUtilCollection;
53: import de.fhdw.wtf.generator.java.generatorModel.GenOperationModifier;
54: import de.fhdw.wtf.generator.java.generatorModel.GenPackage;
55: import de.fhdw.wtf.generator.java.generatorModel.GenParameter;
56: import de.fhdw.wtf.generator.java.generatorModel.GenPrimitiveClass;
57: import de.fhdw.wtf.generator.java.generatorModel.GenSimpleInterfaceClass;
58: import de.fhdw.wtf.generator.java.generatorModel.GenType;
59: import de.fhdw.wtf.generator.java.generatorModel.GenUnqualifiedPackage;
60: import de.fhdw.wtf.generator.java.generatorModel.GenUserClass;
61: import de.fhdw.wtf.generator.java.generatorModel.GenVisibility;
62: import de.fhdw.wtf.generator.java.generatorModel.GenVoidType;
63: import de.fhdw.wtf.generator.java.generatorModel.GeneratorModel;
64: import de.fhdw.wtf.generator.java.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 =
432:                                 this.getSymmetricRelationAccessClassOfClass(owner, targetClass, attribute);
433:                 final String targetClassName = targetClass.getName().getLastAddedName().toString().toLowerCase();
434:                 final String inverseGetterIdentifier = modifier.getIdentifierToken().getIdentifier();
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:                 // Bei 1 Assoziation set erstellen
453:                 if (!isStarAssociation) {
454:                         final Vector<GenParameter> parameters = new Vector<>();
455:                         final GenParameter ownerParameter =
456:                                         GenParameter.create(ownerString, this.getGeneratorModel().getJavaClassForWTFClass(owner));
457:                         final GenParameter targetParameter =
458:                                         GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
459:                         parameters.add(ownerParameter);
460:                         parameters.add(targetParameter);
461:                         final String methodContent = "//TODO methodContent wird noch implementiert";
462:                         // TODO method
463:                         final GenJavaOperation setMethod =
464:                                         GenJavaOperation.create(
465:                                                         "set" + attribute.getName(),
466:                                                         GenVisibility.PUBLIC,
467:                                                         parameters,
468:                                                         new Vector<GenException>(),
469:                                                         methodContent,
470:                                                         GenVoidType.getInstance(),
471:                                                         new Vector<GenOperationModifier>(),
472:                                                         GenComment.create(""));
473:                         symmetricRelationAccessClass.addOperation(setMethod);
474:                         
475:                 } else {
476:                         // TODO hier muss noch add und remove für * Assoziation implementiert werden
477:                         final Vector<GenParameter> parameters1 = new Vector<>();
478:                         final GenParameter ownerParameter1 =
479:                                         GenParameter.create(ownerString, this.getGeneratorModel().getJavaClassForWTFClass(owner));
480:                         final GenParameter targetParameter1 =
481:                                         GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
482:                         parameters1.add(ownerParameter1);
483:                         parameters1.add(targetParameter1);
484:                         final String methodContentAdd = "//TODO methodContent wird noch implementiert.";
485:                         // TODO method
486:                         final GenJavaOperation addMethod =
487:                                         GenJavaOperation.create(
488:                                                         "add" + attribute.getName(),
489:                                                         GenVisibility.PUBLIC,
490:                                                         parameters1,
491:                                                         new Vector<GenException>(),
492:                                                         methodContentAdd,
493:                                                         GenVoidType.getInstance(),
494:                                                         new Vector<GenOperationModifier>(),
495:                                                         GenComment.create(""));
496:                         
497:                         final Vector<GenParameter> parameters2 = new Vector<>();
498:                         final GenParameter ownerParameter2 =
499:                                         GenParameter.create(ownerString, this.getGeneratorModel().getJavaClassForWTFClass(owner));
500:                         final GenParameter targetParameter2 =
501:                                         GenParameter.create(targetString, this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
502:                         parameters2.add(ownerParameter2);
503:                         parameters2.add(targetParameter2);
504:                         final String methodContentRemove = "//methodContent wird noch implementiert.";
505:                         // TODO method.
506:                         final GenJavaOperation removeMethod =
507:                                         GenJavaOperation.create(
508:                                                         "remove" + attribute.getName(),
509:                                                         GenVisibility.PUBLIC,
510:                                                         parameters2,
511:                                                         new Vector<GenException>(),
512:                                                         methodContentRemove,
513:                                                         GenVoidType.getInstance(),
514:                                                         new Vector<GenOperationModifier>(),
515:                                                         GenComment.create(""));
516:                         symmetricRelationAccessClass.addOperation(removeMethod);
517:                         symmetricRelationAccessClass.addOperation(addMethod);
518:                 }
519:                 
520:                 // TODO: Use constants/config-file instead of literals
521:                 final String associationName =
522:                                 "generated.model" + "." + owner.getName().toString().replace('>', '.') + "." + attribute.getName();
523:                 final String methodContent =
524:                                 "return this." + superMethodName + "(" + targetClassName + ", \"" + associationName + "\");";
525:                 
526:                 final GenJavaOperation inverseGetter =
527:                                 GenJavaOperation.create(
528:                                                 inverseGetterIdentifier,
529:                                                 GenVisibility.PUBLIC,
530:                                                 new Vector<GenParameter>(),
531:                                                 new Vector<GenException>(),
532:                                                 methodContent,
533:                                                 returnType,
534:                                                 new Vector<GenOperationModifier>(),
535:                                                 GenComment.create(""));
536:                 
537:                 // final GenJavaOperation
538:                 
539:                 final GenClass genClass = this.getGeneratorModel().getJavaClassForWTFClass(targetClass);
540:                 final GenClassClass genClassClass = genClass.getImplementor();
541:                 final GenParameter newParam = GenParameter.create(targetClassName, genClassClass);
542:                 inverseGetter.getParameters().add(newParam);
543:                 
544:                 symmetricRelationAccessClass.addOperation(inverseGetter);
545:         }
546:         
547:         private Vector<ClassType> getTargetClasses(final Type attrTypeOccurance) throws GenTypeNotReferencedException {
548:                 final Vector<ClassType> targetTypes = new Vector<>();
549:                 final Type attributeType = UtilTransformer.getTypeProxyFreePrototype(attrTypeOccurance);
550:                 attributeType.accept(new TypeVisitorException<GenTypeNotReferencedException>() {
551:                         
552:                         @Override
553:                         public void handle(final AtomicType s) throws GenTypeNotReferencedException {
554:                                 s.accept(new AtomicTypeVisitorException<GenTypeNotReferencedException>() {
555:                                         
556:                                         @Override
557:                                         public void handle(final BaseType baseType) throws GenTypeNotReferencedException {
558:                                                 // NOthing
559:                                         }
560:                                         
561:                                         @Override
562:                                         public void handle(final ClassType clazz) throws GenTypeNotReferencedException {
563:                                                 targetTypes.add(clazz);
564:                                         }
565:                                 });
566:                         }
567:                         
568:                         @Override
569:                         public void handle(final CompositeType c) throws GenTypeNotReferencedException {
570:                                 
571:                                 c.accept(new CompositeTypeVisitorException<GenTypeNotReferencedException>() {
572:                                         
573:                                         @Override
574:                                         public void handle(final MapType map) throws GenTypeNotReferencedException {
575:                                                 targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(map.getOf()));
576:                                                 
577:                                         }
578:                                         
579:                                         @Override
580:                                         public void handle(final ListType list) throws GenTypeNotReferencedException {
581:                                                 targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(list.getOf()));
582:                                                 
583:                                         }
584:                                         
585:                                         @Override
586:                                         public void handle(final ProductType product) throws GenTypeNotReferencedException {
587:•                                                for (final ProductElementType factorTypeOccurance : product.getElements()) {
588:                                                         targetTypes.addAll(OperationAttributeTransformer.this.getTargetClasses(factorTypeOccurance
589:                                                                         .getType()));
590:                                                 }
591:                                         }
592:                                         
593:                                         @Override
594:                                         public void handle(final SumType sum) throws GenTypeNotReferencedException {
595:•                                                for (final Type summandTypeOccurance : sum.getElements()) {
596:                                                         targetTypes.addAll(OperationAttributeTransformer.this
597:                                                                         .getTargetClasses(summandTypeOccurance));
598:                                                 }
599:                                         }
600:                                         
601:                                         @Override
602:                                         public void handle(final ThrownType thrownType) throws GenTypeNotReferencedException {
603:                                                 // nothing to do
604:                                         }
605:                                 });
606:                         }
607:                         
608:                         @Override
609:                         public void handle(final TypeProxy s) throws GenTypeNotReferencedException {
610:                                 // nothing to do
611:                         }
612:                 });
613:                 return targetTypes;
614:         }
615:         
616:         // TODO
617:         private GenClass getSymmetricRelationAccessClassOfClass(final ClassType ownerClass,
618:                         final ClassType targetClass,
619:                         final Attribute attribute) {
620:                 // Bsp.: A
621:                 final String ownerClassName = ownerClass.getName().getLastAddedName() + "";
622:                 
623:                 // Bsp.: B
624:                 final String targetClassName = targetClass.getName().getLastAddedName() + "";
625:                 
626:                 // Sortiere Klassen Lexikographisch
627:                 final String[] strings = { ownerClassName, targetClassName };
628:                 Arrays.sort(strings);
629:                 
630:                 // Name des Singletons der für die Relation verantwortlich ist, Bsp.: A_BSymmetricRelationAccess
631:                 final String symmetricRelationAccessName = strings[0] + "_" + strings[1] + "SymmetricRelationAccess";
632:                 
633:                 for (final GenClass knownClass : this.getGeneratorModel().getClasses()) {
634:                         if (knownClass.getName().equals(symmetricRelationAccessName)) {
635:                                 return knownClass;
636:                         }
637:                 }
638:                 
639:                 GenPackage superClassPackage = GenUnqualifiedPackage.create("de");
640:                 superClassPackage = superClassPackage.addName("fhdw");
641:                 superClassPackage = superClassPackage.addName("wtf");
642:                 superClassPackage = superClassPackage.addName("context");
643:                 superClassPackage = superClassPackage.addName("model");
644:                 
645:                 final GenUserClass symmetricRelationAccessSuperClass =
646:                                 GenUserClass.create(
647:                                                 "SymmetricRelationAccess",
648:                                                 new Vector<GenJavaOperation>(),
649:                                                 new Vector<GenInterfaceClass>(),
650:                                                 new Vector<GenJavaAttribute>(),
651:                                                 new Vector<GenClassModifier>(),
652:                                                 new Vector<GenJavaOperation>(),
653:                                                 null,
654:                                                 superClassPackage,
655:                                                 GenComment.createFromPlainText("", false),
656:                                                 "");
657:                 
658:                 GenPackage classPackage = GenUnqualifiedPackage.create("generated");
659:                 classPackage = classPackage.addName("symmetry");
660:                 
661:                 final GenUserClass symmetricRelationAccessClass =
662:                                 GenUserClass.create(
663:                                                 symmetricRelationAccessName,
664:                                                 new Vector<GenJavaOperation>(),
665:                                                 new Vector<GenInterfaceClass>(),
666:                                                 new Vector<GenJavaAttribute>(),
667:                                                 new Vector<GenClassModifier>(),
668:                                                 new Vector<GenJavaOperation>(),
669:                                                 symmetricRelationAccessSuperClass,
670:                                                 classPackage,
671:                                                 GenComment.createFromPlainText("", false),
672:                                                 "");
673:                 
674:                 final Vector<GenAttributeModifier> instanceAttributeModifiers = new Vector<>();
675:                 instanceAttributeModifiers.add(GenAttributeModifier.STATIC);
676:                 final GenJavaAttribute instanceAttribute =
677:                                 GenJavaAttribute.create(
678:                                                 "instance",
679:                                                 GenVisibility.PRIVATE,
680:                                                 symmetricRelationAccessClass,
681:                                                 instanceAttributeModifiers);
682:                 
683:                 final Vector<GenOperationModifier> getInstanceModifiers = new Vector<>();
684:                 getInstanceModifiers.add(GenOperationModifier.STATIC);
685:                 final GenJavaOperation getInstanceOperation =
686:                                 GenJavaOperation.create(
687:                                                 "getInstance",
688:                                                 GenVisibility.PUBLIC,
689:                                                 new Vector<GenParameter>(),
690:                                                 new Vector<GenException>(),
691:                                                 "if(instance == null) instance = new " + symmetricRelationAccessName + "(); return instance;",
692:                                                 symmetricRelationAccessClass,
693:                                                 getInstanceModifiers,
694:                                                 GenComment.create(""));
695:                 
696:                 // Wenn Assoziation reflexiv, dann nur eine HashMap & registerSetter-Methode, ansonsten zwei.
697:                 final Vector<String> mapLst = new Vector<>();
698:                 if (ownerClass.getName().equals(targetClass.getName())) {
699:                         final String newMap = this.generateHashMap(targetClass, attribute, symmetricRelationAccessClass);
700:                         this.registerSetter(ownerClass, targetClass, attribute, symmetricRelationAccessClass);
701:                         mapLst.add(newMap);
702:                 } else {
703:                         final String newMap1 = this.generateHashMap(targetClass, attribute, symmetricRelationAccessClass);
704:                         this.registerSetter(ownerClass, targetClass, attribute, symmetricRelationAccessClass);
705:                         
706:                         final String newMap2 = this.generateHashMap(ownerClass, attribute, symmetricRelationAccessClass);
707:                         this.registerSetter(targetClass, ownerClass, attribute, symmetricRelationAccessClass);
708:                         
709:                         mapLst.add(newMap1);
710:                         mapLst.add(newMap2);
711:                 }
712:                 
713:                 // Baue den Inhalt des Konstruktors zusammen
714:                 String constructorContent = "";
715:                 for (final String currentMapName : mapLst) {
716:                         constructorContent =
717:                                         constructorContent + "\t\tthis." + currentMapName + " = new HashMap<>();" + System.lineSeparator();
718:                 }
719:                 
720:                 // Konstruktor erzeugen
721:                 final GenJavaOperation constructor =
722:                                 GenJavaOperation.createConstructor(
723:                                                 symmetricRelationAccessClass,
724:                                                 GenVisibility.PRIVATE,
725:                                                 new Vector<GenParameter>(),
726:                                                 new Vector<GenException>(),
727:                                                 constructorContent,
728:                                                 GenComment.create(""));
729:                 
730:                 symmetricRelationAccessClass.getAttributes().add(instanceAttribute);
731:                 symmetricRelationAccessClass.addOperation(getInstanceOperation);
732:                 symmetricRelationAccessClass.getConstructors().add(constructor);
733:                 
734:                 this.getGeneratorModel().addNonAstClass(symmetricRelationAccessClass);
735:                 this.getGeneratorModel().addSymmetricManager(symmetricRelationAccessClass, ownerClass);
736:                 this.getGeneratorModel().addSymmetricManager(symmetricRelationAccessClass, targetClass);
737:                 return symmetricRelationAccessClass;
738:         }
739:         
740:         public void handleOperation(final Operation o, final ClassType owner) throws TaskException {
741:                 final GenComment comment = GenComment.create(DEFAULT_OPERATION_COMMENT);
742:                 final String name = o.getName();
743:                 final Vector<GenOperationModifier> modifiers = this.getOperationModifiers(o);
744:                 final Collection<GenException> exceptions = this.getGeneratorModel().getGenExceptionsForType(o.getReturnType());
745:                 final GenType returnTyp = this.getGeneratorModel().getGenTypeForType(o.getReturnType());
746:                 final String method =
747:                                 DEFAULT_METHOD + (returnTyp.getFullyQualifiedTypeName().equals("void") ? "" : "\n\t\treturn null;");
748:                 final List<GenParameter> parameters = this.handleParameter(o.getParameters());
749:                 final GenJavaOperation result =
750:                                 GenJavaOperation.create(
751:                                                 name,
752:                                                 GenVisibility.PUBLIC,
753:                                                 parameters,
754:                                                 exceptions,
755:                                                 method,
756:                                                 returnTyp,
757:                                                 modifiers,
758:                                                 comment);
759:                 
760:                 final GenClass genOwner = this.getGeneratorModel().getClassMapping().get(owner);
761:                 genOwner.accept(new GenClassVisitor() {
762:                         
763:                         @Override
764:                         public void handle(final GenPrimitiveClass primitiveClass) {
765:                                 // primitiveClass will not be generated, adding operation is useless
766:                         }
767:                         
768:                         @Override
769:                         public void handle(final GenInterfaceClass interfaceClass) {
770:                                 
771:                                 interfaceClass.accept(new GenInterfaceClassVisitor() {
772:                                         
773:                                         @Override
774:                                         public void handle(final GenInterfaceWithClassImplClass interfaceWithImplClass) {
775:                                                 interfaceWithImplClass.addOperation(result);
776:                                                 interfaceWithImplClass.getClassRepresentation().addOperation(result);
777:                                         }
778:                                         
779:                                         @Override
780:                                         public void handle(final GenSimpleInterfaceClass simpleInterface) {
781:                                                 simpleInterface.addOperation(result);
782:                                         }
783:                                         
784:                                         @Override
785:                                         public void handle(final GenExternalInterfaceClass iface) {
786:                                                 throw new Error("Operation " + o.getName() + " cannot be added to external interface "
787:                                                                 + iface.getFullyQualifiedTypeName());
788:                                         }
789:                                 });
790:                         }
791:                         
792:                         @Override
793:                         public void handle(final GenClassClass classClass) {
794:                                 classClass.addOperation(result);
795:                         }
796:                 });
797:         }
798:         
799:         /**
800:          * this method generates a HashMap in the symmetricRelationAccessClass.
801:          *
802:          * @param targetClass
803:          * targetClass
804:          * @param attribute
805:          * attribute
806:          * @param symmetricRelationAccessClass
807:          * Singelton
808:          * @return the name of the new HashMap
809:          */
810:         private String generateHashMap(final ClassType targetClass,
811:                         final Attribute attribute,
812:                         final GenClass symmetricRelationAccessClass) {
813:                 // HashMaps generieren
814:                 final String mapName = "map" + targetClass.getName().getLastAddedName() + "";
815:                 final GenParameter targetParameter00 =
816:                                 GenParameter.create("", this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
817:                 final GenJavaAttribute hashMap =
818:                                 GenJavaAttribute.create(
819:                                                 mapName,
820:                                                 GenVisibility.PRIVATE,
821:                                                 Generic.create("java.util.HashMap<" + targetParameter00 + ", Setter>", null),
822:                                                 new Vector<GenAttributeModifier>());
823:                 
824:                 symmetricRelationAccessClass.getImplementor().getAttributes().add(hashMap);
825:                 return mapName;
826:         }
827:         
828:         /**
829:          * this method generates a registerSetter-Method in the symmetricRelationAccessClass.
830:          *
831:          * @param ownerClass
832:          * ownerClass
833:          * @param targetClass
834:          * targetClass
835:          * @param attribute
836:          * attribute
837:          * @param symmetricRelationAccessClass
838:          * Singelton
839:          */
840:         private void registerSetter(final ClassType ownerClass,
841:                         final ClassType targetClass,
842:                         final Attribute attribute,
843:                         final GenClass symmetricRelationAccessClass) {
844:                 // registerSetter
845:                 final String ownerClassName = ownerClass.getName().getLastAddedName() + "";
846:                 final Vector<GenParameter> registerSetterParameters = new Vector<>();
847:                 final GenParameter setterParameter = GenParameter.create("setter", Generic.create("Setter", null));
848:                 final GenParameter parameter =
849:                                 GenParameter.create("obj", this.getGeneratorModel().getJavaClassForWTFClass(targetClass));
850:                 registerSetterParameters.add(parameter);
851:                 registerSetterParameters.add(setterParameter);
852:                 final GenJavaOperation registerSetterMethod =
853:                                 GenJavaOperation.create(
854:                                                 "registerSetter" + ownerClassName,
855:                                                 GenVisibility.PUBLIC,
856:                                                 registerSetterParameters,
857:                                                 new Vector<GenException>(),
858:                                                 "this.map" + ownerClassName + ".put(obj, setter);",
859:                                                 GenVoidType.getInstance(),
860:                                                 new Vector<GenOperationModifier>(),
861:                                                 GenComment.create(""));
862:                 
863:                 symmetricRelationAccessClass.addOperation(registerSetterMethod);
864:         }
865:         
866:         /**
867:          * Returns a {@link Vector} with {@link GenOperationModifier} for the {@link Operation} <code>o</code>.
868:          *
869:          * @param o
870:          * o
871:          * @return Vector<GenOperationModifier>
872:          */
873:         private Vector<GenOperationModifier> getOperationModifiers(final Operation o) {
874:                 final Vector<GenOperationModifier> result = new Vector<>();
875:                 final Iterator<de.fhdw.wtf.common.ast.OperationModifier> i = o.getModifiers().iterator();
876:                 while (i.hasNext()) {
877:                         final de.fhdw.wtf.common.ast.OperationModifier current = i.next();
878:                         current.accept(new OperationModifierVisitor() {
879:                                 @Override
880:                                 public boolean handle(final de.fhdw.wtf.common.ast.OperationModifier abstract1) {
881:                                         result.add(GenOperationModifier.ABSTRACT);
882:                                         return false;
883:                                 }
884:                         });
885:                 }
886:                 return result;
887:         }
888:         
889:         /**
890:          * TODO Janik / Tilmann soll das wirklich so?!
891:          *
892:          * @param o
893:          * @return
894:          * @throws TaskException
895:          */
896:         protected List<GenParameter> handleParameter(final ProductType o) throws TaskException {
897:                 final List<ProductElementType> elements = o.getElements();
898:                 final Vector<GenParameter> result = new Vector<>();
899:                 for (final ProductElementType productElement : elements) {
900:                         final GenType paramType = this.getGeneratorModel().getGenTypeForType(productElement.getType());
901:                         result.add(GenParameter.create(productElement.getName(), paramType));
902:                 }
903:                 return result;
904:         }
905:         
906:         // BEGIN PRODUCTS
907:         
908:         /**
909:          * For each element of <code>product</code> this methods adds an <code>protected</code> attribute to the
910:          * {@link GenClassClass} that represents the {@link ProductType}.
911:          *
912:          * @param product
913:          * given {@link ProductType} to add attributes for.
914:          * @param prod
915:          * The underlying {@link GenClassClass} for the product passed.
916:          * @throws TaskException
917:          * Thrown when the product or an element-Type is not referenced in the {@link GeneratorModel}.
918:          */
919:         private void handleProductAttributes(final ProductType product, final GenClassClass prod) throws TaskException {
920:                 final StringBuilder methodString = new StringBuilder();
921:                 final List<GenParameter> parameters = new Vector<>();
922:                 final Iterator<ProductElementType> i = product.getElements().iterator();
923:                 while (i.hasNext()) {
924:                         final ProductElementType element = i.next();
925:                         final Collection<GenAttributeModifier> modifiers = new Vector<>();
926:                         final GenType type = this.getGeneratorModel().getGenTypeForType(element.getType());
927:                         final GenJavaAttribute currentAttr =
928:                                         GenJavaAttribute.create(element.getName(), GenVisibility.PROTECTED, type, modifiers);
929:                         prod.getAttributes().add(currentAttr);
930:                         
931:                         final String methodStringInitialize =
932:                                         THIS_POINT + currentAttr.getName() + EQUAL_SYMBOL + currentAttr.getName() + JAVA_LINE_END;
933:                         if (methodString.toString().equals("")) {
934:                                 methodString.append(methodStringInitialize);
935:                         } else {
936:                                 methodString.append(NEW_LINE + TAB + TAB + methodStringInitialize);
937:                         }
938:                         final GenParameter parameter = GenParameter.create(currentAttr.getName(), currentAttr.getTyp());
939:                         parameters.add(parameter);
940:                 }
941:                 // constructor
942:                 final GenComment comment = GenComment.createFromPlainText(DEFAULT_PRODUCT_CONSTRUCTOR_COMMENT, true);
943:                 final GenJavaOperation constructor =
944:                                 GenJavaOperation.createConstructor(
945:                                                 prod,
946:                                                 GenVisibility.PUBLIC,
947:                                                 parameters,
948:                                                 new Vector<GenException>(),
949:                                                 methodString.toString(),
950:                                                 comment);
951:                 prod.getConstructors().add(constructor);
952:         }
953:         
954:         /**
955:          * For each element of <code>product</code> this operation adds an getter and an setter to the {@link GenClassClass}
956:          * that represents the {@link ProductType}. Furthermore this operation adds a constructor based on all attributes to
957:          * the {@link GenClassClass}.
958:          *
959:          * @param product
960:          * given {@link ProductType} to generate getters, setters and a constructor for.
961:          * @param prod
962:          * The underlying {@link GenClassClass} for the product passed.
963:          * @throws TaskException
964:          * Thrown when the product or an element-Type is not referenced in the {@link GeneratorModel}.
965:          */
966:         private void generateProductsGettersAndSetters(final ProductType product, final GenClassClass prod)
967:                         throws TaskException {
968:                 final Iterator<ProductElementType> i = product.getElements().iterator();
969:                 BigInteger currentElementCount = BigInteger.ZERO;
970:                 while (i.hasNext()) {
971:                         currentElementCount = currentElementCount.add(BigInteger.ONE);
972:                         final ProductElementType current = i.next();
973:                         final String name = current.getName();
974:                         final String capitalName = name.substring(0, 1).toUpperCase() + name.substring(1);
975:                         final Collection<GenException> exceptions = new Vector<>();
976:                         final GenType prodElementType = this.getGeneratorModel().getGenTypeForType(current.getType());
977:                         final Collection<GenOperationModifier> operationModifier = new Vector<>();
978:                         // getter
979:                         final List<GenParameter> paramsGetter = new ArrayList<>();
980:                         final String implementationGetter = "return this.p$" + currentElementCount.toString() + ";";
981:                         prod.addOperation(GenJavaOperation.create(
982:                                         "get" + capitalName,
983:                                         GenVisibility.PUBLIC,
984:                                         paramsGetter,
985:                                         exceptions,
986:                                         implementationGetter,
987:                                         prodElementType,
988:                                         operationModifier,
989:                                         GenComment.createFromPlainText("", true)));
990:                         // setter
991:                         final List<GenParameter> paramsSetter = new ArrayList<>();
992:                         final GenParameter setParam = GenParameter.create(name, prodElementType);
993:                         paramsSetter.add(setParam);
994:                         final String implementationSetter = "this.p$" + currentElementCount.toString() + " = " + name + ";";
995:                         prod.addOperation(GenJavaOperation.create(
996:                                         "set" + capitalName,
997:                                         GenVisibility.PUBLIC,
998:                                         paramsSetter,
999:                                         exceptions,
1000:                                         implementationSetter,
1001:                                         GenVoidType.getInstance(),
1002:                                         operationModifier,
1003:                                         GenComment.createFromPlainText("", false)));
1004:                 }
1005:         }
1006:         
1007:         // END PRODUCTS
1008:         
1009:         @Override
1010:         public String toString() {
1011:                 return "Operation and Attribute generation";
1012:         }
1013:         
1014:         @Override
1015:         public void beginTask() throws TaskException {
1016:                 // Nothing
1017:         }
1018:         
1019:         @Override
1020:         public void finalizeTask() throws TaskException {
1021:                 // Nothing
1022:         }
1023: }