title: Generic Casting on primitive value causing ClassCastException
body: <p>I have a method below doing casting on a String according to the given type, assuming the String given must be correct.</p>
  
  <pre><code>private static &lt;T&gt; T parsePrimitive(final Class&lt;T&gt; primitiveType, final String primitiveValue) {
      if (primitiveType.equals(int.class) || primitiveType.equals(Integer.class)) {
          return primitiveType.cast(Integer.parseInt(primitiveValue));
      }
      /*
      ...
      for the rest of the primitive type
      ...
      */
  }
  </code></pre>
  
  <p>However, when I call <code>parsePrimitive(int.class, "10");</code>,</p>
  
  <blockquote>
    <p>primitiveType.cast(Integer.parseInt(primitiveValue));</p>
  </blockquote>
  
  <p>This causes <code>ClassCastException</code>, any idea for this?</p>
  
  <p>p.s. In fact, when I use Object as the return type, and no casting in before return, it works fine outside the method, but this is not generic enough I think.</p>
  
  <p>Thanks in advance for any help.</p>