Get class of generic

前端 未结 5 1002
野趣味
野趣味 2020-12-16 05:22

My class starts with

public abstract class LastActionHero(){

Now somewhere in the code I want to write H.class<

5条回答
  •  心在旅途
    2020-12-16 05:35

    We do it in the following way:

        private Class persistentClass;
    
        public Class getPersistentClass() {
            if (persistentClass == null) {
                this.persistentClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            }
            return persistentClass;
        }
    

提交回复
热议问题