Skip to content

Package: ConstructorSymmetricAttribute

ConstructorSymmetricAttribute

nameinstructionbranchcomplexitylinemethod
ConstructorSymmetricAttribute(Model, TaskExecutor, GeneratorModel)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
beginTask()
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%
finalizeTask()
M: 179 C: 9
5%
M: 9 C: 1
10%
M: 5 C: 1
17%
M: 32 C: 4
11%
M: 0 C: 1
100%
findCorrespondingType(Attribute)
M: 44 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getSetterName(ClassType, ClassType, ClassType)
M: 67 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
handleAttribute(Attribute, ClassType)
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%
handleClass(ClassType)
M: 10 C: 16
62%
M: 3 C: 3
50%
M: 2 C: 2
50%
M: 2 C: 6
75%
M: 0 C: 1
100%
handleConstructorOrOperation(ConstructorOrOperation, ClassType)
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%
handleGroup(Group)
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%

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:         @Override
93:         public void finalizeTask() throws TaskException {
94:                 try {
95:                         final Iterator<ClassType> i1 = this.lst.iterator();
96:•                        while (i1.hasNext()) {
97:                                 final ClassType clss = i1.next();
98:                                 System.out.println("------> Klasse: " + clss.getName());
99:                                 final Iterator<Attribute> i2 = clss.getAttributes().iterator();
100:•                                while (i2.hasNext()) {
101:                                         final Attribute att = i2.next();
102:•                                        if (att.isSymmetric() == false) {
103:                                                 continue;
104:                                         }
105:                                         
106:                                         final ClassType correspondingType = this.findCorrespondingType(att);
107:                                         final GenUserClass singleton = this.generatorModel.getSymmetricManager(clss, correspondingType);
108:                                         
109:•                                        if (singleton == null) {
110:                                                 System.out.println("------> Singleton is null!");
111:                                                 return;
112:                                         }
113:                                         
114:                                         final String singletonName = singleton.getName();
115:                                         final String singletonInstance = ".getInstance().";
116:                                         final String setterName = this.getSetterName(clss, correspondingType, clss);
117:                                         final String setterCall = singletonName + singletonInstance + setterName;
118:                                         
119:                                         // Aufruf in jedem Constructor von beispielsweise A$Impl
120:                                         final String content =
121:                                                         "\t\t" + setterCall + "(this, new " + (clss.getName() + "").replace('>', '.')
122:                                                                         + "Setter());";
123:                                         
124:                                         // Bsp.: Finde Klasse "A"
125:                                         final GenClass genClss = this.generatorModel.getJavaClassForWTFClass(clss);
126:                                         
127:                                         // Bsp.: Hole Klasse "A$Impl"
128:                                         final GenClassClass genClssClss = genClss.getImplementor();
129:                                         
130:                                         // Füge Import des Singleton-Symmetric-Managers in beispielsweise A$Impl hinzu
131:                                         genClssClss.setImports(genClssClss.getImports() + System.lineSeparator() + "import "
132:                                                         + singleton.getPackag().toString() + ".*;");
133:                                         
134:                                         // Output zum Debugging (wenn nötig)
135:                                         System.out.println("------> genClss : " + genClss.getFullyQualifiedTypeName());
136:                                         System.out.println("------> genClssClss : " + genClssClss.getFullyQualifiedTypeName());
137:                                         
138:                                         // Durchlaufe nun jeden Constructor
139:                                         final Iterator<GenJavaOperation> opIt = genClssClss.getConstructors().iterator();
140:•                                        while (opIt.hasNext()) {
141:                                                 final GenJavaOperation op = opIt.next();
142:                                                 op.getState().accept(new GenOperationStateVisitor() {
143:                                                         
144:                                                         @Override
145:                                                         public void handle(final GenFullParsedOperationState s) {
146:                                                                 s.setMethodBody(s.getMethodBody() + System.lineSeparator() + content);
147:                                                         }
148:                                                         
149:                                                         @Override
150:                                                         public void handle(final GenSimpleOperationState s) {
151:                                                                 // Leer
152:                                                         }
153:                                                 });
154:                                         }
155:                                 }
156:                         }
157:                 } catch (final Exception e) {
158:                         System.out.println("----------> ERROR: " + e.getMessage());
159:                         e.printStackTrace();
160:                 }
161:         }
162:         
163:         @Override
164:         public void beginTask() throws TaskException {
165:                 // Leer
166:         }
167:         
168: }