GumTreeDiff / gumtree

An awesome code differencing tool
https://github.com/GumTreeDiff/gumtree/wiki
GNU Lesser General Public License v3.0
913 stars 171 forks source link

Getting the better output of MappingStore #135

Closed feiwww closed 5 years ago

feiwww commented 5 years ago

Thanks for your awesome code! I got some problems when want to use gumtree in my maven project. As the documentation I try to parse code or diff ast, but got "unhandled exception type IOException". Got "No generator found for file: myfile.java" when add throws declaration. Can you give me a few guidance?

import com.github.gumtreediff.gen.*;
import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.tree.*;
import com.github.gumtreediff.client.*;
import com.github.gumtreediff.matchers.*;

public class ASTDiff {
    public static void main(String args[])  {
        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

        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
    }
}
jrfaller commented 5 years ago

This might be due to ClassIndex :(

To avoid any problem if you know that you will be parsing Java files you can directly instantiate the good generator : new JdtGenerator().generateFromFile(file1);

feiwww commented 5 years ago

Thanks for your reply. It's useful to me! I still a little question:

  1. Why I got many warning This method should currently not be used (please use toShortString()) when print Iterator or MappingStore.toString().
  2. In the node (15 "CompilationUnit" "" ((0 361)) , can we believe that ((0 361)) is nodeID? how to get the unmatched nodes?
jrfaller commented 5 years ago
  1. sorry for that, it will not be the case in the soon to come version 3 :)
  2. it's absolute position of the node in the text
feiwww commented 5 years ago

I see the content of MappingStore.toString(). It include type(number) and code/value that cant one-to-one match a node. but could you tell me how to get the precisely unmatched nodes ?

jrfaller commented 5 years ago

I think that you have to traverse both trees and try if the MappingStore has them. It would be nice to have an utility method for that.

feiwww commented 5 years ago

I just compared two ast of Test.java in the paper Fine-grained and Accurate Source Code Differencing. MappingStore are as follows

[(34@@0,34@@0), (44@@,44@@), (15@@,15@@), (83@@public,83@@private), (55@@class,55@@class), (42@@i,42@@i), (83@@public,83@@public), (42@@Test,42@@Test), (39@@int,39@@int), (43@@String,43@@String), (8@@,8@@), (41@@,41@@), (31@@,31@@), (42@@foo,42@@foo), (45@@"Foo!",45@@"Foo!"), (27@@==,27@@==), (42@@String,42@@String), (42@@i,42@@i)]

The list may include the duplicate elements, so that I think I can't use it to find the unmatched node of the tree. could you please give me some points. Thank you.

jrfaller commented 5 years ago

What do you mean by duplicate elements?

feiwww commented 5 years ago

emmm, for example, one tree could include more than one node 34@@0, how to know the position of this matched node in the first pair.

feiwww commented 5 years ago

I think the better output of MappingStore(in toString() and iterator() Method) should be "type@value@(absolute position)" rather than "type@value". After that we usually use a unique value to represent node. Maybe, it's easy to solve it, and i am desired to ask for it in maven api. Thanks^-^

jrfaller commented 5 years ago

You're right! It will be the case in GumTree 3.0.

feiwww commented 5 years ago

Great! Any schedule? looking forward to…

jrfaller commented 5 years ago

ASAP but we are redesigning several APIs so it's gonna take some time. Hopefully in May / June.

jrfaller commented 5 years ago

OK in last version of develop we have improved mapping store textual conversion. You can try it out and tell us what you think.