title: Type safety warning when casting to generic type
body: <p>(<em>When researching this, all I found were SO posts and websites saying you cannot simply cast to a generic type due to type erasure, I found nothing on point.</em>)</p>
  
  <p>Can someone explain to me why Eclipse prompts me with this warning?</p>
  
  <pre><code>public &lt;T&gt; T getFromBag(String key, Class&lt;T&gt; clazz) {
          Object val = null;
          T typeCastValue = null;
  
          if (commonDataBag.containsKey(key)) {
              val = commonDataBag.get(key);
              typeCastValue = clazz.cast(val);
          }
          return typeCastValue;
      }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>Map&lt;String, Foo&gt; stuff= myService().&lt;Map&gt;getFromBag(key, Map.class);
  </code></pre>
  
  <p>Warning:</p>
  
  <pre><code>Type safety: The expression of type Map needs unchecked conversion to conform to Map&lt;String,Foo&gt;
  </code></pre>