spoon icon indicating copy to clipboard operation
spoon copied to clipboard

[Bug]: Different methods with same name are merged into one method

Open livelyRyan opened this issue 2 years ago • 1 comments

Describe the bug

I use spoon.refactoring.MethodInvocationSearch to get the invocation relationship of methods, and different methods with same name are merged into one when i use getInvocationsOfMethod to retrieve the result. The demo is shown as below.

Source code you are trying to analyze/transform

public class Test {

    public void test2() {
        test3();
        Test2.test3();
        Child.test3();
    }

    public static void test3() {

    }

    public void test4() {

    }

    private static class Child {
        public static void test3() {

        }
    }

}

Source code for your Spoon processing

@Slf4j
@RunWith(SpringRunner.class)
public class TestMethodLines {

    @Test
    public void test() throws URISyntaxException {
        final Launcher spoon = initSpoon();
        MethodInvocationSearch processor = new MethodInvocationSearch();
        CtModel model = spoon.getModel();
        model.getElements(new TypeFilter<>(CtExecutable.class)).forEach(processor::scan);

        MethodCallGraph methodCallGraph = MethodGraphUtils.buildMethodCallGraph(processor.getInvocationsOfMethod());
        Collection<MethodCallGraph.MethodNode> allMethods = methodCallGraph.getAllMethods();
        for (MethodCallGraph.MethodNode methodNode : allMethods) {
            Collection<MethodCallGraph.MethodNode> callerMethods = methodNode.getCallerMethods();
            if (CollectionUtils.size(callerMethods) != 0) {
                String join = JOINER
                        .join(Collections2.transform(callerMethods, new Function<MethodCallGraph.MethodNode, Object>() {
                            @Override public Object apply(MethodCallGraph.MethodNode input) {
                                return input.getMethod().getMethodName();
                            }
                        }));
                System.out.println(methodNode.getMethod().getMethodName() + " is called by " +join);
            }
        }
    }

    private Launcher initSpoon() throws URISyntaxException {
        String path = Test1.class.getResource("/" + "Test.java").toURI().getPath();
        final String[] arg = {
                "-i", path,
                "-o", "target/spooned/"
        };

        final Launcher launcher = new Launcher();
        launcher.setArgs(arg);
        launcher.run();
        return launcher;
    }
}

Actual output

test3 is called by test2

Expected output

rathen than showing only one single method "test3 is called by test2" ,three different methods of "test3" are called by test2

Spoon Version

tried with 9.1.0 and 10.1.1

JVM Version

11

What operating system are you using?

tried both on Linux and Windows

livelyRyan avatar Aug 08 '22 09:08 livelyRyan

I wrote this code a while ago, I can try to have a look in it next week. This week, I'm sadly busy.

MartinWitt avatar Aug 09 '22 03:08 MartinWitt