我有這將返回基於類的類型的自定義類型的靜態方法的對象,如何獲得類通用型
public class GenericMethod {
public static <T> T returnGeneric(Class<T> clazz) {
return null;
}
}
現在,我想通過一類具有通用鍵入它,
CustomType<String> type = GenericMethod.returnGeneric(CustomType.class);
唯一的問題是,上述語句給出和未經檢查的轉換警告。
我試着解決方法new CustomType<String>().getName()
這也沒有解決問題。
有沒有正確的方法,或唯一的解決方案是使用@SuppressWarnings
?
可能重複:http://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime? – benzonico