Skip to content

Package: ConstructorSymmetricAttribute$1

ConstructorSymmetricAttribute$1

nameinstructionbranchcomplexitylinemethod
handle(GenFullParsedOperationState)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
handle(GenSimpleOperationState)
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: 9 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.util.HashSet;
4: import java.util.Iterator;
5:
6: import de.fhdw.wtf.common.ast.Attribute;
7: import de.fhdw.wtf.common.ast.ConstructorOrOperation;
8: import de.fhdw.wtf.common.ast.Group;
9: import de.fhdw.wtf.common.ast.Model;
10: import de.fhdw.wtf.common.ast.type.ClassType;
11: import de.fhdw.wtf.common.exception.walker.TaskException;
12: import de.fhdw.wtf.common.task.TaskExecutor;
13: import de.fhdw.wtf.generator.java.generatorModel.GenClass;
14: import de.fhdw.wtf.generator.java.generatorModel.GenClassClass;
15: import de.fhdw.wtf.generator.java.generatorModel.GenFullParsedOperationState;
16: import de.fhdw.wtf.generator.java.generatorModel.GenJavaOperation;
17: import de.fhdw.wtf.generator.java.generatorModel.GenSimpleOperationState;
18: import de.fhdw.wtf.generator.java.generatorModel.GenUserClass;
19: import de.fhdw.wtf.generator.java.generatorModel.GeneratorModel;
20: import de.fhdw.wtf.generator.java.visitor.GenOperationStateVisitor;
21: import de.fhdw.wtf.walker.walker.SimpleWalkerTask;
22:
23: public class ConstructorSymmetricAttribute extends SimpleWalkerTask {
24:         
25:         private final GeneratorModel generatorModel;
26:         private final HashSet<ClassType> lst = new HashSet<ClassType>();
27:         
28:         private ClassType findCorrespondingType(final Attribute a) {
29:                 final Iterator<ClassType> i = this.lst.iterator();
30:                 while (i.hasNext()) {
31:                         final ClassType current = i.next();
32:                         final String str1 = (current.getName() + "").trim().toLowerCase();
33:                         final String str2 = (a.getAttrType().getTypeString()).trim().toLowerCase();
34:                         if (str1.equals(str2)) {
35:                                 System.out.println("------> findCorrespondingType: NOT NULL");
36:                                 return current;
37:                         }
38:                 }
39:                 System.out.println("------> findCorrespondingType: IS NULL");
40:                 return null;
41:         }
42:         
43:         public ConstructorSymmetricAttribute(final Model m,
44:                         final TaskExecutor taskmanager,
45:                         final GeneratorModel generatorModel) {
46:                 super(m, taskmanager);
47:                 this.generatorModel = generatorModel;
48:         }
49:         
50:         @Override
51:         public void handleClass(final ClassType c) throws TaskException {
52:                 final Iterator<Attribute> i = c.getAttributes().iterator();
53:                 while (i.hasNext()) {
54:                         final Attribute current = i.next();
55:                         if (current.isSymmetric()) {
56:                                 if (this.lst.contains(c) == false) {
57:                                         this.lst.add(c);
58:                                 }
59:                         }
60:                 }
61:         }
62:         
63:         @Override
64:         public void handleGroup(final Group g) throws TaskException {
65:                 // Leer
66:         }
67:         
68:         @Override
69:         public void handleAttribute(final Attribute a, final ClassType owner) throws TaskException {
70:                 // Leer
71:         }
72:         
73:         @Override
74:         public void handleConstructorOrOperation(final ConstructorOrOperation coo, final ClassType owner)
75:                         throws TaskException {
76:                 // Leer
77:         }
78:         
79:         private void finalizeTaskClassTypeAttribute(final ClassType clss, final Attribute att) {
80:                 
81:                 final ClassType correspondingType = this.findCorrespondingType(att);
82:                 final GenUserClass singleton = this.generatorModel.getSymmetricManager(clss, correspondingType);
83:                 
84:                 if (singleton == null) {
85:                         System.out.println("------> Singleton is null!");
86:                         return;
87:                 }
88:                 
89:                 final String singletonName = singleton.getName();
90:                 final String singletonInstance = ".getInstance().";
91:                 final String setterName = "registerSetter" + clss.getName().getLastAddedName();
92:                 final String setterCall = singletonName + singletonInstance + setterName;
93:                 
94:                 // Aufruf in jedem Constructor von beispielsweise A$Impl
95:                 final String content =
96:                                 "\t\t" + setterCall + "(this, new " + (clss.getName() + "").replace('>', '.') + "Setter());";
97:                 
98:                 // Bsp.: Finde Klasse "A"
99:                 final GenClass genClss = this.generatorModel.getJavaClassForWTFClass(clss);
100:                 
101:                 // Bsp.: Hole Klasse "A$Impl"
102:                 final GenClassClass genClssClss = genClss.getImplementor();
103:                 
104:                 // Füge Import des Singleton-Symmetric-Managers in beispielsweise A$Impl hinzu
105:                 genClssClss.setImports(genClssClss.getImports() + System.lineSeparator() + "import "
106:                                 + singleton.getPackag().toString() + "." + singleton.getName() + ";");
107:                 
108:                 // Output zum Debugging (wenn nötig)
109:                 System.out.println("------> genClss : " + genClss.getFullyQualifiedTypeName());
110:                 System.out.println("------> genClssClss : " + genClssClss.getFullyQualifiedTypeName());
111:                 
112:                 // Durchlaufe nun jeden Constructor
113:                 final Iterator<GenJavaOperation> opIt = genClssClss.getConstructors().iterator();
114:                 while (opIt.hasNext()) {
115:                         final GenJavaOperation op = opIt.next();
116:                         op.getState().accept(new GenOperationStateVisitor() {
117:                                 
118:                                 @Override
119:                                 public void handle(final GenFullParsedOperationState s) {
120:                                         s.setMethodBody(s.getMethodBody() + System.lineSeparator() + content);
121:                                 }
122:                                 
123:                                 @Override
124:                                 public void handle(final GenSimpleOperationState s) {
125:                                         // Leer
126:                                 }
127:                         });
128:                 }
129:         }
130:         
131:         private void finalizeTaskClassType(final ClassType clss) {
132:                 final Iterator<Attribute> i2 = clss.getAttributes().iterator();
133:                 while (i2.hasNext()) {
134:                         final Attribute att = i2.next();
135:                         if (att.isSymmetric() == false) {
136:                                 continue;
137:                         }
138:                         this.finalizeTaskClassTypeAttribute(clss, att);
139:                 }
140:         }
141:         
142:         @Override
143:         public void finalizeTask() throws TaskException {
144:                 try {
145:                         final Iterator<ClassType> i1 = this.lst.iterator();
146:                         while (i1.hasNext()) {
147:                                 final ClassType clss = i1.next();
148:                                 this.finalizeTaskClassType(clss);
149:                         }
150:                 } catch (final Exception e) {
151:                         System.out.println("----------> ERROR: " + e.getMessage());
152:                         e.printStackTrace();
153:                 }
154:         }
155:         
156:         @Override
157:         public void beginTask() throws TaskException {
158:                 // Leer
159:         }
160:         
161: }