I think that the first method should look something like this:
public static <T> T create(final String className, Class<T> ifaceClass)
throws ClassNotFoundException {
final Class<T> clazz = Class.forName(className).asSubclass(ifaceClass);
return create(clazz);
}
You cannot do an up-cast typecast using a type parameter ... without those pesky type-safety warnings.
By the way, if you ignore those warnings, the create method may create an instance of some class that isn't compatible with the actual type used by the caller. This is likely to lead to an unexpected ClassCastException later on; e.g. when the instance is assigned.
EDIT: @Pascal points out that we need to add a typecast to make this compile; i.e.
Class<T> clazz = (Class<T>) Class.forName(className).asSubclass(ifaceClass);
Unfortunately, we also need to add a @SuppressWarnings annotation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…