title: Generic Class with Static Nested Class of the Same Type
body: <p>I am trying to write a generic configuration loader which supports loading into any target data structure by means of using a helper object supplied by the caller.  The helper is used to divorce the loader from any knowledge of the data structure, with methods to pre-process member values, create substructures and add/remove members from the structures.</p>
  
  <p>Given the class and variable declaration:</p>
  
  <pre><code>public class ConfigLoader&lt;T extends Object&gt;
  ...
  private final Class&lt;T&gt;                  stcClass;
  </code></pre>
  
  <p>and the static nested interface:</p>
  
  <pre><code>static public interface Helper&lt;T&gt;
  {
  public Object configValue(String qulfld                , String loc);
  public Object configValue(String qulfld, T          val, String loc);
  public Object configValue(String qulfld, String     val, String loc);
  public Object configValue(String qulfld, Boolean    val, String loc);
  public Object configValue(String qulfld, Number     val, String loc);
  
  public T      crtObject();
  public void   addMember(T tgt, String fld, Object val);
  public void   rmvMember(T tgt, String fld);
  }
  </code></pre>
  
  <p>and the base constructor:</p>
  
  <pre><code>private ConfigLoader(JsonParser psr, Helper&lt;T&gt; hlp, DataStruct vld) {
      super();
  
      parser      =psr;
      helper      =hlp;
      stcClass    =helper.crtObject().getClass(); // &lt;== error here
      validation  =vld;
      errors      =new ArrayList&lt;Fail&gt;();
      }
  </code></pre>
  
  <p>I am getting a compiler error in the constructor line indicated:</p>
  
  <pre><code>ConfigLoader.java:79: error: incompatible types
      stcClass    =helper.crtObject().getClass();
                                              ^
    required: Class&lt;T&gt;
    found:    Class&lt;CAP#1&gt;
    where T is a type-variable:
      T extends Object declared in class ConfigLoader
    where CAP#1 is a fresh type-variable:
      CAP#1 extends Object from capture of ? extends Object
  1 error
  </code></pre>
  
  <p>The intent is to create one dummy structure up front from which to extract the <code>Class&lt;T&gt;</code> object, which is then used in some other code to validate the structure type and then to invoke the helper visitor method with the <code>T</code> argument using <code>helper.configValue(qulnam,stcClass.cast(val),loc.toString())</code>.</p>
  
  <p>What I can't figure out is why the compiler can't validate that the return of the helper's <code>crtObject</code> method is guaranteed to be in fact a <code>T</code> object since the helper passed to the constructor is itself a <code>Helper&lt;T&gt;</code>.</p>
  
  <p>The only alternative I am seeing is to pass in a <code>Class&lt;T&gt;</code> as a constructor argument.</p>
  
  <p>Any help would be appreciated.</p>