I have searched for my use case and found some interesting answers but they are not as suitable as i need. What would the appropriate way to do this:
@SuppressWarnings("unchecked")
public <T> T newInstance(String name) throws ClassCastException, InstantiationException, IllegalAccessException, ClassNotFoundException {
return (T) loadClass(name).newInstance();
}
or a little different:
public <T> T newInstance(String name, Class<T> c) throws ClassCastException, InstantiationException, IllegalAccessException, ClassNotFoundException {
return c.cast(loadClass(name).newInstance());
}
I think that both methods do the same. From my point of view method 1 is better because of less parameters. Boths throw a ClassCastException which will be fine for me. Truly, the @SuppressWarnings("unchecked") annotation is not nice.
Can someone tell me if there are any advantages for one method towards the other?
Edit: Jon Skeet's answer is correct. The following snippet can provide additional clarification.
public class Test {
public static void main(String[] args) {
Object o = new Object();
// Exception at c.cast(o)
Test.<String>realCast(o, String.class);
}
private static <T> T realCast(Object o, Class<T> c) {
return c.cast(o);
}
}
Using realCast() produces an Exception when o can't be cast to c. In comparison to that fakeCast() only gives a promise that the method's result is type of T.