Skip to content

Package: CyclicInheritanceCheck

CyclicInheritanceCheck

nameinstructionbranchcomplexitylinemethod
CyclicInheritanceCheck(Model, TaskExecutor)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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%
create(Model, TaskExecutor)
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%
finalizeTask()
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%
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: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
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.walker.tasks;
2:
3: import java.util.HashSet;
4: import java.util.Set;
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.Name;
11: import de.fhdw.wtf.common.ast.type.AtomicType;
12: import de.fhdw.wtf.common.ast.type.BaseType;
13: import de.fhdw.wtf.common.ast.type.ClassType;
14: import de.fhdw.wtf.common.ast.type.Type;
15: import de.fhdw.wtf.common.ast.visitor.AtomicTypeVisitorException;
16: import de.fhdw.wtf.common.exception.walker.CyclicInheritanceException;
17: import de.fhdw.wtf.common.exception.walker.TaskException;
18: import de.fhdw.wtf.common.task.TaskExecutor;
19: import de.fhdw.wtf.common.token.Token;
20: import de.fhdw.wtf.walker.walker.HelperUtils;
21: import de.fhdw.wtf.walker.walker.SimpleWalkerTask;
22:
23: /**
24: * This Task check the Inheritance-Structur. If this Checker found a cyclus, it will thrown a
25: * {@link CyclicInheritanceException}.
26: *
27: * This Checker must run after the {@link TypeReferencer}.
28: */
29: public final class CyclicInheritanceCheck extends SimpleWalkerTask {
30:         /**
31:          * Constructor of {@link CyclicInheritanceCheck}.
32:          *
33:          * @param m
34:          * model
35:          * @param taskmanager
36:          * taskmanager
37:          */
38:         private CyclicInheritanceCheck(final Model m, final TaskExecutor taskmanager) {
39:                 super(m, taskmanager);
40:         }
41:         
42:         /**
43:          * Creates a {@link CyclicInheritanceCheck}-Object.
44:          *
45:          * @param m
46:          * model
47:          * @param taskmanager
48:          * taskmanager
49:          * @return The {@link CyclicInheritanceCheck}-Object.
50:          */
51:         public static CyclicInheritanceCheck create(final Model m, final TaskExecutor taskmanager) {
52:                 return new CyclicInheritanceCheck(m, taskmanager);
53:         }
54:         
55:         @Override
56:         public void handleGroup(final Group g) throws TaskException {
57:                 // Niothing to do here
58:         }
59:         
60:         @Override
61:         public void handleClass(final ClassType c) throws TaskException {
62:                 final CyclicChecker cyclicChecker = new CyclicChecker();
63:                 cyclicChecker.checkClassInheritance(c);
64:         }
65:         
66:         @Override
67:         public void handleAttribute(final Attribute a, final ClassType owner) throws TaskException {
68:                 // Nothing to do here
69:         }
70:         
71:         @Override
72:         public void finalizeTask() throws TaskException {
73:                 // Nothing to do here
74:         }
75:         
76:         /**
77:          * CyclicChecker check the Inheritance-Structur. If the CyclicChecker found a cyclus, it will thrown a
78:          * {@link CyclicInheritanceException}.
79:          */
80:         private class CyclicChecker {
81:                 
82:                 /**
83:                  * Found supertypes.
84:                  */
85:                 private final Set<Name> foundSupertypes;
86:                 
87:                 /**
88:                  * Intern-Constructor of {@link CyclicChecker}.
89:                  *
90:                  * @param alreadyFoundSupertypes
91:                  * alreadyFoundSupertypes in earlier steps.
92:                  */
93:                 CyclicChecker(final Set<Name> alreadyFoundSupertypes) {
94:                         this.foundSupertypes = new HashSet<>();
95:                         this.foundSupertypes.addAll(alreadyFoundSupertypes);
96:                 }
97:                 
98:                 /**
99:                  * Constructor of {@link CyclicChecker}.
100:                  */
101:                 CyclicChecker() {
102:                         this.foundSupertypes = new HashSet<>();
103:                 }
104:                 
105:                 /**
106:                  * Check the Inheritance-Structur of c for cycles. First- and LastToken will need for Exception-Message. They
107:                  * are need to mark the Failure in the wtf-Model (f.e. ...:class=Person+{... and then the Person-Position)
108:                  *
109:                  * @param c
110:                  * class
111:                  * @param firstToken
112:                  * firstToken
113:                  * @param lastToken
114:                  * lastToken
115:                  * @throws CyclicInheritanceException
116:                  * CyclicInheritanceException
117:                  */
118:                 void checkClassInheritance(final ClassType c, final Token firstToken, final Token lastToken)
119:                                 throws CyclicInheritanceException {
120:                         if (this.foundSupertypes.contains(c.getTypeName())) {
121:                                 throw CyclicInheritanceException.create(c.getTypeName(), firstToken, lastToken);
122:                         }
123:                         this.checkClassInheritance(c);
124:                 }
125:                 
126:                 /**
127:                  * Check the Inheritance-Structur of c for cycles.
128:                  *
129:                  * @param c
130:                  * class
131:                  * @throws CyclicInheritanceException
132:                  * CyclicInheritanceException
133:                  */
134:                 public void checkClassInheritance(final ClassType c) throws CyclicInheritanceException {
135:                         this.foundSupertypes.add(c.getTypeName());
136:                         
137:                         for (final Type current : c.getSuperTypes()) {
138:                                 final AtomicType targetType = HelperUtils.getReferencedAtomicType(current);
139:                                 
140:                                 final CyclicChecker cyclicChecker = new CyclicChecker(this.foundSupertypes);
141:                                 
142:                                 targetType.accept(new AtomicTypeVisitorException<CyclicInheritanceException>() {
143:                                         
144:                                         @Override
145:                                         public void handle(final BaseType baseType) throws CyclicInheritanceException {
146:                                                 // Nothing to do, this should check an other Checker
147:                                         }
148:                                         
149:                                         @Override
150:                                         public void handle(final ClassType clazz) throws CyclicInheritanceException {
151:                                                 cyclicChecker.checkClassInheritance(clazz, current.getFirstToken(), current.getLastToken());
152:                                         }
153:                                 });
154:                         }
155:                 }
156:         }
157:         
158:         @Override
159:         public void beginTask() throws TaskException {
160:                 // Nothing to do here
161:         }
162:         
163:         @Override
164:         public void handleConstructorOrOperation(final ConstructorOrOperation coo, final ClassType owner)
165:                         throws TaskException {
166:                 // Nothing to do here
167:                 
168:         }
169: }