title: Get generic field type at runtime via reflection
body: <p>I am having trouble accessing the runtime type of <code>id</code> via reflection from <code>MyClass</code>.</p>
  
  <pre><code>public abstract class AbstractEntity&lt;T&gt; {
  
      @Id
      private T id; //I need to access the runtime class of T
  
  }
  
  public class MyClass extends AbstractEntity&lt;Long&gt; {} 
  </code></pre>
  
  <p>I am using java reflection and have access to the <code>id Field</code> in the superclass.</p>
  
  <p>I tried using:</p>
  
  <ul>
  <li><code>field.getGenericType()</code>: which returns <code>TypeVariableImpl</code> with a value <code>T</code>.</li>
  <li><code>field.getType()</code> which returns <code>Object</code>.</li>
  </ul>
  
  <p>I want to access <code>Long</code> as the field type.  </p>
  
  <p>I am writing a library for others to use and there are a few issues:</p>
  
  <ol>
  <li>I don't actually know if the type is a generic type or a regular type, I only know it is annotated with <code>@id</code>.  </li>
  <li>I don't know where in the class hierarchy this field is located.</li>
  </ol>
  
  <p>Here is the method I am using to get hold of all fields in the class hierarchy.</p>
  
  <pre><code>private Set&lt;Field&gt; getAllFields(final Object object) {
      final Set&lt;Field&gt; fields = new HashSet&lt;&gt;();
      fields.addAll(Arrays.asList(object.getClass().getDeclaredFields()));
      Class&lt;?&gt; superclass = object.getClass().getSuperclass();
      while (superclass != null) {
          fields.addAll(Arrays.asList(superclass.getDeclaredFields()));
          superclass = superclass.getSuperclass();
      }
      return fields;
  }
  </code></pre>
  
  <p>I then filter this list for field annotated with <code>@Id</code> of which there should only be one.</p>