Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose the most specific method when calling overloaded methods #5

Open
lukaseder opened this issue May 29, 2014 · 1 comment
Open

Comments

@lukaseder
Copy link
Member

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.

@DevDengChao
Copy link
Contributor

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.

@lukaseder lukaseder added this to the Version 0.9.16 milestone Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants