Skip to content

Commit 26268d8

Browse files
committed
enhance documentation
1 parent e7aa576 commit 26268d8

22 files changed

+169
-166
lines changed

.DS_Store

6 KB
Binary file not shown.

.classpath

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
34
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5-
<classpathentry exported="true" kind="lib" path="lib/org.eclipse.jdt.core-3.10.0.jar"/>
6-
<classpathentry kind="lib" path="lib/org.eclipse.osgi_3.12.0.v20170512-1932.jar"/>
7-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.osgi_3.12.0.v20170512-1932.jar"/>
8-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.osgi_3.5.2.R35x_v20100126.jar"/>
9-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.jdt.core-3.10.0.jar"/>
10-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.jdt.core_3.5.2.v_981_R35x.jar"/>
11-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar"/>
12-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar"/>
13-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.core.runtime_3.5.0.v20090525.jar"/>
14-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.core.resources_3.5.2.R35x_v20091203-1235.jar"/>
15-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.core.jobs_3.4.100.v20090429-1800.jar"/>
16-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar"/>
17-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/commons-logging-1.1.1.jar"/>
18-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/commons-lang-2.5.jar"/>
19-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/commons-configuration-1.6.jar"/>
20-
<classpathentry kind="lib" path="/Users/xubowen/Desktop/Study/Research/201706-KnowledgeGraphandAPI/ASTGeneration/lib/commons-collections-3.2.1.jar"/>
5+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/commons-collections-3.2.1.jar"/>
6+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/commons-configuration-1.6.jar"/>
7+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/commons-lang-2.5.jar"/>
8+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/commons-logging-1.1.1.jar"/>
9+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar"/>
10+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.core.jobs_3.4.100.v20090429-1800.jar"/>
11+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.core.resources_3.5.2.R35x_v20091203-1235.jar"/>
12+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.core.runtime_3.5.0.v20090525.jar"/>
13+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar"/>
14+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar"/>
15+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.jdt.core_3.5.2.v_981_R35x.jar"/>
16+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.jdt.core-3.10.0.jar"/>
17+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.osgi_3.5.2.R35x_v20100126.jar"/>
18+
<classpathentry kind="lib" path="/Users/Bowen/GoogleDrive/Study/PHD/PhD/2019-2020/Term2/IS706/W2/JDT2AST2DOT/lib/org.eclipse.osgi_3.12.0.v20170512-1932.jar"/>
2119
<classpathentry kind="output" path="bin"/>
2220
</classpath>

.gitignore.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*

.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>ASTGeneration</name>
3+
<name>javaparser</name>
44
<comment></comment>
55
<projects>
66
</projects>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

bin/main/ASTGenerator.class

-1.16 KB
Binary file not shown.

bin/main/ASTtoDOT.class

675 Bytes
Binary file not shown.

bin/main/Main.class

-70 Bytes
Binary file not shown.

bin/structure/MyASTNode.class

-16 Bytes
Binary file not shown.

bin/util/FileUtil.class

809 Bytes
Binary file not shown.

output/.DS_Store

6 KB
Binary file not shown.

