Reputation: 2436
I'm attempting to use Spring's org.springframework.core.ResolvableType
to figure out the parameterized type at runtime as such:
public class MyClass<T> implements MyInterface<T> {
private final Class<T> typeClass;
public CustomStateSerializer() {
ResolvableType type = ResolvableType.forClass(getClass());
ResolvableType genericType = type.getGeneric();
this.typeClass= (Class<T>) genericType.resolve();
}
}
...
new MyClass<MyType>();
Unfortunately, genericType
results to ?
. Clearly I'm not using it correctly and I can't seem to find any good docs for the solution.
Upvotes: 3
Views: 5172
Reputation: 622
default Class<T> getEntityClass() {
return (Class<T>) ResolvableType.forClass(getClass())
.as(VersionedRepository.class)
.getGeneric(0)
.resolve();
}
This would work
Upvotes: 0
Reputation: 6302
The class is only has reference to which is not defined yet. So you can pass the instance that will have an specific already:
ResolvableType type = ResolvableType.forClass(getClass(), this);
this.typeClass = type.getGeneric(0).resolve();
Upvotes: -1