title: Converting Integer to Long
body: <p>I need to get the value of a field using reflection. It so happens that I am not always sure what the datatype of the field is. For that, and to avoid some code duplication I have created the following method:</p>
  
  <pre><code>@SuppressWarnings("unchecked")
  private static &lt;T&gt; T getValueByReflection(VarInfo var, Class&lt;?&gt; classUnderTest, Object runtimeInstance) throws Throwable {
    Field f = classUnderTest.getDeclaredField(processFieldName(var));
    f.setAccessible(true);
    T value = (T) f.get(runtimeInstance);
  
    return value;
  }
  </code></pre>
  
  <p>And use this method like: </p>
  
  <pre><code>Long value1 = getValueByReflection(inv.var1(), classUnderTest, runtimeInstance);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Double[] value2 = getValueByReflection(inv.var2(), classUnderTest, runtimeInstance);
  </code></pre>
  
  <p>The problem is that I can't seem to cast <code>Integer</code> to <code>Long</code>:</p>
  
  <pre><code>java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
  </code></pre>
  
  <p>Is there a better way to achieve this?</p>
  
  <p>I am using Java 1.6.</p>