Skip to content
Jean-Rémy Falleri edited this page Apr 21, 2020 · 14 revisions

It is possible to process source code file directly from the Java code. To use the GumTree API, we recommend to launch your program using the classpath of the dist project, which contains all the dependencies. Here are some examples.

Parsing a file

Using the generator registry

Run.initGenerators();
String file = "myfile.java";
TreeContext tc = Generators.getInstance().getTree(file); // retrieve the default generator for the file
ITree t = tc.getRoot(); // return the root of the tree
System.out.println(TreeIoUtils.toLisp(tc).toString()); // displays the tree in LISP syntax

Using a specific generator

String file = "myfile.java";
ITree tree = new JdtTreeGenerator().generateFromString(file).getRoot();

Computing the mappings between two trees

Run.initGenerators();
String srcFile = "file_v0.java";
String dstFile = "file_v1.java";
ITree src = Generators.getInstance().getTree(srcFile).getRoot();
ITree dst = Generators.getInstance().getTree(dstFile).getRoot();
Matcher defaultMatcher = Matchers.getInstance().getMatcher();
MappingStore mappings = defaultMatcher.match(src, dst);

Computing the edit script between two trees

Run.initGenerators();
String srcFile = "file_v0.java";
String dstFile = "file_v1.java";
ITree src = Generators.getInstance().getTree(srcFile).getRoot();
ITree dst = Generators.getInstance().getTree(dstFile).getRoot();
Matcher defaultMatcher = Matchers.getInstance().getMatcher();
MappingStore mappings = defaultMatcher.match(src, dst);
EditScriptGenerator editScriptGenerator = new SimplifiedChawatheScriptGenerator();
EditScript actions = editScriptGenerator.computeActions(mappings);
Clone this wiki locally