Skip to content

Package: UtilTransformer$1$1

UtilTransformer$1$1

nameinstructionbranchcomplexitylinemethod
handle(ListType)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handle(MapType)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handle(ProductType)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handle(SumType)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handle(ThrownType)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.wtf.generator.transformer.transformers.classTransformer;
2:
3: import de.fhdw.wtf.common.ast.Name;
4: import de.fhdw.wtf.common.ast.QualifiedName;
5: import de.fhdw.wtf.common.ast.UnqualifiedName;
6: import de.fhdw.wtf.common.ast.type.AtomicType;
7: import de.fhdw.wtf.common.ast.type.ByNameState;
8: import de.fhdw.wtf.common.ast.type.ByReferenceState;
9: import de.fhdw.wtf.common.ast.type.CompositeType;
10: import de.fhdw.wtf.common.ast.type.InvalidState;
11: import de.fhdw.wtf.common.ast.type.ListType;
12: import de.fhdw.wtf.common.ast.type.MapType;
13: import de.fhdw.wtf.common.ast.type.ProductType;
14: import de.fhdw.wtf.common.ast.type.SumType;
15: import de.fhdw.wtf.common.ast.type.ThrownType;
16: import de.fhdw.wtf.common.ast.type.Type;
17: import de.fhdw.wtf.common.ast.type.TypeProxy;
18: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorReturn;
19: import de.fhdw.wtf.common.ast.visitor.NameReturnVisitor;
20: import de.fhdw.wtf.common.ast.visitor.TypeProxyStateVisitorReturn;
21: import de.fhdw.wtf.common.ast.visitor.TypeVisitorReturn;
22: import de.fhdw.wtf.generator.java.generatorModel.GenPackage;
23: import de.fhdw.wtf.generator.java.generatorModel.GenQualifiedPackage;
24: import de.fhdw.wtf.generator.java.generatorModel.GenUnqualifiedPackage;
25: import de.fhdw.wtf.generator.java.generatorModel.GeneratorModel;
26:
27: public final class UtilTransformer {
28:         
29:         private final GeneratorModel generatorModel;
30:         
31:         private static final String GEN_PACKAGE_PREFIX_FIRST = "generated";
32:         private static final String GEN_PACKAGE_PREFIX_SECOND = "model";
33:         
34:         private UtilTransformer(final GeneratorModel generatorModel) {
35:                 this.generatorModel = generatorModel;
36:         }
37:         
38:         public static UtilTransformer create(final GeneratorModel generatorModel) {
39:                 return new UtilTransformer(generatorModel);
40:         }
41:         
42:         /**
43:          * Returns an AST-Type that is no {@link TypeProxy} and represents a Prototype. If the given type is a TypeProxy,
44:          * this method extracts the contained Type. Furthermore if the type is a {@link CompositeType} it gets the Prototype
45:          * of the given type.
46:          *
47:          * @param type
48:          * The Type to get an Prototype-representation for.
49:          * @return {@link Type}
50:          */
51:         public static Type getTypeProxyFreePrototype(final Type type) {
52:                 final Type noTypeProxyType = UtilTransformer.getTypeOfPossibleTypeProxy(type);
53:                 return UtilTransformer.getPrototype(noTypeProxyType);
54:         }
55:         
56:         /**
57:          * Returns the Prototype for a given {@link Type}.
58:          *
59:          * @param type
60:          * The Type to get the Prototype for.
61:          * @return {@link Type}
62:          */
63:         private static Type getPrototype(final Type type) {
64:                 return type.accept(new TypeVisitorReturn<Type>() {
65:                         
66:                         @Override
67:                         public Type handle(final AtomicType atomicType) {
68:                                 return atomicType;
69:                         }
70:                         
71:                         @Override
72:                         public Type handle(final CompositeType compositeType) {
73:                                 return compositeType.accept(new CompositeTypeVisitorReturn<Type>() {
74:                                         
75:                                         @Override
76:                                         public Type handle(final ListType list) {
77:                                                 return list.getPrototype();
78:                                         }
79:                                         
80:                                         @Override
81:                                         public Type handle(final MapType map) {
82:                                                 return map.getPrototype();
83:                                         }
84:                                         
85:                                         @Override
86:                                         public Type handle(final ProductType product) {
87:                                                 return product.getPrototype();
88:                                         }
89:                                         
90:                                         @Override
91:                                         public Type handle(final SumType sum) {
92:                                                 return sum.getPrototype();
93:                                         }
94:                                         
95:                                         @Override
96:                                         public Type handle(final ThrownType thrownType) {
97:                                                 return thrownType.getPrototype();
98:                                         }
99:                                 });
100:                         }
101:                         
102:                         @Override
103:                         public Type handle(final TypeProxy typeProxy) {
104:                                 return UtilTransformer.getTypeOfPossibleTypeProxy(typeProxy);
105:                         }
106:                 });
107:         }
108:         
109:         /**
110:          * Returns the {@link Type}. If the type is a {@link TypeProxy} and has the state {@link ByReferenceState}, it will
111:          * return the targetType. If the type is a {@link TypeProxy} and has the state {@link ByNameState} or
112:          * {@link InvalidState} it will return null.
113:          *
114:          * @param type
115:          * @return {@link Type}
116:          */
117:         private static Type getTypeOfPossibleTypeProxy(final Type type) {
118:                 return type.accept(new TypeVisitorReturn<Type>() {
119:                         
120:                         @Override
121:                         public Type handle(final AtomicType s) {
122:                                 return s;
123:                         }
124:                         
125:                         @Override
126:                         public Type handle(final CompositeType c) {
127:                                 return c;
128:                         }
129:                         
130:                         @Override
131:                         public Type handle(final TypeProxy s) {
132:                                 return getTypeProxyTarget(s);
133:                         }
134:                 });
135:         }
136:         
137:         /**
138:          * Returns the referenced type by typeProxy, if typeProxy´s state is not of type {@link ByReferenceState} null will
139:          * be returned.
140:          *
141:          * @param typeProxy
142:          * typeProxy
143:          * @return {@link Type}
144:          */
145:         private static Type getTypeProxyTarget(final TypeProxy typeProxy) {
146:                 return typeProxy.getState().accept(new TypeProxyStateVisitorReturn<Type>() {
147:                         
148:                         @Override
149:                         public Type handle(final ByNameState byName) {
150:                                 return null;
151:                         }
152:                         
153:                         @Override
154:                         public Type handle(final InvalidState invalidTypeReference) {
155:                                 return null;
156:                         }
157:                         
158:                         @Override
159:                         public Type handle(final ByReferenceState byType) {
160:                                 return byType.getTarget();
161:                         }
162:                 });
163:         }
164:         
165:         /**
166:          * Sets {@link #classPath} and stores the current package name to the {@link #context}.
167:          *
168:          * @param name
169:          * Name of the current class
170:          * @return {@link GenPackage}
171:          */
172:         public GenPackage getPackage(final Name name) {
173:                 final GenPackage resultFirst = GenUnqualifiedPackage.create(GEN_PACKAGE_PREFIX_FIRST);
174:                 final GenPackage result = resultFirst.addName(GEN_PACKAGE_PREFIX_SECOND);
175:                 return result.addPackage(this.getRestPackage(name));
176:         }
177:         
178:         /**
179:          * Returns the {@link GenPackage} of <code>name</code>.
180:          *
181:          * @param name
182:          * the name that will be checked
183:          * @return {@link GenPackage}
184:          */
185:         private GenPackage getRestPackage(final Name name) {
186:                 return name.visit(new NameReturnVisitor<GenPackage>() {
187:                         @Override
188:                         public GenPackage handle(final QualifiedName qualifiedName) {
189:                                 final GenUnqualifiedPackage first = GenUnqualifiedPackage.create(qualifiedName.getFirst().toString());
190:                                 return qualifiedName.getRest().visit(new NameReturnVisitor<GenPackage>() {
191:                                         @Override
192:                                         public GenPackage handle(final QualifiedName qualifiedName2) {
193:                                                 final GenPackage rest = UtilTransformer.this.getRestPackage(qualifiedName2);
194:                                                 return GenQualifiedPackage.create(first, rest);
195:                                         }
196:                                         
197:                                         @Override
198:                                         public GenPackage handle(final UnqualifiedName unqualifiedName) {
199:                                                 return first;
200:                                         }
201:                                 });
202:                         }
203:                         
204:                         @Override
205:                         public GenPackage handle(final UnqualifiedName unqualifiedName) {
206:                                 return GenUnqualifiedPackage.create(unqualifiedName.toString());
207:                         }
208:                 });
209:         }
210:         
211:         /**
212:          * Projections.
213:          */
214:         public GeneratorModel getGeneratorModel() {
215:                 return this.generatorModel;
216:         }
217: }