Skip to content

Package: HelperUtils$8

HelperUtils$8

nameinstructionbranchcomplexitylinemethod
handle(ByNameState)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handle(ByReferenceState)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handle(InvalidState)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package de.fhdw.wtf.walker.walker;
2:
3: import de.fhdw.wtf.common.ast.type.AtomicType;
4: import de.fhdw.wtf.common.ast.type.BaseType;
5: import de.fhdw.wtf.common.ast.type.ByNameState;
6: import de.fhdw.wtf.common.ast.type.ByReferenceState;
7: import de.fhdw.wtf.common.ast.type.ClassType;
8: import de.fhdw.wtf.common.ast.type.CompositeType;
9: import de.fhdw.wtf.common.ast.type.InvalidState;
10: import de.fhdw.wtf.common.ast.type.ListType;
11: import de.fhdw.wtf.common.ast.type.MapType;
12: import de.fhdw.wtf.common.ast.type.ProductType;
13: import de.fhdw.wtf.common.ast.type.SumType;
14: import de.fhdw.wtf.common.ast.type.ThrownType;
15: import de.fhdw.wtf.common.ast.type.Type;
16: import de.fhdw.wtf.common.ast.type.TypeProxy;
17: import de.fhdw.wtf.common.ast.visitor.AtomicTypeVisitorReturn;
18: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorReturn;
19: import de.fhdw.wtf.common.ast.visitor.TypeProxyStateVisitorReturn;
20: import de.fhdw.wtf.common.ast.visitor.TypeVisitorReturn;
21: import de.fhdw.wtf.common.exception.ast.ASTException;
22:
23: /**
24: * Hilfsklasse für den Umgang mit {@link TypeProxy}´s.
25: *
26: */
27: public final class HelperUtils {
28:         
29:         /**
30:          * Error-Fehlermeldung, wenn {@link TypeProxy} nicht als State {@link ByReferenceState} hat.
31:          *
32:          */
33:         private static final String ERROR_MESSAGE = "Referencer must run successfully before this task!!! ";
34:         
35:         /**
36:          * Error-Message: It was a CompositeType.
37:          */
38:         private static final String ERROR_WAS_COMPOSITE_MESSAGE = "It was a CompositeType!";
39:         
40:         /**
41:          * Error-Message: It was a TypeProxy.
42:          */
43:         private static final String ERROR_WAS_TYPEPROXY_MESSAGE = "It was a TypeProxy!";
44:         
45:         /**
46:          * Error-Message: It was a BaseType.
47:          */
48:         private static final String ERROR_WAS_BASETYPE_MESSAGE = "It was a BaseType!";
49:         
50:         /**
51:          * Hilfsklasse für den Umgang mit {@link TypeProxy}´s.
52:          */
53:         private HelperUtils() {
54:         }
55:         
56:         /**
57:          * Returns the Type or returns the target of the typeProxy if the Type is a TypeProxy and the state is a
58:          * ByReferenceState. If it is another State an ASTException will be thrown.
59:          *
60:          * @param type
61:          * Type
62:          * @return the type
63:          */
64:         public static Type getReferencedType(final Type type) {
65:                 return type.accept(new TypeVisitorReturn<Type>() {
66:                         
67:                         @Override
68:                         public Type handle(final AtomicType s) {
69:                                 return s;
70:                         }
71:                         
72:                         @Override
73:                         public Type handle(final CompositeType c) {
74:                                 return c;
75:                         }
76:                         
77:                         @Override
78:                         public Type handle(final TypeProxy s) {
79:                                 return s.getState().accept(new TypeProxyStateVisitorReturn<Type>() {
80:                                         
81:                                         @Override
82:                                         public Type handle(final ByNameState byName) {
83:                                                 throw new ASTException(ERROR_MESSAGE + byName);
84:                                         }
85:                                         
86:                                         @Override
87:                                         public Type handle(final InvalidState invalidTypeReference) {
88:                                                 throw new ASTException(ERROR_MESSAGE + invalidTypeReference.getName());
89:                                         }
90:                                         
91:                                         @Override
92:                                         public Type handle(final ByReferenceState byType) {
93:                                                 return byType.getTarget();
94:                                         }
95:                                 });
96:                         }
97:                 });
98:         }
99:         
100:         /**
101:          * Returns the AtomicType or returns the target of the typeProxy if the Type is a TypeProxy, the state is a
102:          * ByReferenceState and the target is also an AtomicType. If it is another State or Type an ASTException will be
103:          * thrown.
104:          *
105:          * @param type
106:          * Type
107:          * @return the type
108:          */
109:         public static AtomicType getReferencedAtomicType(final Type type) {
110:                 return type.accept(new TypeVisitorReturn<AtomicType>() {
111:                         
112:                         @Override
113:                         public AtomicType handle(final AtomicType s) {
114:                                 return s;
115:                         }
116:                         
117:                         @Override
118:                         public AtomicType handle(final CompositeType c) {
119:                                 throw new ASTException(ERROR_WAS_COMPOSITE_MESSAGE);                        }
120:                         
121:                         @Override
122:                         public AtomicType handle(final TypeProxy s) {
123:                                 return s.getState().accept(new TypeProxyStateVisitorReturn<AtomicType>() {
124:                                         
125:                                         @Override
126:                                         public AtomicType handle(final ByNameState byName) {
127:                                                 throw new ASTException(ERROR_MESSAGE + byName);
128:                                         }
129:                                         
130:                                         @Override
131:                                         public AtomicType handle(final InvalidState invalidTypeReference) {
132:                                                 throw new ASTException(ERROR_MESSAGE + invalidTypeReference.getName());
133:                                         }
134:                                         
135:                                         @Override
136:                                         public AtomicType handle(final ByReferenceState byType) {
137:                                                 return getReferencedAtomicType(byType.getTarget());
138:                                         }
139:                                 });
140:                         }
141:                 });
142:         }
143:         
144:         /**
145:          * Returns the ClassType or returns the target of the typeProxy if the Type is a TypeProxy, the state is a
146:          * ByReferenceState and the target is also an ClassType. If it is another State or Type an ASTException will be
147:          * thrown.
148:          *
149:          * @param type
150:          * Type
151:          * @return the type
152:          */
153:         public static ClassType getReferencedClassType(final Type type) {
154:                 return type.accept(new TypeVisitorReturn<ClassType>() {
155:                         
156:                         @Override
157:                         public ClassType handle(final AtomicType s) {
158:                                 return s.accept(new AtomicTypeVisitorReturn<ClassType>() {
159:                                         
160:                                         @Override
161:                                         public ClassType handle(final BaseType baseType) {
162:                                                 throw new ASTException(ERROR_WAS_BASETYPE_MESSAGE);
163:                                         }
164:                                         
165:                                         @Override
166:                                         public ClassType handle(final ClassType clazz) {
167:                                                 return clazz;
168:                                         }
169:                                 });
170:                         }
171:                         
172:                         @Override
173:                         public ClassType handle(final CompositeType c) {
174:                                 throw new ASTException(ERROR_WAS_COMPOSITE_MESSAGE + " " + c.toString());
175:                         }
176:                         
177:                         @Override
178:                         public ClassType handle(final TypeProxy s) {
179:                                 return s.getState().accept(new TypeProxyStateVisitorReturn<ClassType>() {
180:                                         
181:                                         @Override
182:                                         public ClassType handle(final ByNameState byName) {
183:                                                 throw new ASTException(ERROR_MESSAGE + byName);
184:                                         }
185:                                         
186:                                         @Override
187:                                         public ClassType handle(final InvalidState invalidTypeReference) {
188:                                                 throw new ASTException(ERROR_MESSAGE + invalidTypeReference.getName());
189:                                         }
190:                                         
191:                                         @Override
192:                                         public ClassType handle(final ByReferenceState byType) {
193:                                                 return getReferencedClassType(byType.getTarget());
194:                                         }
195:                                 });
196:                         }
197:                 });
198:         }
199:         
200:         /**
201:          * Returns the Type of a TypeProxy if the state is a ByReferenceState. If it is another State an ASTException will
202:          * be thrown.
203:          *
204:          * @param typeProxy
205:          * TypeProxy
206:          * @return the type
207:          */
208:         public static Type getTargetType(final TypeProxy typeProxy) {
209:                 return typeProxy.getState().accept(new TypeProxyStateVisitorReturn<Type>() {
210:                         
211:                         @Override
212:                         public Type handle(final ByNameState byName) {
213:                                 throw new ASTException(ERROR_MESSAGE + byName);
214:                         }
215:                         
216:                         @Override
217:                         public Type handle(final InvalidState invalidState) {
218:                                 throw new ASTException(ERROR_MESSAGE + invalidState.getName());
219:                         }
220:                         
221:                         @Override
222:                         public Type handle(final ByReferenceState byType) {
223:                                 return byType.getTarget();
224:                         }
225:                 });
226:         }
227:         
228:         /**
229:          * Returns the Type of a TypeProxy if the state is a ByReferenceState. If it is another State null is returned.
230:          *
231:          * @param typeProxy
232:          * TypeProxy
233:          * @return the type or null
234:          */
235:         public static Type tryGetTargetType(final TypeProxy typeProxy) {
236:                 return typeProxy.getState().accept(new TypeProxyStateVisitorReturn<Type>() {
237:                         
238:                         @Override
239:                         public Type handle(final ByNameState byName) {
240:                                 return null;
241:                         }
242:                         
243:                         @Override
244:                         public Type handle(final InvalidState invalidState) {
245:                                 return null;
246:                         }
247:                         
248:                         @Override
249:                         public Type handle(final ByReferenceState byType) {
250:                                 return byType.getTarget();
251:                         }
252:                 });
253:         }
254:         
255:         /**
256:          * Returns the AtomicType of a TypeProxy if the state is a ByReferenceState. If it is another State or not an
257:          * AtomicType, an ASTException will be thrown.
258:          *
259:          * @param typeProxy
260:          * TypeProxy
261:          * @return the type
262:          */
263:         public static AtomicType getTargetAtomicType(final TypeProxy typeProxy) {
264:                 return typeProxy.getState().accept(new TypeProxyStateVisitorReturn<AtomicType>() {
265:                         
266:                         @Override
267:                         public AtomicType handle(final ByNameState byName) {
268:                                 throw new ASTException(ERROR_MESSAGE + byName);
269:                         }
270:                         
271:                         @Override
272:                         public AtomicType handle(final InvalidState invalidState) {
273:                                 throw new ASTException(ERROR_MESSAGE + invalidState.getName());
274:                         }
275:                         
276:                         @Override
277:                         public AtomicType handle(final ByReferenceState byType) {
278:                                 return byType.getTarget().accept(new TypeVisitorReturn<AtomicType>() {
279:                                         
280:                                         @Override
281:                                         public AtomicType handle(final AtomicType s) {
282:                                                 return s;
283:                                         }
284:                                         
285:                                         @Override
286:                                         public AtomicType handle(final CompositeType c) {
287:                                                 throw new ASTException(ERROR_WAS_COMPOSITE_MESSAGE);
288:                                         }
289:                                         
290:                                         @Override
291:                                         public AtomicType handle(final TypeProxy s) {
292:                                                 throw new ASTException(ERROR_WAS_TYPEPROXY_MESSAGE);
293:                                         }
294:                                 });
295:                         }
296:                 });
297:         }
298:         
299:         /**
300:          * Returns the ClassType of a TypeProxy if the state is a ByReferenceState. If it is another State or not an
301:          * ClassType, an ASTException will be thrown.
302:          *
303:          * @param typeProxy
304:          * TypeProxy
305:          * @return the type
306:          */
307:         public static ClassType getTargetClassType(final TypeProxy typeProxy) {
308:                 return typeProxy.getState().accept(new TypeProxyStateVisitorReturn<ClassType>() {
309:                         
310:                         @Override
311:                         public ClassType handle(final ByNameState byName) {
312:                                 throw new ASTException(ERROR_MESSAGE + byName);
313:                         }
314:                         
315:                         @Override
316:                         public ClassType handle(final InvalidState invalidState) {
317:                                 throw new ASTException(ERROR_MESSAGE + invalidState.getName());
318:                         }
319:                         
320:                         @Override
321:                         public ClassType handle(final ByReferenceState byType) {
322:                                 return byType.getTarget().accept(new TypeVisitorReturn<ClassType>() {
323:                                         
324:                                         @Override
325:                                         public ClassType handle(final AtomicType s) {
326:                                                 return s.accept(new AtomicTypeVisitorReturn<ClassType>() {
327:                                                         
328:                                                         @Override
329:                                                         public ClassType handle(final BaseType baseType) {
330:                                                                 throw new ASTException(ERROR_WAS_BASETYPE_MESSAGE);
331:                                                         }
332:                                                         
333:                                                         @Override
334:                                                         public ClassType handle(final ClassType clazz) {
335:                                                                 return clazz;
336:                                                         }
337:                                                 });
338:                                         }
339:                                         
340:                                         @Override
341:                                         public ClassType handle(final CompositeType c) {
342:                                                 throw new ASTException(ERROR_WAS_COMPOSITE_MESSAGE);
343:                                         }
344:                                         
345:                                         @Override
346:                                         public ClassType handle(final TypeProxy s) {
347:                                                 throw new ASTException(ERROR_WAS_TYPEPROXY_MESSAGE);
348:                                         }
349:                                 });
350:                         }
351:                 });
352:         }
353:         
354:         /**
355:          * Returns the ByReferenceState of a TypeProxy if the state is a ByReferenceState. If it is another State an
356:          * ASTException will be thrown.
357:          *
358:          * @param typeProxy
359:          * TypeProxy
360:          * @return the ByReferenceState
361:          */
362:         public static ByReferenceState getByReferenceState(final TypeProxy typeProxy) {
363:                 return typeProxy.getState().accept(new TypeProxyStateVisitorReturn<ByReferenceState>() {
364:                         
365:                         @Override
366:                         public ByReferenceState handle(final ByNameState byName) {
367:                                 throw new ASTException(ERROR_MESSAGE + byName);
368:                         }
369:                         
370:                         @Override
371:                         public ByReferenceState handle(final InvalidState invalidState) {
372:                                 throw new ASTException(ERROR_MESSAGE + invalidState.getName());
373:                         }
374:                         
375:                         @Override
376:                         public ByReferenceState handle(final ByReferenceState byType) {
377:                                 return byType;
378:                         }
379:                 });
380:         }
381: }