title: How to get class of generic type when there is no parameter of it?
body: <p>I just learned about this <a href="https://stackoverflow.com/a/306773/483113">fine looking syntax</a></p>
  
  <pre><code>Collections.&lt;String&gt;emptyList()
  </code></pre>
  
  <p>to get an empty <code>List</code> with elements which are supposedly of type <code>String</code>. Java's source looks like this:</p>
  
  <pre><code>public static final List EMPTY_LIST = new EmptyList&lt;Object&gt;();
  :
  public static final &lt;T&gt; List&lt;T&gt; emptyList() {
    return (List&lt;T&gt;) EMPTY_LIST;
  }
  </code></pre>
  
  <p>Now if I code a method in that way where the generic type does not appear in the parameter list, is there any way how I can access the actual class that becomes <code>T</code>?</p>
  
  <p>I'm saying, up to now my approach to code the same thing would have been</p>
  
  <pre><code>private &lt;T&gt; T get(String key, Class&lt;T&gt; clazz) {
    // here I can do whatever I want with clazz, e.g.:
    return clazz.cast(value);
  }
  </code></pre>
  
  <p>If I removed the <code>clazz</code>-parameter I wouldn't be able to do the <code>cast()</code>. Obviously I could do</p>
  
  <pre><code>  return (T) value;
  </code></pre>
  
  <p>but that gives me the usual warning <code>Type safety: Unchecked cast from Object to T</code>. Ok, <code>@SuppressWarnings("unchecked")</code> helps here, but actually I want to do something with the intended return type of the method. If I add a local variable</p>
  
  <pre><code>T retValue;
  </code></pre>
  
  <p>I'd have to initialise it with something, <code>null</code> doesn't help. After I assign it like</p>
  
  <pre><code>@SuppressWarnings("unchecked")
  T retValue = (T) value;
  </code></pre>
  
  <p>I could do, e.g.</p>
  
  <pre><code>retValue.getClass().getName()
  </code></pre>
  
  <p>but if the cast fails I end up with no information about <code>T</code> again.</p>
  
  <p>Since Java (or at least my Java 6) does not have the generic info any more during runtime, I currently can't think of a way to do this. Is there a way? Or do I have to stick with my "old" approach here?</p>
  
  <p><em>Please note that the example I lined out is very simple and doesn't make much sense. I want to do more complicated stuff here, but that's out of the scope.</em></p>