title: Why is this an unchecked cast? And how to fix it?
body: <p>I have the following code:</p>
  
  <pre><code>public final &lt;T extends Component&gt; T getComponent(Class&lt;T&gt; type) {
      Iterator&lt;Component&gt; it = components.iterator();
  
      while(it.hasNext()) {
          Component next = it.next();
  
          if(type.isAssignableFrom(next.getClass()))
              return (T)next; // Why is this an unchecked cast from Component to T?
      }
  
      return null;
  }
  </code></pre>
  
  <p>And for some reason, <code>return (T)next;</code> is an unchecked cast from Component to T.</p>
  
  <p>I'm not sure as to why this is, because T has to extend Component, it should be able to safely cast it to any subclasses of Component, right?</p>
  
  <p>If I manually do <code>return (TestComponent)next;</code> and change the return type to TestComponent it works fine, so why doesn't it work with T?</p>