Skip to content
Jean-Rémy Falleri edited this page Jun 24, 2015 · 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 gumtree-all 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

Using a specific generator

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

Getting the mappings between two trees

Run.initGenerators();
String file1 = "file_v0.java";
String file2 = "file_v1.java";
ITree src = Generators.getInstance().getTree(file1).getRoot();
ITree dst = Generators.getInstance().getTree(file2).getRoot();
Matcher m = Matchers.getInstance().getMatcher(src, dst); // retrieve the default matcher
m.match();
m.getMappings(); // return the mapping store

Computing the actions between two trees

Run.initGenerators();
String file1 = "file_v0.java";
String file2 = "file_v1.java";
ITree src = Generators.getInstance().getTree(file1).getRoot();
ITree dst = Generators.getInstance().getTree(file2).getRoot();
Matcher m = Matchers.getInstance().getMatcher(src, dst); // retrieve the default matcher
m.match();
ActionGenerator g = new ActionGenerator(src, dst, m.getMappings());
g.generate();
List<Action> actions = g.getActions(); // return the actions
Clone this wiki locally