Reflection to access values that are nested classes
<p>I know it is bad code design, but as a temporary hack...</p>

<p>I need to access a private map where the values are initializations of a static nested class. In the following example, I want to access each value of myMap from a different package.</p>

<pre><code>package belongs.to.someone.else

public class SOExample {
    private Map&lt;String, NestedClass&gt; myMap;

    static class NestedClass {
        final int data;
        NestedClass(final int data) {
            this.data = data;
        }
    }

    public void populateMyMap(){
        for(int i=0; i&lt;100;  i++){
            this.myMap.put(Integer.toString(i), new NestedClass(i));
        }
    }
}
</code></pre>

<p>But I seem to run into a chicken and egg problem when trying to set the SOExample.myMap field to accessible. I get "cannot be accessed from outside of package" error for the <code>SOExample.NestedClass</code> values in the last statement.</p>

<pre><code>package belongs.to.me
public class SOExampleMyPackage {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        SOExample example = new SOExample();
        example.populateMyMap();

        // Make the example.myMap field accessible
        Field f = example.getClass().getDeclaredField("myMap");
        f.setAccessible(true);
        // Next line throws error
        Map&lt;String, SOExample.NestedClass&gt; myMapHere = (Map&lt;String, SOExample.NestedClass&gt;) f.get(example);
    }
}
</code></pre>

<p>I appreciate any ideas about how to solve this problem.</p>