Get generic type of class at runtime

后端 未结 26 2518
野的像风
野的像风 2020-11-21 04:40

How can I achieve this?

public class GenericClass
{
    public Type getMyType()
    {
        //How do I return the type of T?
    }
}
26条回答
  •  太阳男子
    2020-11-21 05:07

    I have seen something like this

    private Class persistentClass;
    
    public Constructor() {
        this.persistentClass = (Class) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
     }
    

    in the hibernate GenericDataAccessObjects Example

提交回复
热议问题