GumTreeDiff / gumtree

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

How to simplify GTD's actions? #318

Closed Feng-Jay closed 1 year ago

Feng-Jay commented 1 year ago

Hi, I'm using GTD3.0.0 on macOS to analyze Java codes.

When I tried to get modification between simple examples, I find the output somewhat redundant:

My diff codes are like this:

Tree t1 = new JdtTreeGenerator().generateFrom().file(srcFile).getRoot();
Tree t2 = new JdtTreeGenerator().generateFrom().file(dstFile).getRoot();
Matcher defaultMatcher = Matchers.getInstance().getMatcher();
MappingStore mappings  = defaultMatcher.match(t1, t2);
EditScriptGenerator editScriptGenerator = new SimplifiedChawatheScriptGenerator();
EditScript actions = editScriptGenerator.computeActions(mappings);
for(Action ac : actions){
    System.out.println(ac.toString());
}

Here are two examples:

//Example1
public class test1 {
    public int add(int a, int b) {
        return a+b;
    }

    public void test (int x, int a) {
        int x1 = add(x,x);
    }
}
//Example2
public class test2 {
    public int add(int a, int b) {
        return a+b;
    }

    public void test (int x, int a) {
        int x2 = 0;
    }
}

Clearly, the example2 changes the class name and replace int x1 = add(x,x); with int x2 = 0;

The output actions are correct, but I think there are too many delete-node to handle in my application:

===
update-node
---
SimpleName: test1 [13,18]
replace test1 by test2
===
update-node
---
SimpleName: x [142,143]
replace x by x2
===
move-tree
---
SimpleName: x [142,143]
to
VariableDeclarationFragment [133,146]
at 0
===
insert-node
---
NumberLiteral: 0 [138,139]
to
VariableDeclarationFragment [133,146]
at 1
===
delete-node
---
SimpleName: x1 [133,135]
===
===
delete-node
---
SimpleName: add [138,141]
===
===
delete-node
---
SimpleName: x [144,145]
===
===
delete-node
---
METHOD_INVOCATION_ARGUMENTS [142,145]
===
===
delete-node
---
MethodInvocation [138,146]
===

Whether can I simplify these delete-node actions to only:

===
delete-node
---
MethodInvocation [138,146]
===

I notice you mentioned -m gumtree-simple in issue#264 how can I use it in java codes?

By the way, I wonder where can I get the doc of these APIs, it seems no javadoc in my IDEA Editor.

jrfaller commented 1 year ago

Hi @Feng-Jay !

This is a case where the original gumtree algorithm is too aggressive when searching for mappings in the recovery phase.

I recommend you to use the new simple gumtree algorithm which has a good trade off between speed and accuracy: Matcher defaultMatcher = new CompositeMatchers.SimpleGumTree();. Cheers!