title: How to get Class<T> from List<T>
body: <p>I feel utterly silly for having to ask this, but I'm failing to understand why doesn't the following Java code compile:</p>
  <pre><code>void &lt;T&gt; doSomething(List&lt;T&gt; items) {
      Class&lt;? extends T&gt; clazz = items.get(0).getClass();
      ...
  }
  </code></pre>
  <p>From Java doc:</p>
  <blockquote>
  <p>The actual result type is Class&lt; ? extends |X|&gt; where |X| is the
  erasure of the static type of the expression on which getClass is
  called. For example, no cast is required in this code fragment:</p>
  <p>Number n = 0;  Class&lt; ? extends Number&gt; c = n.getClass();</p>
  </blockquote>
  <p>EDIT:</p>
  <ol>
  <li><p>Found <a href="https://stackoverflow.com/questions/19332856/what-is-meant-by-the-erasure-of-the-static-type-of-the-expression-on-which-it-i?rq=1">this nice explanation</a> of what <em>erasure of the static type</em> means.</p>
  </li>
  <li><p>There's a way to preserve generic type information using subclasses, known as <a href="http://gafter.blogspot.nl/2006/12/super-type-tokens.html?m=1" rel="nofollow noreferrer">super type token</a>. A now deleted answer helpfully pointed out that Guava library has a <a href="https://github.com/google/guava/wiki/ReflectionExplained" rel="nofollow noreferrer">convenient utility</a> for exploiting this.</p>
  </li>
  <li><p>Here's a <a href="https://www.javacodegeeks.com/2013/12/advanced-java-generics-retreiving-generic-type-arguments.html" rel="nofollow noreferrer">great article</a> on extracting generic types reflectively and a nice lib, called <a href="https://github.com/coekie/gentyref" rel="nofollow noreferrer">GenTyRef</a>, simplifying it</p>
  </li>
  <li><p>I forked GenTyRef to add support for working with <code>AnnotatedType</code>s (introduced in Java 8). It's called <a href="https://github.com/leangen/geantyref" rel="nofollow noreferrer">GeantyRef</a> (and it's in Maven Central)</p>
  </li>
  </ol>