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