title: Get all fields (even private and inherited) from class
body: <p>I am making university project.<br>
  I need to get all fields from class. Even private and inherited. I tried to get all declared fields and then cast to super class and repeat.
  Fragment of my code:</p>
  
  <pre><code>private void listAllFields(Object obj) {
      List&lt;Field&gt; fieldList = new ArrayList&lt;Field&gt;();
      while (obj != null) {
          fieldList.addAll(Arrays.asList(obj.getClass().getDeclaredFields()));
          obj = obj.getClass().getSuperclass().cast(obj);
      }
      // rest of code
  </code></pre>
  
  <p>But it does not work. <code>tmpObj</code> after casting is still the same class (not superclass).<br>
  I will appreciate any help how to fix casting problem, or how to retrieve these fields in different way. </p>
  
  <p>Problem is not to gain access to fields, but to get names of fields!<br>
  I manages it that way:</p>
  
  <pre><code>private void listAllFields(Object obj) {
      List&lt;Field&gt; fieldList = new ArrayList&lt;Field&gt;();
      Class tmpClass = obj.getClass();
      while (tmpClass != null) {
          fieldList.addAll(Arrays.asList(tmpClass .getDeclaredFields()));
          tmpClass = tmpClass .getSuperclass();
      }
      // rest of code
  </code></pre>