Why Does My Generic Method Require a Cast?
<p>I have a service layer method that I want to expose that should return an instance of an object that extends CMSContent (e.g. Delivery Time, Price).  However, in method <strong>getCMSContent</strong>, the compiler is insisting that I do a cast to <strong>T</strong>.  Is this cast below acceptable, or am I defeating the purpose of generics?</p>

<p>I compile with "-Xlint:unchecked" and receive no warnings, and it functions as I expect.</p>

<pre><code>    public &lt;T extends CMSContent&gt; T getCMSContent(String cmsKey, Class&lt;T&gt; clazz) {
        T cmsInstance = (T) CMSObjectCache.getCachedCMSObject(cmsKey, clazz);
        return cmsInstance;
    }
</code></pre>

<p>This is the entire method of getCachedCMSObject</p>

<pre><code>public static &lt;T&gt; T getCachedCMSObject(String objectKey, Class&lt;T&gt; cls) {
    init();
    CMSObject cmsObject = cmsObjectCache.get(objectKey);
    if (cmsObject != null) {
        return cmsObject.getCMSObject(cls);
    }
    return null;
}
</code></pre>
