title: In Java, invoke method using reflection and without giving invoker object type and multiple parameters type?
body: <p>My question is, how to invoke a method using reflection, with giving invoker object and <strong>multiple</strong> parameters, <strong>without giving invoker object type and parameters type and without calling API.</strong></p>
  
  <p>That is </p>
  
  <pre><code>Student student = new Student();
  student.setNameAndClass("John", "1D"); // Method 0
  </code></pre>
  
  <p>can be replaced by</p>
  
  <pre><code>MyMethodUtils.invoke(student, "setNameAndClass", Object[] {"John", "1D"}); // Method 1
  </code></pre>
  
  <p>where the <code>invoke()</code> method is made by JDK methods.
  It can be in other signature if it fulfill the requirement.</p>
  
  <h2>My research and study</h2>
  
  <p>I cannot find any related solution in StackOverflow.
  <a href="https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string">This answer need class list</a>
  <a href="https://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters">This looks pretty close but it is C# and calling API</a></p>
  
  <p>I am able to make the <code>invoke()</code> method like this.</p>
  
  <pre><code>MyMethodUtils.invoke(student, "getNameAndClass"); // Method 2 (no parameters)
  MyMethodUtils.invoke(student, "setNameAndClass", Object[] {"John", "1D"},
  Class[] {String.class, String.class}); // Method 3 (with multiple parameters but inconvenient to use)
  </code></pre>
  
  <p>Method 2, 3 works properly. However, when I tried to implement the Method 1 (what I want), it is working only when the type is exact match.</p>
  
  <p>For example, if the method is like</p>
  
  <pre><code>setMarks(String name, Collection&lt;Integer&gt; marks);
  </code></pre>
  
  <p>native invoking works but reflection don't</p>
  
  <pre><code>student.setMarks("John", new ArrayList&lt;Integer&gt;()); //works
  MyMethodUtils.invoke(student, "setMarks", "John", new ArrayList&lt;Integer&gt;()); // getMethod() method always cannot find a proper method
  </code></pre>
  
  <p>Is it impossible? Or is there ways to implement so?</p>