title: How do I use generics with an array of Classes?
body: <p>I want to create an array of Classes, each representing a type that is available in the system I'm building.  All the Classes involved are subclasses of a common superclass.  So I'd like to do:</p>
  
  <pre><code>Class&lt;? extends SuperClass&gt;[] availableTypes = { SubClass1.class, SubClass2.class };
  </code></pre>
  
  <p>This gives me the error:</p>
  
  <pre><code>Cannot create a generic array of Class&lt;? extends SuperClass&gt;.
  </code></pre>
  
  <p>I get the same message if I try to qualify the creation of the array on the right hand side of the initialization:</p>
  
  <pre><code>Class&lt;? extends SuperClass&gt;[] availableTypes = Class&lt;? extends SuperClass&gt;[] { SubClass1.class, SubClass2.class };
  </code></pre>
  
  <p>I can get the code to compile if I eliminate the generics qualifications:</p>
  
  <pre><code>Class[] availableTypes = { SubClass1.class, SubClass2.class };
  </code></pre>
  
  <p>But then I get the generics warning:</p>
  
  <p>Class is a raw type.  References to generic type Class should be parameterized.</p>
  
  <p>I'm trying; I'm trying! :)  Also, at this point, even if this didn't provoke a warning, I lose a piece of the interface I was trying to define.  I don't want to just return an array of arbitrary classes; I want to return an array of classes that are all subclasses of a particular SuperClass!</p>
  
  <p>Eclipse has some pretty powerful tools for figuring out what parameters to use to fix generics declarations, but in this case it falls down, as it tends to do when you deal with Class.  The "Infer Generic Type Arguments" procedure it offers doesn't change the code at all, leaving the warning.</p>
  
  <p>I was able to work around this by using a Collection instead:</p>
  
  <pre><code>List&lt;Class&lt;? extends SuperClass&gt;&gt; availableTypes = new List&lt;Class&lt;? extends SuperClass&gt;&gt;();
  </code></pre>
  
  <p>But what's the right way to do this with arrays?</p>