Skip to content
Jean-Rémy Falleri edited this page Feb 4, 2021 · 14 revisions

You can use GumTree directly from Java code. Here are some examples (it should compatible with the version at the latest commit):

Parsing a file

Using the generator registry

Run.initGenerators(); // registers the available parsers
String file = "myfile.java";
TreeContext tc = TreeGenerators.getInstance().getTree(file); // retrieves and applies the default parser for the file 
Tree t = tc.getRoot(); // retrieves the root of the tree
System.out.println(t.toTreeString()); // displays the tree in our ad-hoc format
System.out.println(TreeIoUtils.toLisp(tc).toString()); // displays the tree in LISP syntax

Using a specific generator

String file = "myfile.java";
Tree tree = new JdtTreeGenerator().generateFrom().file(file).getRoot(); // instantiates and applies the JDT generator

Computing the mappings between two trees

Run.initGenerators(); // registers the available parsers
String srcFile = "file_v0.java";
String dstFile = "file_v1.java";
Tree src = TreeGenerators.getInstance().getTree(srcFile).getRoot(); // retrieves and applies the default parser for the file 
Tree dst = TreeGenerators.getInstance().getTree(dstFile).getRoot(); // retrieves and applies the default parser for the file 
Matcher defaultMatcher = Matchers.getInstance().getMatcher(); // retrieves the default matcher
MappingStore mappings = defaultMatcher.match(src, dst); // computes the mappings between the trees

Computing the edit script between two trees

Run.initGenerators(); // registers the available parsers
String srcFile = "file_v0.java";
String dstFile = "file_v1.java";
Tree src = TreeGenerators.getInstance().getTree(srcFile).getRoot(); // retrieves and applies the default parser for the file 
Tree dst = TreeGenerators.getInstance().getTree(dstFile).getRoot(); // retrieves and applies the default parser for the file 
Matcher defaultMatcher = Matchers.getInstance().getMatcher(); // retrieves the default matcher
MappingStore mappings = defaultMatcher.match(src, dst); // computes the mappings between the trees
EditScriptGenerator editScriptGenerator = new SimplifiedChawatheScriptGenerator(); // instantiates the simplified Chawathe script generator
EditScript actions = editScriptGenerator.computeActions(mappings); // computes the edit script