title: Java Reflection calling constructor with primitive types
body: <p>I have a method in my test framework that creates an instance of a class, depending on the parameters passed in:</p>
  
  <pre><code>public void test(Object... constructorArgs) throws Exception {
      Constructor&lt;T&gt; con;
      if (constructorArgs.length &gt; 0) {
          Class&lt;?&gt;[] parameterTypes = new Class&lt;?&gt;[constructorArgs.length];
          for (int i = 0; i &lt; constructorArgs.length; i++) {
              parameterTypes[i] = constructorArgs[i].getClass();  
          }
          con = clazz.getConstructor(parameterTypes);
      } else {
          con = clazz.getConstructor();
      }
  }
  </code></pre>
  
  <p>The problem is, this doesn't work if the constructor has primitive types, as follows:</p>
  
  <pre><code>public Range(String name, int lowerBound, int upperBound) { ... }
  
  .test("a", 1, 3);
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>java.lang.NoSuchMethodException: Range.&lt;init&gt;(java.lang.String, java.lang.Integer, java.lang.Integer)
  </code></pre>
  
  <p>The primitive ints are auto-boxed in to object versions, but how do I get them back for calling the constructor?</p>