title: Passing/returning a class with type parameter to a function
body: <p>Let's assume the following method (say from Guava's Iterables):</p>
  
  <pre><code>public static &lt;T&gt; Iterable&lt;T&gt; filter(final Iterable&lt;?&gt; unfiltered, final Class&lt;T&gt; type) {
      return null;
  }
  </code></pre>
  
  <p>and this collection:</p>
  
  <pre><code>Set&lt;?&gt; objs = ...;
  </code></pre>
  
  <p>then the following code compiles and the generics are correctly derived </p>
  
  <pre><code>Iterable&lt;String&gt; a2 = Iterables.filter(objs, String.class);
  </code></pre>
  
  <p>(In Guava this would return an iterable of all Strings in <code>objs</code>.)</p>
  
  <p>But now lets assume the following class:</p>
  
  <pre><code>static class Abc&lt;E&gt; {
      E someField;
  }
  </code></pre>
  
  <p>I have no idea how to call <code>filter</code> and get <code>Iterable&lt;Abc&lt;?&gt;&gt;</code>: </p>
  
  <pre><code>Iterable&lt;Abc&gt;    a3 = Iterables.filter(objs, Abc.class);
  Iterable&lt;Abc&lt;?&gt;&gt; a4 = Iterables.filter(objs, Abc.class); // Compile error - Abc and Abc&lt;?&gt; are incompatible types
  Iterable&lt;Abc&lt;?&gt;&gt; a5 = Iterables.filter(objs, Abc&lt;?&gt;.class); // Compile error
  Iterable&lt;Abc&lt;?&gt;&gt; a6 = Iterables.&lt;Abc&lt;?&gt;&gt;filter(objs, Abc.class); // Compile error
  Iterable&lt;Abc&lt;?&gt;&gt; a7 = (Iterable&lt;Abc&lt;?&gt;&gt;) Iterables.filter(objs, Abc.class); //  Compile error - inconvertible types
  Iterable&lt;Abc&lt;?&gt;&gt; a8 = Iterables.filter(objs, new Abc&lt;?&gt;().getClass()); // Compile error
  Iterable&lt;Abc&lt;?&gt;&gt; a8a = Iterables.filter(objs, new Abc&lt;Object&gt;().getClass()); // Compile error
  </code></pre>
  
  <p>Only a3 compiles, but then I do not have the parameter on Abc, and thus no generic type checking in subsequent code is done.</p>
  
  <p>I know that the type parameters are not present at runtime and so I do not atempt to write code like:</p>
  
  <pre><code>Iterable&lt;Abc&lt;String&gt;&gt; a9 = Iterables.filter(objs, Abc&lt;String&gt;.class); // Compile error
  </code></pre>
  
  <p>I just want to filter all objects of the type Abc (as a3 does) but having the generic parameter in the result. The only way of doing this I found is the following, which is silly:</p>
  
  <pre><code>Iterable&lt;Abc&lt;?&gt;&gt; a10 = new HashSet&lt;Abc&lt;?&gt;&gt;();
  for (Abc&lt;?&gt; a : Iterables.filter(objs, Abc.class)) {
      ((Set&lt;Abc&lt;?&gt;&gt;)a10).add(a);
  }
  </code></pre>
  
  <p>Thanks.</p>