output/HelloWorld.java_main.dot

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
digraph "DirectedGraph" {
2+
graph [label = "main", labelloc=t, concentrate = true];
3+
"1188392295" [ label="(public static void main(String[] args){ System.out.println(\"Hello, World\"); } ,MethodDeclaration,3,6)" type=31 startLineNumber=3 endLineNumber=6 ]
4+
"226710952" [ label="(public,Modifier,3,3)" type=83 startLineNumber=3 endLineNumber=3 ]
5+
"1509563803" [ label="(static,Modifier,3,3)" type=83 startLineNumber=3 endLineNumber=3 ]
6+
"684874119" [ label="(void,PrimitiveType,3,3)" type=39 startLineNumber=3 endLineNumber=3 ]
7+
"1157740463" [ label="(main,SimpleName,3,3)" type=42 startLineNumber=3 endLineNumber=3 ]
8+
"1379435698" [ label="(String[] args,SingleVariableDeclaration,3,3)" type=44 startLineNumber=3 endLineNumber=3 ]
9+
"1529306539" [ label="(String[],ArrayType,3,3)" type=5 startLineNumber=3 endLineNumber=3 ]
10+
"1635985705" [ label="(String,SimpleType,3,3)" type=43 startLineNumber=3 endLineNumber=3 ]
11+
"695682681" [ label="(String,SimpleName,3,3)" type=42 startLineNumber=3 endLineNumber=3 ]
12+
"1073502961" [ label="(args,SimpleName,3,3)" type=42 startLineNumber=3 endLineNumber=3 ]
13+
"1582797472" [ label="({ System.out.println(\"Hello, World\"); } ,Block,3,6)" type=8 startLineNumber=3 endLineNumber=6 ]
14+
"644166178" [ label="(System.out.println(\"Hello, World\"); ,ExpressionStatement,5,5)" type=21 startLineNumber=5 endLineNumber=5 ]
15+
"892529689" [ label="(System.out.println(\"Hello, World\"),MethodInvocation,5,5)" type=32 startLineNumber=5 endLineNumber=5 ]
16+
"1757676444" [ label="(System.out,QualifiedName,5,5)" type=40 startLineNumber=5 endLineNumber=5 ]
17+
"182738614" [ label="(System,SimpleName,5,5)" type=42 startLineNumber=5 endLineNumber=5 ]
18+
"94345706" [ label="(out,SimpleName,5,5)" type=42 startLineNumber=5 endLineNumber=5 ]
19+
"670035812" [ label="(println,SimpleName,5,5)" type=42 startLineNumber=5 endLineNumber=5 ]
20+
"1870647526" [ label="(\"Hello, World\",StringLiteral,5,5)" type=45 startLineNumber=5 endLineNumber=5 ]
21+
"1188392295" -> "226710952"
22+
"1188392295" -> "1509563803"
23+
"1188392295" -> "684874119"
24+
"1188392295" -> "1157740463"
25+
"1188392295" -> "1379435698"
26+
"1379435698" -> "1529306539"
27+
"1529306539" -> "1635985705"
28+
"1635985705" -> "695682681"
29+
"1379435698" -> "1073502961"
30+
"1188392295" -> "1582797472"
31+
"1582797472" -> "644166178"
32+
"644166178" -> "892529689"
33+
"892529689" -> "1757676444"
34+
"1757676444" -> "182738614"
35+
"1757676444" -> "94345706"
36+
"892529689" -> "670035812"
37+
"892529689" -> "1870647526"
38+
}

output/test.java_testNonEscaped.dot

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/main/ASTGenerator.java

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main;
22

3-
import java.io.BufferedReader;
43
import java.io.File;
5-
import java.io.FileReader;
64
import java.io.IOException;
75
import java.util.ArrayList;
86
import java.util.List;
@@ -15,43 +13,31 @@
1513
import main.MethodNodeVisitor;
1614
import structure.MyMethodNode;
1715
import structure.MyASTNode;
16+
import util.FileUtil;
1817

