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


DAO層有很多操作其實可以用范式加反射來實現的,所以我就開始構想我的萬金油BaseDAO了。

BaseHibernateDaoAdvance.java
public class BaseHibernateDaoAdvance<T extends ModifyInfoEntity, PK extends Serializable, DTO extends BaseDto>
        extends HibernateDaoSupport implements BaseDaoAdvance<T, PK, DTO> {
    
    private final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    private void getSessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }
    
    // 實體類類型(由構造方法自動賦值)
    private Class<T> entityClass;
    private Class<DTO> dtoClass;
    // 構造方法,根據實例類自動獲取實體類類型
    public BaseHibernateDaoAdvance() {
        System.out.println("-------------->in BaseHibernateDaoAdvance");
        
        this.entityClass = null;
        Class c = getClass();
        System.out.println(c);
        Type t = c.getGenericSuperclass();
        System.out.println(t);
        if (t instanceof ParameterizedType) {
            System.out.println("in if");
            Type[] p = ((ParameterizedType) t).getActualTypeArguments();
            System.out.println(Arrays.toString(p));
            this.entityClass = (Class<T>) p[0];
            this.dtoClass = (Class<DTO>) p[2];
        }
    }
}

 

寫好,准備些測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/config/applicationContext.xml",
"classpath:/config/service.xml","classpath:/config/dao.xml" })
public class BaseDAOImplAdvanceTest {
    
    
    private BaseDaoAdvance<Course, Serializable, CourseDTO> baseDAOImpl;
    
    
    public BaseDaoAdvance<Course, Serializable, CourseDTO> getBaseDAOImpl() {
        return baseDAOImpl;
    }

    @Autowired
    public void setBaseDAOImpl(
            BaseDaoAdvance<Course, Serializable, CourseDTO> baseDAOImpl) {
        this.baseDAOImpl = baseDAOImpl;
    }


    @Test
    public void queryList(){
        CourseDTO dto = new CourseDTO();
        baseDAOImpl.queryList(dto);
        System.out.println("here-------------------->");
    }
}

 

但是提示錯誤java.lang.NullPointerException

了解一下代碼,發現應該是這一段判斷不通過

        if (t instanceof ParameterizedType) {
            System.out.println("in if");
            Type[] p = ((ParameterizedType) t).getActualTypeArguments();
            System.out.println(Arrays.toString(p));
            this.entityClass = (Class<T>) p[0];
            this.dtoClass = (Class<DTO>) p[2];
        }

 去掉if測試一下,還是報錯,java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

~~~~(>_<)~~~~

其實原因是因為范式的使用方式不對。

增加了具體的DAO和實現

public interface CourseDao extends BaseDaoAdvance<Course, Serializable,CourseDTO> {

}
@Repository
public class CourseDaoImpl extends BaseHibernateDaoAdvance<Course, Serializable,CourseDTO> implements CourseDao {

}

測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/config/applicationContext.xml",
"classpath:/config/service.xml","classpath:/config/dao.xml" })
public class CourseDaoImplTest {
//    private BaseDaoAdvance<Course, Serializable, CourseDTO> baseDAOImpl;
    @Autowired
    private CourseDao courseDao;

    @Test
    public void queryList(){

        CourseDTO dto = new CourseDTO();
        courseDao.queryList(dto);
        System.out.println("here-------------------->");
    }
}

OK,成功!

范式應該在編譯的時候就指定,而不是運行時。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM