Convert from Class<T> to TypeReference<T>

class, generics, jackson, java

Solution

You can override `getType()` to return your type:

new TypeReference<T>(){
    @Override
    public Type getType() {
        return valueType;
    }
}; 

Problem

Basically just the opposite of this question. I want to convert a Class<T> object to a TypeReference<T> object. I have tried: ``` foo(Class<T> valueType) { TypeReference ref = new TypeReference<T>(){}; } ``` but that just returns a type reference of the classes's super class. I also tried: ``` foo(Class<T> valueType) { TypeReference ref = new TypeReference<valueType>(){}; } ``` and ``` foo(Class<T> valueType) { TypeReference ref = new TypeReference<valueType.getRawClass()>(){}; } ``` But the second two don't compile. How do I do this?

Original source