Skip to content

Commit 6e6be83

Browse files
committed
re
1 parent 0b411b1 commit 6e6be83

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,15 @@ private ASTNode searchDirectParentControlNode(ASTNode astNode) {
219219
// locate the direct parent control type node's position
220220
parent = parent.getParent();
221221
}
222+
if (null == parent) {
223+
return null;
224+
}
222225

223226
// mark the s between astNode and statements and return
224227
int currentLine = AstUtils.getSpecificStartLine(astNode);
225228
int directParentStartLine = AstUtils.getSpecificStartLine(parent);
226229
markCtrlEdge(currentLine, astNode, directParentStartLine, parent);
230+
227231
return parent;
228232
}
229233

mirror-common/src/main/java/cn/com/mirror/reflect/EdgeConstructor.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import cn.com.mirror.repository.neo4j.node.MethodNode;
1717
import cn.com.mirror.repository.neo4j.node.StatementNode;
1818
import cn.com.mirror.repository.neo4j.storage.GraphEngine;
19+
import cn.com.mirror.utils.FileUtils;
1920
import lombok.Data;
2021
import lombok.extern.slf4j.Slf4j;
2122

@@ -60,10 +61,6 @@ public void construct() {
6061
Base tailB = getBaseElement(ctrlVal);
6162

6263
if (null == headB || null == tailB) {
63-
// TODO xyz the statement of "return this;" and "return null" can not be visited,
64-
// directly result in that head statement can not be retrieve from the base elements.
65-
// do some spacial treatment for "return this;" and "return null".
66-
6764
log.debug("TARGET: {}", targetPath);
6865
log.debug("HEAD: {}:{} \t->\t TAIL: {}:{}", ctrlKey.getLineNum(),
6966
ctrlKey.getVertexType(), ctrlVal.getLineNum(), ctrlVal.getVertexType());
@@ -141,7 +138,22 @@ private Base getBaseElement(Vertex vertex) {
141138

142139
case FIELD:
143140
case STATEMENT: {
144-
return unit.getStatements().get(vertex.getTargetPath()).get(vertex.getLineNum());
141+
Statement statement = unit.getStatements().get(vertex.getTargetPath()).get(vertex.getLineNum());
142+
if (null != statement) {
143+
return statement;
144+
}
145+
146+
log.warn("Statement: " + vertex.getLineNum()
147+
+ "-" + vertex.getVertexType()
148+
+ " for target: {" + vertex.getTargetPath() + "}"
149+
+ " is not retrieved from the variable visitor.");
150+
151+
statement = new Statement(vertex.getTargetPath(),
152+
vertex.getLineNum(),
153+
vertex.getLineNum(),
154+
FileUtils.listCodeLines(vertex.getTargetPath()).get(vertex.getLineNum() - 1),
155+
unit.getPackages().get(vertex.getTargetPath()));
156+
return statement;
145157
}
146158

147159
default:

mirror-common/src/main/java/cn/com/mirror/utils/AstUtils.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
public class AstUtils {
1010

1111
public static int getStartLine(ASTNode node) {
12-
if (null == node) return -1;
13-
1412
// Using the first two character to calculate the line number
1513
return ((CompilationUnit) node.getRoot()).getLineNumber(node.getStartPosition() + 1);
1614
}
1715

1816

1917
public static int getEndLine(ASTNode node) {
20-
if (null == node) return -1;
2118
return ((CompilationUnit) node.getRoot()).getLineNumber(
2219
node.getStartPosition() + node.getLength() - 1);
2320
}
@@ -38,6 +35,7 @@ public static final CompilationUnit getCompUnitResolveBinding(String javaFile) {
3835

3936
/**
4037
* Get specific statement (or TypeDeclaration, MethodDeclaration) start line number
38+
*
4139
* @param astNode Statement, TypeDeclaration, MethodDeclaration
4240
*/
4341
public static final int getSpecificStartLine(ASTNode astNode) {
@@ -61,6 +59,11 @@ public static final int getSpecificStartLine(ASTNode astNode) {
6159

6260
if (astNode instanceof SwitchCase) {
6361
SwitchCase switchCase = (SwitchCase) astNode;
62+
// the expression of default case is null
63+
if (null == switchCase.getExpression()) {
64+
return getStartLine(switchCase);
65+
}
66+
6467
return getStartLine(switchCase.getExpression());
6568
}
6669

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class PairAnalyserTests {
1818

1919
private static final String TEST_FILE =
20-
"/home/piggy/work/mirror/mirror-common/src/main/java/cn/com/mirror/project/unit/element/Method.java";
20+
"/home/piggy/work/mirror/mirror-common/src/main/java/cn/com/mirror/analyser/visitor/VariableVisitor.java";
2121

2222
@Test
2323
public void testDirectCtrlEdge() {

0 commit comments

Comments
 (0)