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

method reflection does not handle overloading resolution #55

Open
jason-s opened this issue May 20, 2011 · 3 comments
Open

method reflection does not handle overloading resolution #55

jason-s opened this issue May 20, 2011 · 3 comments

Comments

@jason-s
Copy link

jason-s commented May 20, 2011

This is a major feature missing from both the Java reflection API and the present state of Mirror, IMHO, and is related to issue #44.

If you think, yeah, well, real classes don't have overloaded methods or constructors, look at java.io.File and java.io.FileInputStream.

Use case: you're running an interpreter and you want to allow dynamic method calls at runtime where the object and argument types are not known in advance.

Handling this is not easy and there are quite a few corner cases (at least if you're trying to make overloading resolution match the compile-time overloading resolution in JLS section 15.12 ).

I should be able to help w/ this (as soon as I get permission from our company's legal dept, we've done open-source contributions before so it shouldn't take too long)

@jonasabreu
Copy link
Member

Hi jason-s,

issue #44 was related to an easier way to reflect a method when there is only one method with that name defined. You can easily reflect any overloaded method using

Method m = new Mirror().on(clazz).reflect().method("methodName").withArgs(Param1.class, Param2.class);

Does this fix your problem?

@jason-s
Copy link
Author

jason-s commented May 20, 2011

Does this fix your problem?

Nope. That assumes you know the exact classes of the declared method parameters. (e.g. as in Method.getParameterTypes)

All I have is the classes of the actual arguments. (from calling someArgument.getClass())

Just for example, let's say you have

public int compute(Object a0, Object a1) { return 1; }
protected int compute(String a0, Object a1) { return 2; }
public int compute(Object obj, String s) { return 3; }

If your arguments are "hi" and "there", then all three methods are applicable. Only the first and last are accessible. The third method is the most specific method that is applicable and accessible, so it's the one that should be invoked, in order to match the compile-time overload resolution. (all italicized terms in this paragraph are per the JLS)

(see my question on StackOverflow)

@jonasabreu
Copy link
Member

Ok. Now I get it.

If you need the method definition, at this moment Mirror can't reflect it, but internally we have code that look for the best match method (but it probably does not follow the compile-time overloading resolution).

That's probably a good point to improve according to the JLS section 15.12. Then we expose this behavior on the DSL.

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