I use the Guava TypeToken class in my project, but I get an unexpected result.
I have MyGenericClass<T> :
public class MyGenericClass<T> implements MyInterface { private TypeToken<T> recordType; public MyGenericClass(String name) { this.recordType = new TypeToken<T>(getClass()) {};
So, if I create an object via new MyGenericClass<String>() and then call getRecordType() , I expect to get java.lang.String , instead I get java.lang.Object .
But, if I extend the general class:
public class MyStringImpl extends MyGenericClass<String> {
and create an instance of this new class: new MyStringImpl() , then I get the correct result.
Why is this happening? Is this the expected TypeToken behavior?
source share