1918
public class ASTGenerator {
2019

2120
public List<MyMethodNode> methodNodeList = new ArrayList<MyMethodNode>();
2221

2322
public ASTGenerator(File f) {
24-
// TODO Auto-generated constructor stub
2523
ParseFile(f);
2624
}
2725

26+
/**
27+
* get function for methodNodeList
28+
* @return
29+
*/
2830
public List<MyMethodNode> getMethodNodeList() {
2931
return methodNodeList;
3032
}
3133

32-
// read file content into a string
33-
public String readFileToString(String filePath) throws IOException {
34-
StringBuilder fileData = new StringBuilder(1000);
35-
BufferedReader reader = new BufferedReader(new FileReader(filePath));
36-
37-
char[] buf = new char[10];
38-
int numRead = 0;
39-
while ((numRead = reader.read(buf)) != -1) {
40-
// System.out.println(numRead);
41-
String readData = String.valueOf(buf, 0, numRead);
42-
fileData.append(readData);
43-
buf = new char[1024];
44-
}
45-
46-
reader.close();
47-
48-
return fileData.toString();
49-
}
50-
51-
// use ASTParse to parse string
52-
public void parse(String str) {
53-
ASTParser parser = ASTParser.newParser(AST.JLS8);
54-
parser.setSource(str.toCharArray());
34+
/**
35+
* use ASTParse to parse string
36+
* @param srcStr
37+
*/
38+
public void parse(String srcStr) {
39+
ASTParser parser = ASTParser.newParser(AST.JLS3);
40+
parser.setSource(srcStr.toCharArray());
5541
parser.setKind(ASTParser.K_COMPILATION_UNIT);
5642

5743
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
@@ -69,7 +55,8 @@ public void parse(String str) {
6955
for (ASTNode node : astnodes) {
7056
MyASTNode myNode = new MyASTNode();
7157
myNode.astNode = node;
72-
myNode.lineNum = cu.getLineNumber(node.getStartPosition());
58+
myNode.startLineNum = cu.getLineNumber(node.getStartPosition());
59+
myNode.endLineNum = cu.getLineNumber(node.getStartPosition()+node.getLength());
7360
// add to nodeList
7461
mNode.nodeList.add(myNode);
7562
// add to mapping
@@ -87,30 +74,11 @@ public void parse(String str) {
8774
// System.out.print(ast);
8875
}
8976

90-
// loop directory to get file list
91-
public void ParseFilesInDir() throws IOException {
92-
File dirs = new File(".");
93-
String dirPath = dirs.getCanonicalPath() + File.separator + "src" + File.separator;
94-
95-
File root = new File(dirPath);
96-
// System.out.println(rootDir.listFiles());
97-
File[] files = root.listFiles();
98-
String filePath = null;
99-
100-
for (File f : files) {
101-
filePath = f.getAbsolutePath();
102-
if (f.isFile()) {
103-
parse(readFileToString(filePath));
104-
}
105-
}
106-
}
107-
108-
// loop directory to get file list
10977
public void ParseFile(File f) {
11078
String filePath = f.getAbsolutePath();
11179
if (f.isFile()) {
11280
try {
113-
parse(readFileToString(filePath));
81+
parse(FileUtil.readFileToString(filePath));
11482
} catch (IOException e) {
11583
// TODO Auto-generated catch block
11684
e.printStackTrace();

src/main/ASTtoDOT.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
public class ASTtoDOT {
99

10+
/**
11+
* Convert a method node to .dot string
12+
* @param m
13+
* @return
14+
*/
1015
public static String ASTtoDotParser(MyMethodNode m) {
1116
String str = "digraph \"DirectedGraph\" {\n";
1217
// name
@@ -15,9 +20,9 @@ public static String ASTtoDotParser(MyMethodNode m) {
1520
ASTNode astNode = mn.astNode;
1621
int hashcode = astNode.hashCode();
1722
int nodeType = astNode.getNodeType();
18-
int lineNum = mn.lineNum;
19-
str += ("\"" + String.valueOf(hashcode) + "\" [ type=" + String.valueOf(nodeType) + " line="
20-
+ String.valueOf(lineNum) + " ]\n");
23+
str += ("\"" + String.valueOf(hashcode) + "\" [ label=\""+buildLabel(mn)+"\" type=" + String.valueOf(nodeType) + " startLineNumber="
24+
+ String.valueOf(mn.startLineNum)+" endLineNumber="
25+
+ String.valueOf(mn.endLineNum) + " ]\n");
2126
}
2227
for (int[] k : m.mapping) {
2328
int pHashcode = k[0];
@@ -27,4 +32,15 @@ public static String ASTtoDotParser(MyMethodNode m) {
2732
str += "}\n";
2833
return str;
2934
}
35+
36+
/**
37+
* Configure the label, i.e., what you want to display in the visulization
38+
* @param node
39+
* @return
40+
*/
41+
public static String buildLabel(MyASTNode node) {
42+
String contentString=node.astNode.toString().replace("\n", " ").replace("\"", "\\\"").replace(" ", " ");
43+
String nodeType=ASTNode.nodeClassForType(node.astNode.getNodeType()).getName().replace("org.eclipse.jdt.core.dom.", "");
44+
return "("+contentString+","+nodeType+","+node.startLineNum+","+node.endLineNum+")";
45+
}
3046
}

src/main/Main.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
import structure.MyMethodNode;
1010

1111
public class Main {
12+
13+
/**
14+
* given the path of a java program which you want to parse and the output directory
15+
* @param args
16+
* @throws IOException
17+
*/
1218
public static void main(String[] args) throws IOException {
13-
String FilePath = "PathofTestJavaFile";
14-
String outputDir = "OutputDir";
19+
String FilePath = "/Users/Bowen/eclipse-workspace/javaparser/testdata/HelloWorld.java";
20+
String outputDir = "/Users/Bowen/eclipse-workspace/javaparser/output/";
1521
File f = new File(FilePath);
1622
ASTGenerator astGenerator = new ASTGenerator(f);
1723
List<MyMethodNode> methodNodeList = astGenerator.getMethodNodeList();

src/structure/MyASTNode.java

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

55
public class MyASTNode {
66

7-
public ASTNode astNode = null;
8-
public int lineNum = -1;
7+
public ASTNode astNode;
8+
public int startLineNum;
9+
public int endLineNum;
910
}

0 commit comments

Comments
 (0)