Java: Generic Return Type, difference between using class parameter vs bracketed call?
<p>I need to interact with a complex legacy class that has nearly all its method arguments and return types as <code>Object</code>.</p>

<p>In my code, I know I will only be dealing with a small slice of functionality, and I know the exact types of all arguments and return types that will be used. In order to make my code more friendly, I'd like to bury any casting that may need to take place and work only with concrete types. I noticed there were two functionally identical ways I could wrap the legacy class method:</p>

<pre><code>public class LegacyClass {
    public Object legacyMethod(Object o) {
        ...
    }
}
</code></pre>



<pre><code>LegacyClass foo = new LegacyClass();

private &lt;T&gt; T legacyMethodWrapper1(String s) {
    return (T) foo.legacyMethod(s); //Warning: unchecked cast
}

private &lt;T&gt; T legacyMethodWrapper2(String s, Class&lt;T&gt; c) {
    return (T) foo.legacyMethod(s); //Warning: unchecked cast
}

public String getLegacyDataString1(String s) {
    return this.&lt;String&gt;legacyMethodWrapper1(s);
}

public String getLegacyDataString2(String s) {
    return this.legacyMethodWrapper2(s, String.class);
}
</code></pre>

<p>While there doesn't seem to be any functional difference in the simple code shown here, I was curious if I was missing some other subtle difference between <code>legacyMethodWrapper1</code> and <code>legacyMethodWrapper2</code>, other than the fact that <code>legacyMethodWrapper2</code> has access to the additional <code>Class</code> object, and could potentially do some more complex operations other than just casting?</p>

<p>Edit: To be clear, one of the goals of wrapping these legacy methods is 1) provide type safety to the <em>caller</em> and 2) prevent the caller from seeing meaningless warnings. Because I know more about the poorly designed legacy class than the compiler can ever know, I can provide a 100% type safe interface and hide any meaningless compiler warnings.</p>