java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

后端 未结 2 1363
温柔的废话
温柔的废话 2021-01-13 05:53

I have created hierarchy of interface and classes using generic and messed up everything.
Topmost class is AbstractJpaEntity which is extended by all domain entity

2条回答
  •  一生所求
    2021-01-13 06:16

    This is a solution

    public TO getTo() throws Exception {
        if(to == null) {
            try {
                to = ((Class) ((ParameterizedType) this.getClass().getGenericSuperclass())
                        .getActualTypeArguments()[0]).newInstance();
            } catch(ClassCastException cce) {
                cce.printStackTrace();
                to = ((Class) ((ParameterizedType) (((Class) this.getClass()
                        .getAnnotatedSuperclass().getType()).getGenericSuperclass()))
                                .getActualTypeArguments()[0]).newInstance();
            }
        }
        return to;
    }
    

提交回复
热议问题