|
1 | 1 | package cn.com.mirror.analyser.visitor;
|
2 | 2 |
|
3 | 3 | import cn.com.mirror.constant.ControlNodeTypeEnum;
|
4 |
| -import cn.com.mirror.constant.VertexTypeEnum; |
5 | 4 | import cn.com.mirror.project.pair.Vertex;
|
| 5 | +import cn.com.mirror.project.pair.factory.VertexFactory; |
6 | 6 | import cn.com.mirror.utils.AstUtils;
|
7 | 7 | import lombok.Getter;
|
8 | 8 | import lombok.extern.slf4j.Slf4j;
|
@@ -31,84 +31,6 @@ public ControlEdgeVisitor(String targetPath) {
|
31 | 31 | this.targetPath = targetPath;
|
32 | 32 | }
|
33 | 33 |
|
34 |
| - private ASTNode searchDirectParentControlNode(ASTNode astNode) { |
35 |
| - ASTNode parent = astNode.getParent(); |
36 |
| - while (null != parent && |
37 |
| - !isControlType(parent)) { |
38 |
| - // locate the direct parent control type node's position |
39 |
| - parent = parent.getParent(); |
40 |
| - } |
41 |
| - |
42 |
| - // mark the s between astNode and statements and return |
43 |
| - int currentLine = AstUtils.getSpecificStartLine(astNode); |
44 |
| - int directParentStartLine = AstUtils.getSpecificStartLine(parent); |
45 |
| - markCtrlEdge(currentLine, astNode, directParentStartLine, parent); |
46 |
| - return parent; |
47 |
| - } |
48 |
| - |
49 |
| - |
50 |
| - /** |
51 |
| - * check the control type node in a method |
52 |
| - */ |
53 |
| - private boolean isControlType(ASTNode astNode) { |
54 |
| - // is astNode in a control type node, this method will return directly |
55 |
| - // if (x && y){}, x and y are in the control type of if node, this method |
56 |
| - // will re turn directly and cant find x and y's direct control edge |
57 |
| - |
58 |
| - if (astNode instanceof MethodDeclaration) return true; |
59 |
| - |
60 |
| - if (astNode instanceof Statement) { |
61 |
| - Statement statement = (Statement) astNode; |
62 |
| - |
63 |
| - switch (ControlNodeTypeEnum.getControlNodeType(statement)) { |
64 |
| - case IF: |
65 |
| - case SWITCH_CASE: |
66 |
| - case SWITCH: |
67 |
| - /** |
68 |
| - * the blocks in if and switch case statement's block are |
69 |
| - * control dependence on the condition expression which is |
70 |
| - * well conveyed in a way like if(expression) and switch(expression). |
71 |
| - */ |
72 |
| - case TRY: |
73 |
| - case WHILE: |
74 |
| - case ENHANCED_FOR: |
75 |
| - case FOR: |
76 |
| - case LABELED: |
77 |
| - case DO: |
78 |
| - return true; |
79 |
| - |
80 |
| - default: |
81 |
| - return false; |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - return false; |
86 |
| - |
87 |
| - } |
88 |
| - |
89 |
| - private boolean markCtrlEdge(int curLine, |
90 |
| - ASTNode astNode, |
91 |
| - int parentLine, |
92 |
| - ASTNode parent) { |
93 |
| - if (-1 == parentLine || parentLine == curLine) { |
94 |
| - return false; |
95 |
| - } |
96 |
| - |
97 |
| - // TODO xyz vertex might not need to create all the time, use a cache and generate from it |
98 |
| - Vertex head = new Vertex(this.targetPath, curLine, checkVertexType(astNode), null, -1); |
99 |
| - Vertex tail = new Vertex(this.targetPath, parentLine, checkVertexType(parent), null, -1); |
100 |
| - ctrlEdges.put(head, tail); |
101 |
| - |
102 |
| - return true; |
103 |
| - } |
104 |
| - |
105 |
| - private VertexTypeEnum checkVertexType(ASTNode node) { |
106 |
| - if (node instanceof TypeDeclaration) return VertexTypeEnum.CLASS; |
107 |
| - if (node instanceof MethodDeclaration) return VertexTypeEnum.METHOD; |
108 |
| - if (node instanceof FieldDeclaration) return VertexTypeEnum.FIELD; |
109 |
| - return VertexTypeEnum.STATEMENT; |
110 |
| - } |
111 |
| - |
112 | 34 | @Override
|
113 | 35 | public boolean visit(TypeDeclaration node) {
|
114 | 36 | int typeDecLine = AstUtils.getStartLine(node.getName());
|
@@ -170,14 +92,6 @@ public boolean visit(DoStatement node) {
|
170 | 92 | return super.visit(node);
|
171 | 93 | }
|
172 | 94 |
|
173 |
| - |
174 |
| - // EmptyStatement |
175 |
| -// @Override |
176 |
| -// public boolean visit(EmptyStatement node) { |
177 |
| -// return super.visit(node); |
178 |
| -// } |
179 |
| - |
180 |
| - |
181 | 95 | // ExpressionStatement
|
182 | 96 | @Override
|
183 | 97 | public boolean visit(ExpressionStatement node) {
|
@@ -295,4 +209,77 @@ public boolean visit(EnhancedForStatement node) {
|
295 | 209 | searchDirectParentControlNode(node);
|
296 | 210 | return super.visit(node);
|
297 | 211 | }
|
| 212 | + |
| 213 | + |
| 214 | + // private methods |
| 215 | + private ASTNode searchDirectParentControlNode(ASTNode astNode) { |
| 216 | + ASTNode parent = astNode.getParent(); |
| 217 | + while (null != parent && |
| 218 | + !isControlType(parent)) { |
| 219 | + // locate the direct parent control type node's position |
| 220 | + parent = parent.getParent(); |
| 221 | + } |
| 222 | + |
| 223 | + // mark the s between astNode and statements and return |
| 224 | + int currentLine = AstUtils.getSpecificStartLine(astNode); |
| 225 | + int directParentStartLine = AstUtils.getSpecificStartLine(parent); |
| 226 | + markCtrlEdge(currentLine, astNode, directParentStartLine, parent); |
| 227 | + return parent; |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * check the control type node in a method |
| 232 | + */ |
| 233 | + private boolean isControlType(ASTNode astNode) { |
| 234 | + // is astNode in a control type node, this method will return directly |
| 235 | + // if (x && y){}, x and y are in the control type of if node, this method |
| 236 | + // will re turn directly and cant find x and y's direct control edge |
| 237 | + |
| 238 | + if (astNode instanceof MethodDeclaration) return true; |
| 239 | + |
| 240 | + if (astNode instanceof Statement) { |
| 241 | + Statement statement = (Statement) astNode; |
| 242 | + |
| 243 | + switch (ControlNodeTypeEnum.getControlNodeType(statement)) { |
| 244 | + case IF: |
| 245 | + case SWITCH_CASE: |
| 246 | + case SWITCH: |
| 247 | + /** |
| 248 | + * the blocks in if and switch case statement's block are |
| 249 | + * control dependence on the condition expression which is |
| 250 | + * well conveyed in a way like if(expression) and switch(expression). |
| 251 | + */ |
| 252 | + case TRY: |
| 253 | + case WHILE: |
| 254 | + case ENHANCED_FOR: |
| 255 | + case FOR: |
| 256 | + case LABELED: |
| 257 | + case DO: |
| 258 | + return true; |
| 259 | + |
| 260 | + default: |
| 261 | + return false; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + return false; |
| 266 | + |
| 267 | + } |
| 268 | + |
| 269 | + private boolean markCtrlEdge(int curLine, |
| 270 | + ASTNode astNode, |
| 271 | + int parentLine, |
| 272 | + ASTNode parent) { |
| 273 | + if (-1 == parentLine || parentLine == curLine) { |
| 274 | + return false; |
| 275 | + } |
| 276 | + |
| 277 | + VertexFactory vertexFactory = new VertexFactory(); |
| 278 | + Vertex head = vertexFactory.genVertex(this.targetPath, curLine, astNode); |
| 279 | + Vertex tail = vertexFactory.genVertex(this.targetPath, parentLine, parent); |
| 280 | + ctrlEdges.put(head, tail); |
| 281 | + return true; |
| 282 | + } |
| 283 | + |
| 284 | + |
298 | 285 | }
|
0 commit comments