janino-compiler / janino

Janino is a super-small, super-fast Java™ compiler.
http://janino-compiler.github.io/janino
Other
1.25k stars 208 forks source link

overloaded static on-demand imported methods cause a CompileException: Ambiguous static method import #93

Closed ihasbini closed 5 years ago

ihasbini commented 5 years ago

When a class "on-demand" imports static methods from other class(es), and when the methods have the same name, and different parameter types, then the exception "org.codehaus.commons.compiler.CompileException: Ambiguous static method import" is thrown.

The below code in UnitCompiler.java does not select the "most specific method" to bind to the invocation: ` // Static method declared through static-import-on-demand? for (Object o : this.importStaticOnDemand(mi.methodName)) {

            if (!(o instanceof IMethod)) continue;
            IMethod im = (IMethod) o;

            if (iMethod != null) {
                this.compileError(
                    "Ambiguous static method import: \""
                    + iMethod.toString()
                    + "\" vs. \""
                    + im.toString()
                    + "\""
                );
            }

            iMethod = im;
        }
        if (iMethod != null) break FIND_METHOD;

`

To reproduce: ` public class C1 { public static String testOverload(int param) { return "Hello World (int): " + param; } }

public class C2 { public static String testOverload(long param) { return "Hello World (long): " + param; } public static String testOverload(short param) { return "Hello World (short): " + param; } }

import static test.C1.; import static test.C2.;

public class StaticImports { public void execute() { System.out.println(testOverload((short) 1)); System.out.println(testOverload((long) 2)); System.out.println(testOverload((int) 3)); } } `

ihasbini commented 5 years ago

I would like to create a pull request for a fix please. How should I proceed? Thank you.

aunkrig commented 5 years ago

Merged the corresponding PR (#94) manually.

Please re-test!

alarribeau commented 5 years ago

Hi @aunkrig ,

We have discovered another regression related to overloaded static imports that the PR #94 didn't fix. I have created a PR #97 with a test describing the issue and a fix attempt.

Let me know if you need more information! Alex

aunkrig commented 5 years ago

Excellent work... thank you for your PR #97!! Ran all regression tests, work like a breeze.

aunkrig commented 5 years ago

The fix is on the TRUNK - do you want me to prepare a new release?

alarribeau commented 5 years ago

Yes please, a quick release would be neat! It would allow us to catch-up (we are stuck on 3.0.12).

aunkrig commented 5 years ago

Version 3.0.15 just came out - please test and close.

alarribeau commented 5 years ago

It works perfectly @aunkrig , thanks! (I can't close it though, I didn't create it in the first place).

aunkrig commented 5 years ago

Good to hear. Thanks for your support!