Skip to content

Commit 78fbaac

Browse files
committed
re
1 parent 34092cb commit 78fbaac

File tree

5 files changed

+178
-95
lines changed

5 files changed

+178
-95
lines changed

mirror-common/src/main/java/cn/com/mirror/analyser/visitor/ControlEdgeVisitor.java

Lines changed: 74 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package cn.com.mirror.analyser.visitor;
22

33
import cn.com.mirror.constant.ControlNodeTypeEnum;
4-
import cn.com.mirror.constant.VertexTypeEnum;
54
import cn.com.mirror.project.pair.Vertex;
5+
import cn.com.mirror.project.pair.factory.VertexFactory;
66
import cn.com.mirror.utils.AstUtils;
77
import lombok.Getter;
88
import lombok.extern.slf4j.Slf4j;
@@ -31,84 +31,6 @@ public ControlEdgeVisitor(String targetPath) {
3131
this.targetPath = targetPath;
3232
}
3333

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-
11234
@Override
11335
public boolean visit(TypeDeclaration node) {
11436
int typeDecLine = AstUtils.getStartLine(node.getName());
@@ -170,14 +92,6 @@ public boolean visit(DoStatement node) {
17092
return super.visit(node);
17193
}
17294

173-
174-
// EmptyStatement
175-
// @Override
176-
// public boolean visit(EmptyStatement node) {
177-
// return super.visit(node);
178-
// }
179-
180-
18195
// ExpressionStatement
18296
@Override
18397
public boolean visit(ExpressionStatement node) {
@@ -295,4 +209,77 @@ public boolean visit(EnhancedForStatement node) {
295209
searchDirectParentControlNode(node);
296210
return super.visit(node);
297211
}
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+
298285
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.com.mirror.exceptions;
2+
3+
/**
4+
* @author piggy
5+
* @description
6+
* @date 18-8-24
7+
*/
8+
public class GenerateVertexException extends RuntimeException {
9+
private static final long serialVersionUID = 1L;
10+
11+
public GenerateVertexException(String message) {
12+
super(message);
13+
}
14+
15+
public GenerateVertexException(String message, Throwable cause) {
16+
super(message, cause);
17+
}
18+
}

mirror-common/src/main/java/cn/com/mirror/project/pair/Vertex.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ public class Vertex implements Serializable {
1717
private String targetPath;
1818
private int lineNum;
1919
private VertexTypeEnum vertexType;
20-
private String host;
20+
private String ip;
2121
private int port;
2222

2323
public Vertex(String targetPath,
2424
int lineNum,
2525
VertexTypeEnum vertexType,
26-
String host,
26+
String ip,
2727
int port) {
2828

2929
this.targetPath = targetPath;
3030
this.lineNum = lineNum;
3131
this.vertexType = vertexType;
32-
this.host = host;
32+
this.ip = ip;
3333
this.port = port;
3434
}
3535

@@ -40,13 +40,14 @@ public boolean equals(Object o) {
4040

4141
Vertex vertex = (Vertex) o;
4242
return lineNum == vertex.lineNum &&
43+
port == vertex.port &&
4344
Objects.equals(targetPath, vertex.targetPath) &&
44-
vertexType == vertex.vertexType;
45+
vertexType == vertex.vertexType &&
46+
Objects.equals(ip, vertex.ip);
4547
}
4648

4749
@Override
4850
public int hashCode() {
49-
50-
return Objects.hash(targetPath, lineNum, vertexType);
51+
return Objects.hash(targetPath, lineNum, vertexType, ip, port);
5152
}
5253
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cn.com.mirror.project.pair.factory;
2+
3+
import cn.com.mirror.constant.VertexTypeEnum;
4+
import cn.com.mirror.exceptions.GenerateVertexException;
5+
import cn.com.mirror.project.pair.Vertex;
6+
import lombok.Data;
7+
import org.eclipse.jdt.core.dom.ASTNode;
8+
import org.eclipse.jdt.core.dom.FieldDeclaration;
9+
import org.eclipse.jdt.core.dom.MethodDeclaration;
10+
import org.eclipse.jdt.core.dom.TypeDeclaration;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
/**
16+
* @author piggy
17+
* @description
18+
* @date 18-8-24
19+
*/
20+
@Data
21+
public class VertexFactory {
22+
private Map<String, Vertex> distributedVertex = new HashMap<>();
23+
24+
private String getHostKey(String ip, int port, String targetPath, int lineNum) {
25+
String hostKey = null;
26+
if (null != ip && -1 != port) {
27+
// vertex are distributed in the system
28+
hostKey = ip + ":" + port + ":" + targetPath.hashCode() + ":" + lineNum;
29+
} else {
30+
// vertex are in the localhost
31+
hostKey = "LOCALHOST:-1" + ":" + targetPath.hashCode() + ":" + lineNum;
32+
}
33+
hostKey.intern();
34+
return hostKey;
35+
}
36+
37+
private VertexTypeEnum checkVertexType(ASTNode node) {
38+
if (node instanceof TypeDeclaration) return VertexTypeEnum.CLASS;
39+
if (node instanceof MethodDeclaration) return VertexTypeEnum.METHOD;
40+
if (node instanceof FieldDeclaration) return VertexTypeEnum.FIELD;
41+
return VertexTypeEnum.STATEMENT;
42+
}
43+
44+
public Vertex getVertex(String ip, int port, String targetPath, int lineNum) {
45+
return this.distributedVertex.get(
46+
getHostKey(ip, port, targetPath, lineNum));
47+
}
48+
49+
public Vertex genVertex(String targetPath, int curLine, ASTNode node) {
50+
return genVertex(targetPath, curLine, node, null, -1);
51+
}
52+
53+
public Vertex genVertex(String targetPath, int curLine, ASTNode node, String ip, int port) {
54+
if (null != ip && -1 == port) {
55+
throw new GenerateVertexException("Port can not be -1.");
56+
}
57+
58+
if (-1 != port && null == ip) {
59+
throw new GenerateVertexException("IP address can not be null");
60+
}
61+
62+
String hostKey = getHostKey(ip, port, targetPath, curLine);
63+
Vertex vertex = this.distributedVertex.get(hostKey);
64+
if (null == vertex) {
65+
vertex = new Vertex(targetPath, curLine, checkVertexType(node), ip, port);
66+
this.distributedVertex.put(hostKey, vertex);
67+
}
68+
69+
return vertex;
70+
}
71+
72+
}

mirror-common/src/test/cn/com/mirror/pair/PairAnalyserTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ public void testDirectCtrlEdge() {
2929
for (Map.Entry<Vertex, Vertex> ver : entry.getValue().entrySet()) {
3030
System.out.println("HEAD: " +
3131
ver.getKey().getLineNum() + " - " +
32-
ver.getKey().getVertexType() + "\t->\t" + "TAIL: " +
32+
ver.getKey().getVertexType() + " - " +
33+
ver.getKey().hashCode() +
34+
"\t->\t" +
35+
"TAIL: " +
3336
ver.getValue().getLineNum() + ", " +
34-
ver.getValue().getVertexType());
37+
ver.getValue().getVertexType() + " - " +
38+
ver.getValue().hashCode()
39+
);
3540
}
3641
// return;
3742
// }

0 commit comments

Comments
 (0)