Skip to content

Commit 1ffc25c

Browse files
committed
re
1 parent 354c691 commit 1ffc25c

File tree

7 files changed

+138
-5
lines changed

7 files changed

+138
-5
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package cn.com.mirror.analyser.visitor;
22

33
import cn.com.mirror.constant.ControlNodeTypeEnum;
4+
import cn.com.mirror.project.pair.Vertex;
5+
import cn.com.mirror.project.pair.VertexTypeEnum;
46
import cn.com.mirror.utils.AstUtils;
57
import lombok.Getter;
68
import lombok.extern.slf4j.Slf4j;
79
import org.eclipse.jdt.core.dom.*;
10+
import org.neo4j.cypher.internal.compiler.v2_3.commands.AstNode;
811

912
import java.util.Arrays;
1013
import java.util.HashMap;
@@ -21,7 +24,14 @@
2124
@Slf4j
2225
@Getter
2326
public class ControlEdgeVisitor extends ASTVisitor {
27+
private String targetPath;
28+
2429
private Map<Integer, Integer> controlEdges = new HashMap<>();
30+
private Map<Vertex, Vertex> ctrlEdges = new HashMap<>();
31+
32+
public ControlEdgeVisitor(String targetPath) {
33+
this.targetPath = targetPath;
34+
}
2535

2636
private ASTNode searchDirectParentControlNode(ASTNode astNode) {
2737
ASTNode parent = astNode.getParent();
@@ -35,6 +45,8 @@ private ASTNode searchDirectParentControlNode(ASTNode astNode) {
3545
int currentLine = AstUtils.getSpecificStartLine(astNode);
3646
int directParentStartLine = AstUtils.getSpecificStartLine(parent);
3747
markEdges(currentLine, directParentStartLine);
48+
49+
markCtrlEdge(currentLine, astNode, directParentStartLine, parent);
3850
return parent;
3951
}
4052

@@ -78,6 +90,35 @@ private boolean isControlType(ASTNode astNode) {
7890

7991
}
8092

93+
private boolean markCtrlEdge(int curLine,
94+
ASTNode astNode,
95+
int parentLine,
96+
ASTNode parent) {
97+
if (-1 == parentLine || parentLine == curLine) {
98+
return false;
99+
}
100+
101+
VertexTypeEnum headType = null;
102+
if (astNode instanceof MethodDeclaration) {
103+
headType = VertexTypeEnum.METHOD;
104+
} else {
105+
headType = VertexTypeEnum.STATEMENT;
106+
}
107+
108+
Vertex head = new Vertex(this.targetPath, curLine, headType);
109+
110+
VertexTypeEnum tailType = null;
111+
if (parent instanceof MethodDeclaration) {
112+
tailType = VertexTypeEnum.METHOD;
113+
} else {
114+
tailType = VertexTypeEnum.STATEMENT;
115+
}
116+
Vertex tail = new Vertex(this.targetPath, parentLine, tailType);
117+
ctrlEdges.put(head, tail);
118+
119+
return true;
120+
}
121+
81122
private boolean markEdges(int curLine, int parentLine) {
82123
if (-1 != parentLine &&
83124
curLine != parentLine) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
@Getter
1414
public class Pair {
1515
private Map<String, Map<Integer, Integer>> directCtrlEdges = new HashMap<>();
16+
private Map<String, Map<Vertex, Vertex>> ctrlEdges = new HashMap<>();
1617

1718
public void addDirectCtrlEdges(String targetPath, Map<Integer, Integer> ctrlEdges) {
1819
this.directCtrlEdges.put(targetPath, ctrlEdges);
1920
}
21+
22+
public void addCtrlEdge(String targetPath, Map<Vertex, Vertex> ctrlEdges) {
23+
this.ctrlEdges.put(targetPath, ctrlEdges);
24+
}
2025
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.com.mirror.project.pair;
2+
3+
import lombok.Getter;
4+
5+
import java.io.Serializable;
6+
import java.util.Objects;
7+
8+
/**
9+
* @author piggy
10+
* @description
11+
* @date 18-8-23
12+
*/
13+
@Getter
14+
public class Vertex implements Serializable {
15+
16+
private String targetPath;
17+
private int lineNum;
18+
private VertexTypeEnum vertexType;
19+
20+
public Vertex(String targetPath, int lineNum, VertexTypeEnum vertexType) {
21+
this.targetPath = targetPath;
22+
this.lineNum = lineNum;
23+
this.vertexType = vertexType;
24+
}
25+
26+
@Override
27+
public boolean equals(Object o) {
28+
if (this == o) return true;
29+
if (o == null || getClass() != o.getClass()) return false;
30+
31+
Vertex vertex = (Vertex) o;
32+
return lineNum == vertex.lineNum &&
33+
Objects.equals(targetPath, vertex.targetPath) &&
34+
vertexType == vertex.vertexType;
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
40+
return Objects.hash(targetPath, lineNum, vertexType);
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.com.mirror.project.pair;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* @author piggy
7+
* @description
8+
* @date 18-8-23
9+
*/
10+
@Getter
11+
public enum VertexTypeEnum {
12+
STATEMENT(1, "statement"),
13+
METHOD(2, "method");
14+
15+
private Integer key;
16+
private String desc;
17+
18+
VertexTypeEnum(Integer key, String desc) {
19+
this.key = key;
20+
this.desc = desc;
21+
}
22+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ public Pair newPair(String repositoryUrl) {
2020

2121
Set<String> targetFiles = FileUtils.extractTargetPath(repositoryUrl);
2222
targetFiles.stream().forEach(targetPath -> {
23-
ControlEdgeVisitor controlEdgeVisitor = new ControlEdgeVisitor();
23+
ControlEdgeVisitor controlEdgeVisitor = new ControlEdgeVisitor(targetPath);
2424
CompilationUnit compilationUnit = AstUtils.getCompUnitResolveBinding(targetPath);
2525
compilationUnit.accept(controlEdgeVisitor);
2626
pair.addDirectCtrlEdges(targetPath, controlEdgeVisitor.getControlEdges());
27+
pair.addCtrlEdge(targetPath, controlEdgeVisitor.getCtrlEdges());
2728
});
2829

2930
return pair;

mirror-common/src/test/cn/com/mirror/analyser/UnitAnalyserTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void testControlDependenceVisitor() {
3232
init();
3333

3434
unit.getTargets().stream().forEach(targetPah -> {
35-
ControlEdgeVisitor controlDependenceVisitor = new ControlEdgeVisitor();
35+
ControlEdgeVisitor controlDependenceVisitor = new ControlEdgeVisitor(targetPah);
3636
CompilationUnit compilationUnit = unit.getCompilationUnits().get(targetPah);
3737
compilationUnit.accept(controlDependenceVisitor);
3838
log.info("Target path: {}", targetPah);

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.com.mirror.analyser.PairAnalyser;
44
import cn.com.mirror.project.pair.Pair;
5+
import cn.com.mirror.project.pair.Vertex;
56
import lombok.extern.slf4j.Slf4j;
67
import org.junit.Test;
78

@@ -23,14 +24,35 @@ public class PairAnalyserTests {
2324
public void test1() {
2425
PairAnalyser pairAnalyser = new PairAnalyser();
2526
Pair pair = pairAnalyser.analyze();
26-
Set<Map.Entry<String, Map<Integer, Integer>>> entries =
27-
pair.getDirectCtrlEdges().entrySet();
27+
Set<Map.Entry<String, Map<Integer, Integer>>> entries = pair.getDirectCtrlEdges().entrySet();
2828
entries.stream().forEach(entry -> {
29-
if(TEST_FILE.equals(entry.getKey())){
29+
if (TEST_FILE.equals(entry.getKey())) {
3030
log.info("Target: {}", entry.getKey());
3131
log.info("Pair: {}", entry.getValue());
3232
return;
3333
}
3434
});
3535
}
36+
37+
38+
@Test
39+
public void test2() {
40+
PairAnalyser pairAnalyser = new PairAnalyser();
41+
Pair pair = pairAnalyser.analyze();
42+
for (Map.Entry<String, Map<Vertex, Vertex>> entry : pair.getCtrlEdges().entrySet()) {
43+
if (TEST_FILE.equals(entry.getKey())) {
44+
log.debug("Target: {}", entry.getKey());
45+
46+
for (Map.Entry<Vertex, Vertex> ver : entry.getValue().entrySet()) {
47+
log.debug("HEAD: {}, {} -> TAIL: {}, {}",
48+
ver.getKey().getLineNum(),
49+
ver.getKey().getVertexType(),
50+
ver.getValue().getLineNum(),
51+
ver.getValue().getVertexType());
52+
}
53+
54+
return;
55+
}
56+
}
57+
}
3658
}

0 commit comments

Comments
 (0)