title: accessing private inner class field using reflection
body: <p>I am wondering how I can access a private field of a private inner class using reflection for my JUnit tests.</p>
  
  <p>Here is an example of what I need:</p>
  
  <pre><code>public class A
  {
      private Map&lt;Integer, InnerClass&gt; objectMap = new TreeMap&lt;Integer, InnerClass&gt;.descendingMap();
  
      private class InnerClass    
      {
          private int fieldOne
          private int fieldTwo
      }
  }
  </code></pre>
  
  <p>In this example, I want to access the values of fieldOne and fieldTwo that are in the instance of A.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>A instanceA = new A(); 
  
  Field privateObjectMapField = A.class.getDeclaredField("objectMap");
  privateObjectMapField.setAccessible(true);
  NavigableMap&lt;Integer, Class&lt;?&gt;&gt; fieldValue =  (NavigableMap&lt;Integer, Class&lt;?&gt;&gt;) privateObjectMapField.get(instanceA);
  
  // 5 is a key in the map so this should set declaredClass to an instance of InnerClass
  Class&lt;?&gt; declaredClass = fieldValue.get(5).getClass();
  Field fieldOne = declaredClass.getDeclaredField("fieldOne");
  fieldOne.setAccessible(true);
  Assert.assertEquals(100, fieldOne.getInt(declaredClass));
  </code></pre>
  
  <p>In this case it crashses at: <code>Class&lt;?&gt; declaredClass = fieldValue.get(5).getClass();</code>
  with the error: </p>
  
  <p><code>java.lang.ClassCastException: com.test.A$InnerClass cannot be cast to java.lang.Class</code></p>
  
  <p>Does anyone know what is wrong here?</p>
  
  <p>I hope this example is clear.</p>
  
  <p>Thanks</p>