title: How to find an overloaded method in Java?
body: <p>When writing something like</p>
  
  <pre><code>doit(43, 44, "hello");
  </code></pre>
  
  <p>the compiler knows which overloaded method is to be called. When I want to do the same via reflection, I need to find out myself, that the method is</p>
  
  <pre><code>doit(Integer, double, CharSequence...);
  </code></pre>
  
  <p>and obtain it via something like</p>
  
  <pre><code>Class[] types = {Integer.class, double.class, CharSequence[].class};
  declaringClass.getDeclaredMethod("doit", types);
  </code></pre>
  
  <p>I wonder if there's already something allowing me to write just</p>
  
  <pre><code>Method m = getMethod(declaringClass, "doit", 43, 44, "hello");
  </code></pre>
  
  <p>I wonder if somebody did this already, as the <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12" rel="noreferrer">JLS</a> is a bit complicated in this respect.</p>
  
  <hr>
  
  <p>Actually, behaving <em>exactly</em> like the compiler is impossible as in <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.2" rel="noreferrer">Phase 1</a> the compiler accepts only methods matching without boxing and unboxing. When calling my hypothetical <code>getMethod</code> from above, the distinction between primitives and their wrappers is already lost (because of autoboxing when passing arguments via varargs). This problem seems to have no solution, so let's ignore it.</p>
  
  <p>As suggested in an answer, <code>BeanUtils.invokeMethod</code> comes close. It's supposed to find the best match, whatever it means. Looking at <a href="http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/MethodUtils.java?view=markup" rel="noreferrer">MethodUtils</a><code>.getMatchingAccessibleMethod</code> shows that</p>
  
  <ul>
  <li>it knows nothing about varargs</li>
  <li>it's non-deterministic</li>
  </ul>
  
  <p>so I'm looking for something better.</p>