├── .classpath
├── .gitignore.md
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── README.md
├── bin
├── main
│ ├── ASTGenerator.class
│ ├── ASTtoDOT.class
│ ├── Main.class
│ ├── MethodNodeVisitor.class
│ └── NodeVisitor.class
├── structure
│ ├── MyASTNode.class
│ └── MyMethodNode.class
└── util
│ └── FileUtil.class
├── lib
├── commons-collections-3.2.1.jar
├── commons-configuration-1.6.jar
├── commons-lang-2.5.jar
├── commons-logging-1.1.1.jar
├── org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar
├── org.eclipse.core.jobs_3.4.100.v20090429-1800.jar
├── org.eclipse.core.resources_3.5.2.R35x_v20091203-1235.jar
├── org.eclipse.core.runtime_3.5.0.v20090525.jar
├── org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar
├── org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar
├── org.eclipse.jdt.core-3.10.0.jar
├── org.eclipse.jdt.core_3.5.2.v_981_R35x.jar
├── org.eclipse.osgi_3.12.0.v20170512-1932.jar
└── org.eclipse.osgi_3.5.2.R35x_v20100126.jar
├── output
├── .DS_Store
└── HelloWorld.java_main.dot
├── src
├── main
│ ├── ASTGenerator.java
│ ├── ASTtoDOT.java
│ ├── Main.java
│ ├── MethodNodeVisitor.java
│ └── NodeVisitor.java
├── structure
│ ├── MyASTNode.java
│ └── MyMethodNode.java
└── util
│ └── FileUtil.java
├── testdata
└── HelloWorld.java
└── visulization
├── README.md
└── graphviz.png
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/.gitignore.md:
--------------------------------------------------------------------------------
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*
24 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | javaparser
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
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
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java-to-AST-with-Visulization
2 |
3 | This project is used for generating an AST for a given Java program. The AST will be stored in .dot format and can be simply visulized by several online tools.
4 |
5 | # How to run?
6 |
7 | - Import the project to eclipse as a Java project.
8 | - Add libraries in `lib` folder to build path.
9 | - Run `Main.java`.
10 |
11 | # Example
12 |
13 | - Input program
14 |
15 | ```java
16 | public class HelloWorld {
17 |
18 | public static void main(String[] args) {
19 | // Prints "Hello, World" to the terminal window.
20 | System.out.println("Hello, World");
21 | }
22 |
23 | }
24 | ```
25 |
26 | - Output .dot
27 |
28 | ```
29 | digraph "DirectedGraph" {
30 | graph [label = "main", labelloc=t, concentrate = true];
31 | "1188392295" [ label="(public static void main(String[] args){ System.out.println(\"Hello, World\"); } ,MethodDeclaration,3,6)" type=31 startLineNumber=3 endLineNumber=6 ]
32 | "226710952" [ label="(public,Modifier,3,3)" type=83 startLineNumber=3 endLineNumber=3 ]
33 | "1509563803" [ label="(static,Modifier,3,3)" type=83 startLineNumber=3 endLineNumber=3 ]
34 | "684874119" [ label="(void,PrimitiveType,3,3)" type=39 startLineNumber=3 endLineNumber=3 ]
35 | "1157740463" [ label="(main,SimpleName,3,3)" type=42 startLineNumber=3 endLineNumber=3 ]
36 | "1379435698" [ label="(String[] args,SingleVariableDeclaration,3,3)" type=44 startLineNumber=3 endLineNumber=3 ]
37 | "1529306539" [ label="(String[],ArrayType,3,3)" type=5 startLineNumber=3 endLineNumber=3 ]
38 | "1635985705" [ label="(String,SimpleType,3,3)" type=43 startLineNumber=3 endLineNumber=3 ]
39 | "695682681" [ label="(String,SimpleName,3,3)" type=42 startLineNumber=3 endLineNumber=3 ]
40 | "1073502961" [ label="(args,SimpleName,3,3)" type=42 startLineNumber=3 endLineNumber=3 ]
41 | "1582797472" [ label="({ System.out.println(\"Hello, World\"); } ,Block,3,6)" type=8 startLineNumber=3 endLineNumber=6 ]
42 | "644166178" [ label="(System.out.println(\"Hello, World\"); ,ExpressionStatement,5,5)" type=21 startLineNumber=5 endLineNumber=5 ]
43 | "892529689" [ label="(System.out.println(\"Hello, World\"),MethodInvocation,5,5)" type=32 startLineNumber=5 endLineNumber=5 ]
44 | "1757676444" [ label="(System.out,QualifiedName,5,5)" type=40 startLineNumber=5 endLineNumber=5 ]
45 | "182738614" [ label="(System,SimpleName,5,5)" type=42 startLineNumber=5 endLineNumber=5 ]
46 | "94345706" [ label="(out,SimpleName,5,5)" type=42 startLineNumber=5 endLineNumber=5 ]
47 | "670035812" [ label="(println,SimpleName,5,5)" type=42 startLineNumber=5 endLineNumber=5 ]
48 | "1870647526" [ label="(\"Hello, World\",StringLiteral,5,5)" type=45 startLineNumber=5 endLineNumber=5 ]
49 | "1188392295" -> "226710952"
50 | "1188392295" -> "1509563803"
51 | "1188392295" -> "684874119"
52 | "1188392295" -> "1157740463"
53 | "1188392295" -> "1379435698"
54 | "1379435698" -> "1529306539"
55 | "1529306539" -> "1635985705"
56 | "1635985705" -> "695682681"
57 | "1379435698" -> "1073502961"
58 | "1188392295" -> "1582797472"
59 | "1582797472" -> "644166178"
60 | "644166178" -> "892529689"
61 | "892529689" -> "1757676444"
62 | "1757676444" -> "182738614"
63 | "1757676444" -> "94345706"
64 | "892529689" -> "670035812"
65 | "892529689" -> "1870647526"
66 | }
67 | ```
68 |
69 | - Visualization
70 |
71 | 
72 |
73 |
--------------------------------------------------------------------------------
/bin/main/ASTGenerator.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/main/ASTGenerator.class
--------------------------------------------------------------------------------
/bin/main/ASTtoDOT.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/main/ASTtoDOT.class
--------------------------------------------------------------------------------
/bin/main/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/main/Main.class
--------------------------------------------------------------------------------
/bin/main/MethodNodeVisitor.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/main/MethodNodeVisitor.class
--------------------------------------------------------------------------------
/bin/main/NodeVisitor.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/main/NodeVisitor.class
--------------------------------------------------------------------------------
/bin/structure/MyASTNode.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/structure/MyASTNode.class
--------------------------------------------------------------------------------
/bin/structure/MyMethodNode.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/structure/MyMethodNode.class
--------------------------------------------------------------------------------
/bin/util/FileUtil.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/bin/util/FileUtil.class
--------------------------------------------------------------------------------
/lib/commons-collections-3.2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/commons-collections-3.2.1.jar
--------------------------------------------------------------------------------
/lib/commons-configuration-1.6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/commons-configuration-1.6.jar
--------------------------------------------------------------------------------
/lib/commons-lang-2.5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/commons-lang-2.5.jar
--------------------------------------------------------------------------------
/lib/commons-logging-1.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/commons-logging-1.1.1.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.core.jobs_3.4.100.v20090429-1800.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.core.jobs_3.4.100.v20090429-1800.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.core.resources_3.5.2.R35x_v20091203-1235.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.core.resources_3.5.2.R35x_v20091203-1235.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.core.runtime_3.5.0.v20090525.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.core.runtime_3.5.0.v20090525.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.jdt.core-3.10.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.jdt.core-3.10.0.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.jdt.core_3.5.2.v_981_R35x.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.jdt.core_3.5.2.v_981_R35x.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.osgi_3.12.0.v20170512-1932.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.osgi_3.12.0.v20170512-1932.jar
--------------------------------------------------------------------------------
/lib/org.eclipse.osgi_3.5.2.R35x_v20100126.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/lib/org.eclipse.osgi_3.5.2.R35x_v20100126.jar
--------------------------------------------------------------------------------
/output/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/output/.DS_Store
--------------------------------------------------------------------------------
/output/HelloWorld.java_main.dot:
--------------------------------------------------------------------------------
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 | }
39 |
--------------------------------------------------------------------------------
/src/main/ASTGenerator.java:
--------------------------------------------------------------------------------
1 | package main;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | import org.eclipse.jdt.core.dom.AST;
9 | import org.eclipse.jdt.core.dom.ASTNode;
10 | import org.eclipse.jdt.core.dom.ASTParser;
11 | import org.eclipse.jdt.core.dom.CompilationUnit;
12 | import org.eclipse.jdt.core.dom.MethodDeclaration;
13 | import main.MethodNodeVisitor;
14 | import structure.MyMethodNode;
15 | import structure.MyASTNode;
16 | import util.FileUtil;
17 |
18 | public class ASTGenerator {
19 |
20 | public List methodNodeList = new ArrayList();
21 |
22 | public ASTGenerator(File f) {
23 | ParseFile(f);
24 | }
25 |
26 | /**
27 | * get function for methodNodeList
28 | * @return
29 | */
30 | public List getMethodNodeList() {
31 | return methodNodeList;
32 | }
33 |
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());
41 | parser.setKind(ASTParser.K_COMPILATION_UNIT);
42 |
43 | final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
44 |
45 | // find the MethodDeclaration node, MethodNodeVisitor
46 | MethodNodeVisitor methodNodeVisitor = new MethodNodeVisitor();
47 | cu.accept(methodNodeVisitor);
48 | // traverse all child nodes, NodeVisitor
49 | for (MethodDeclaration m : methodNodeVisitor.getMethodDecs()) {
50 | MyMethodNode mNode = new MyMethodNode();
51 | mNode.methodNode = m;
52 | NodeVisitor nv = new NodeVisitor();
53 | m.accept(nv);
54 | List astnodes = nv.getASTNodes();
55 | for (ASTNode node : astnodes) {
56 | MyASTNode myNode = new MyASTNode();
57 | myNode.astNode = node;
58 | myNode.startLineNum = cu.getLineNumber(node.getStartPosition());
59 | myNode.endLineNum = cu.getLineNumber(node.getStartPosition()+node.getLength());
60 | // add to nodeList
61 | mNode.nodeList.add(myNode);
62 | // add to mapping
63 | // in case, I need to exclude root node
64 | if (node.equals(m)) {
65 | continue;
66 | }
67 | int pHashcode = node.getParent().hashCode();
68 | int hashcode = node.hashCode();
69 | int[] link = { pHashcode, hashcode };
70 | mNode.mapping.add(link);
71 | }
72 | methodNodeList.add(mNode);
73 | }
74 | // System.out.print(ast);
75 | }
76 |
77 | public void ParseFile(File f) {
78 | String filePath = f.getAbsolutePath();
79 | if (f.isFile()) {
80 | try {
81 | parse(FileUtil.readFileToString(filePath));
82 | } catch (IOException e) {
83 | // TODO Auto-generated catch block
84 | e.printStackTrace();
85 | }
86 | } else {
87 | System.out.println("Not a File!");
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/main/ASTtoDOT.java:
--------------------------------------------------------------------------------
1 | package main;
2 |
3 | import org.eclipse.jdt.core.dom.ASTNode;
4 |
5 | import structure.MyASTNode;
6 | import structure.MyMethodNode;
7 |
8 | public class ASTtoDOT {
9 |
10 | /**
11 | * Convert a method node to .dot string
12 | * @param m
13 | * @return
14 | */
15 | public static String ASTtoDotParser(MyMethodNode m) {
16 | String str = "digraph \"DirectedGraph\" {\n";
17 | // name
18 | str += ("graph [label = \"" + m.methodNode.getName() + "\", labelloc=t, concentrate = true];\n");
19 | for (MyASTNode mn : m.nodeList) {
20 | ASTNode astNode = mn.astNode;
21 | int hashcode = astNode.hashCode();
22 | int nodeType = astNode.getNodeType();
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");
26 | }
27 | for (int[] k : m.mapping) {
28 | int pHashcode = k[0];
29 | int hashcode = k[1];
30 | str += ("\"" + String.valueOf(pHashcode) + "\" -> \"" + String.valueOf(hashcode) + "\"\n");
31 | }
32 | str += "}\n";
33 | return str;
34 | }
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 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/Main.java:
--------------------------------------------------------------------------------
1 | package main;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 | import java.util.List;
6 |
7 | import util.FileUtil;
8 | import main.ASTGenerator;
9 | import structure.MyMethodNode;
10 |
11 | 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 | */
18 | public static void main(String[] args) throws IOException {
19 | String FilePath = "PATH/HelloWorld.java";
20 | String outputDir = "PATH/output/";
21 | File f = new File(FilePath);
22 | ASTGenerator astGenerator = new ASTGenerator(f);
23 | List methodNodeList = astGenerator.getMethodNodeList();
24 | for (MyMethodNode m : methodNodeList) {
25 | String dotStr = ASTtoDOT.ASTtoDotParser(m);
26 | FileUtil.writeFile(outputDir + f.getName() + "_" + m.methodNode.getName() + ".dot", dotStr);
27 | }
28 | System.out.println("Done.");
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/MethodNodeVisitor.java:
--------------------------------------------------------------------------------
1 | package main;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import org.eclipse.jdt.core.dom.ASTVisitor;
6 | import org.eclipse.jdt.core.dom.MethodDeclaration;
7 |
8 |
9 | public class MethodNodeVisitor extends ASTVisitor {
10 |
11 | List methodNodeList = new ArrayList<>();
12 |
13 | public List getMethodDecs() {
14 | return methodNodeList;
15 | }
16 |
17 | @Override
18 | public boolean visit(MethodDeclaration node) {
19 | methodNodeList.add(node);
20 | return true;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/NodeVisitor.java:
--------------------------------------------------------------------------------
1 | package main;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.eclipse.jdt.core.dom.ASTNode;
7 | import org.eclipse.jdt.core.dom.ASTVisitor;
8 |
9 | public class NodeVisitor extends ASTVisitor {
10 |
11 | public List nodeList = new ArrayList();
12 |
13 | @Override
14 | public void preVisit(ASTNode node) {
15 | nodeList.add(node);
16 | }
17 |
18 | public List getASTNodes() {
19 | return nodeList;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/structure/MyASTNode.java:
--------------------------------------------------------------------------------
1 | package structure;
2 |
3 | import org.eclipse.jdt.core.dom.ASTNode;
4 |
5 | public class MyASTNode {
6 |
7 | public ASTNode astNode;
8 | public int startLineNum;
9 | public int endLineNum;
10 | }
11 |
--------------------------------------------------------------------------------
/src/structure/MyMethodNode.java:
--------------------------------------------------------------------------------
1 | package structure;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.eclipse.jdt.core.dom.MethodDeclaration;
7 | import structure.MyASTNode;
8 |
9 | public class MyMethodNode {
10 |
11 | public MethodDeclaration methodNode = null;
12 | public List nodeList = null;
13 |
14 | public List mapping = null;
15 |
16 | public MyMethodNode() {
17 | this.methodNode = null;
18 | this.nodeList = new ArrayList();
19 | this.mapping = new ArrayList();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/util/FileUtil.java:
--------------------------------------------------------------------------------
1 | package util;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.FileReader;
5 | import java.io.FileWriter;
6 | import java.io.IOException;
7 |
8 | public class FileUtil {
9 |
10 | public static void writeFile(String FilePath, String str) {
11 | try {
12 | FileWriter writer = new FileWriter(FilePath);
13 | writer.write(str);
14 | writer.close();
15 | } catch (IOException e) {
16 | e.printStackTrace();
17 | }
18 | }
19 |
20 | // read file content into a string
21 | public static String readFileToString(String filePath) throws IOException {
22 | StringBuilder fileData = new StringBuilder(1000);
23 | BufferedReader reader = new BufferedReader(new FileReader(filePath));
24 |
25 | char[] buf = new char[10];
26 | int numRead = 0;
27 | while ((numRead = reader.read(buf)) != -1) {
28 | // System.out.println(numRead);
29 | String readData = String.valueOf(buf, 0, numRead);
30 | fileData.append(readData);
31 | buf = new char[1024];
32 | }
33 |
34 | reader.close();
35 |
36 | return fileData.toString();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/testdata/HelloWorld.java:
--------------------------------------------------------------------------------
1 | public class HelloWorld {
2 |
3 | public static void main(String[] args) {
4 | // Prints "Hello, World" to the terminal window.
5 | System.out.println("Hello, World");
6 | }
7 |
8 | }
--------------------------------------------------------------------------------
/visulization/README.md:
--------------------------------------------------------------------------------
1 | # Visualize AST in .dot Format
2 |
3 | There are several online .dot visulization tool can support covert raw .dot file to image formats (e.g., png, jpg).
4 |
5 | - [webgraphviz](http://www.webgraphviz.com)
6 | - [GraphvizOnline](https://dreampuf.github.io/GraphvizOnline)
7 | - [Graphviz it!](http://graphviz.it)
--------------------------------------------------------------------------------
/visulization/graphviz.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Program-Analysis/Java-to-AST-with-Visualization/f8ffeaf75f09a29ef3ee410c572161fdc2becb33/visulization/graphviz.png
--------------------------------------------------------------------------------