This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Sadly, the only way to get rid of that warning is by adding a @SuppressWarnings annotation to the method.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I don't think there's anything sad about it. Ultimately, you get a warning because within that method, there's really no way to know that the String argument passed in will be compatible with the type T which must be inferred from the return type. If someone calls this method with
String x = getClassInstance("java.lang.Integer");
it will fail, period. Whereas
String x = getClassInstance("java.lang.String");
works fine. Only the calling code can determine if this will work; thus, it's in the calling code that the decision should be made as to whether to cast or not. Best to just drop the generic type T, and declare the method to return an Object. That's the only thing you can guarantee from within the method. Let the caller decide whether a cast is appropriate.