Java: Mixing Generics and VarArgs
<p>I have the following Interface that defines a certain type</p>

<pre><code>public interface BaseInterface {
}
</code></pre>

<p>This interface will be used to implement a couple of enumerations, as in:</p>

<pre><code>public enum First implements BaseInterface {
  A1, B1, C1;
}


public enum Second implements BaseInterface {
  A2, B2, C2;
}
</code></pre>

<p>I'd now like a small reusable method that works a bit like Enum.valueOf(String), the idea being I supply the name of a constant and all the possible types that could implement it. It will return the enumeration object that implements the interface (I don't need to worry about the possibility of two enumeration constants having the same name). The client code will look something like:</p>

<pre><code>BaseInterface myConstant = parse("A1", First.class, Second.class);
</code></pre>

<p>Where I'm stuck is the definition of the method. I was thinking about something along the lines of:</p>

<pre><code>@SafeVarargs
private final &lt;T extends Enum&lt;T&gt; &amp; BaseInterface &gt; T parse(String name, Class&lt;T&gt;... types) {
    // add code here
}
</code></pre>

<p>However the Java compiler complains about the definition of <code>types</code>. It will only allow me to pass one unique type! The following are valid:</p>

<pre><code>parse("A1");
parse("A1", First.class);
parse("A1", First.class, First.class);
parse("A1", First.class, First.class, First.class);
parse("A1", Second.class);
parse("A1", Second.class, Second.class);
</code></pre>

<p>But the useful version is not:</p>

<pre><code>parse("A1", First.class, Second.class);
</code></pre>

<p>How do I tell Java the <code>types</code> can take <em>ALL</em> classes that extends Enum and implements BaseInterface?</p>