6

I've been playing around with reflections in Java and have come across a fairly difficult problem to solve.

Given just the name of a method and an array of Objects how can I find the best match overload of that method?

I am aware that I can quickly rule out many overloads in most cases by just checking if the length of the parameter list matches the length of my array.
Next I can check if I can cast all objects of the array into the expected types with relfections (I forgot the exact method name, but it can be done).

However I run into an issue when multiple methods match with that procedure.

Let's say I have these two methods:

public void doStuff(Collection<?> data) {
    // Do stuff
}

public void doStuff(List<?> data) {
    // Do stuff
}

If I were to pass have an array with a single Set (or subtype of it) object then I can easily rule out the second overload. Because Sets are not a subinterface/subtype of List.

Having an ArrayList object now makes it hard, because an ArrayList is both a Collection and a List.
I know if I were to use regular code the compiler would certainly use the second method because it's a better/more close match. But how can I do a similar thing with reflections?

I'm interested in all kinds of solutions, although I'd prefer solutions that use the JRE libraries, standalone classes or small (single purpose) libraries.
Also, I'm specifically using Java 8, though if more up to date versions of the language provide simpler methods to do just that, they are still very welcome as answers.

BrainStone
  • 3,028
  • 6
  • 32
  • 59
  • It may be time for you to look into a dynamic/scripting language like Groovy (even just for the implementation of that one method). – ernest_k Oct 27 '19 at 16:56
  • @ernest_k This is not possible with my current setup. – BrainStone Oct 27 '19 at 16:58
  • Have you tried following the same rules as the compiler itself follows, as specified in the [JLS](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.12.2)? – RealSkeptic Oct 27 '19 at 17:00
  • You might be able to find 3rd party libraries for this, but since this is the job of the compiler, trying to do it with reflection is going to be cumbersome. I'm not aware of any in-built functionality, since...well it's not what you usually want when you use reflection. – Kayaman Oct 27 '19 at 17:00
  • @BrainStone Not sure what you mean... You've stated that you're willing to add a simple library. And Groovy scripting can be used as a library in Java projects. With a few lines of code, you can get it to solve your problem with predictable behavior (there's way too much room for error doing it yourself) – ernest_k Oct 27 '19 at 17:01
  • @ernest_k because my project is not suited for scripting. Nor does it make sense to throw the entirety of the Groovy runtime in my project so I can run scripts. The project I would need this in is fairly small Java library. I really aprechiate the suggestion, but it is not suited for my requirements. Feel free to post that suggestion as an answer. I'll be sure to upvote it because it can be good soltion in many other cases. – BrainStone Oct 27 '19 at 17:07
  • @RealSkeptic no I haven't. Seems like a tough read but I'll give it a shot. – BrainStone Oct 27 '19 at 17:09
  • 1
    it's a great question! but it _is_ a duplicate... – Eugene Oct 27 '19 at 18:07
  • 1
    @Eugene I've been googling to solve this issue for ages and never came across that question. Thank you for finding it for me! – BrainStone Oct 27 '19 at 18:59

0 Answers0