I try to write a generic data loader. Here is the loader logic (code snippet 1 (CS1)):
public class Loader<T> {
private ObjectMapper objectMapper = new ObjectMapper();
public T getData(String testDataPath , Class<T> resultClassType) throws IOException {
return objectMapper.readValue(new File(testDataPath), resultClassType);
}
}
And here is the code snipper where I use the loader (code snippet 2 (CS2)):
String[] stringArray;
//Todo something
stringArray = new Loader<String[]>().getData("<path>", String[].class);
My first question is: if I pass the type information here in CS2: new Loader<String[]>()
why I can't use this generic information here: return objectMapper.readValue(new File(testDataPath), new T().getClass());
?
And at this point I got confused of terms T, Class<> and other type related classes which are allowed to pass as second parameter in the readValue function in objectMapper (Class<>, JavaType, TypeReference, ResolvedType).
So can someone explain me why I can't use the T as I tried and what are the differences between Class<>, JavaType, TypeReference, ResolvedType?
Thx!
>` does NOT preserve `` part (again, type erasure) but only `List` part. To preserve full generic type (including those *inner* generic types) we use fact that fields lets us get full information about its type: https://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list/1942680