jOOR icon indicating copy to clipboard operation
jOOR copied to clipboard

Choose the most specific method when calling overloaded methods

Open lukaseder opened this issue 11 years ago • 1 comments

Currently, jOOR chooses the "first" matching method if multiple overloaded methods are applicable. E.g.:

void method(Object o);
void method(Number o);
void method(Integer o);

// This should call method(Number), but it may also call method(Object)
on(Test.class).create(new Long("1"));

Finding the most specific method according to the rules of the JLS is not easy, in particular because jOOR follows its own rules. E.g., in the future, we may also want to consider private methods to be eligible for the search algorithm.

lukaseder avatar May 29 '14 10:05 lukaseder

public class JOORTests {

    private static int object = 0;
    private static int number = 0;
    private static int integer = 0;

    private static void overload(Object o) {
        System.out.println("object overload invoked");
        JOORTests.object++;
    }

    private static void overload(Number number) {
        System.out.println("number overload invoked");
        JOORTests.number++;
    }

    private static void overload(Integer integer) {
        System.out.println("integer overload invoked");
        JOORTests.integer++;
    }

    @Test
    public void test() {
        Reflect reflect = Reflect.onClass(JOORTests.class);
        reflect.call("overload", Integer.parseInt("1"));
        Assert.assertEquals(1, JOORTests.integer);

        reflect.call("overload", Long.parseLong("1"));
        Assert.assertEquals(1, JOORTests.number);


        reflect.call("overload", "Hello world!");
        Assert.assertEquals(1, JOORTests.object);
    }
}

This test case works fine on jdk1.8.0_271 when using org.jooq:joor-java-8:0.9.13 You may add addtional test cases and update the status of this issue.

DevDengChao avatar Jan 26 '21 03:01 DevDengChao