title: Generic Enum in a Collection or vararg parameter
body: <p>I'm trying to create a collection of <code>enum</code> classes so that I can iterate through the collection and then iterate through the enum values by calling <code>enumClass.getEnumConstants()</code>.</p>
  
  <p>Given the following:</p>
  
  <pre><code>public interface MyEnumType {
     String getSubtype ();
  }
  
  public enum MyEnumTypeA implements MyEnumType {
     A_1,
     A_2;
     public String getSubtype () { return "A"; }
  }
  
  public enum MyEnumTypeB implements MyEnumType {
     B_1,
     B_2;
     public String getSubtype () { return "B"; }
  }
  </code></pre>
  
  <p>I've tried using varargs:</p>
  
  <pre><code>private &lt;T extends Enum&lt;T&gt; &amp; MyEnumType&gt; void handleTypes (
     Class&lt;? extends T&gt;... enumTypes )
  {
     for( Class&lt;? extends T&gt; enumType : enumTypes )
        for( T value : enumType.getEnumConstants() )
           handleValue( value );
  }
  
  private void doStuff () {
     // error here
     handleTypes( MyEnumTypeA.class, MyEnumTypeB.class );
  }
  </code></pre>
  
  <p>...but using wild-card generics in varargs produces a compilation error whenever I try to call the function</p>
  
  <p>I've also tried building a <code>List</code>:</p>
  
  <pre><code>public class MyMainClass
  {
     private static Collection&lt;Class&lt;Enum&lt;? extends MyEnumType&gt;&gt;&gt; ALL_TYPES
        = Lists.newArrayList();
     {
        // error here
        ALL_TYPES.add( MyEnumTypeA.class );
        ALL_TYPES.add( MyEnumTypeB.class );
     }
  }
  </code></pre>
  
  <p>...but this too fails to compile when I try to <code>add</code> to the list.</p>
  
  <p>The following works but requires a cast (no compilation errors or warnings if I passed in <code>Object.class</code>):</p>
  
  <pre><code>private void runNoMatchTests ( Class... enumTypes ) {
     for( Class enumType : enumTypes ) {
        for( MyEnumType value : ( (Class&lt;MyEnumType&gt;)enumType ).getEnumConstants() ) {
           handleValue( value );
        }
     }
  }
  </code></pre>
  
  <p>How can this be done without using a cast? Can it be done without using a cast?</p>