Is it possible to obtain an instance of Class<SomeGenericClass<SomeType>>
?
For example an instance of Class<ArrayList<String>>
without instantiating an instance of ArrayList<String>
.
@SuppressWarnings("unchecked")
public static <T> T castToGeneric(Object objectToCast, T typeInferrenceClass) {
return (T)objectToCast;
}
// usage
Object someObject = null /* initialised elsewhere */;
List<String> listOfStrings = castToGeneric(someObject, new ArrayList<String>());
// I want to avoid having to create - new ArrayList<String>();
I know this code is ugly and I shouldn't be doing this. But we are where we are...