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,成功!

范式应该在编译的时候就指定,而不是运行时。

时间: 2024-10-06 01:14:55

java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType的相关文章

MySQL分段统计SQL写法 与 Mybatis 报错:java.math.BigDecimal cannot be cast to java.lang.Integer

mysql> select -> sum(case when score<60 then 1 else 0 end) as '<60', -> sum(case when score>=60 and score<=69 then 1 else 0 end) as '60~69', -> sum(case when score>=70 and score<=79 then 1 else 0 end) as '70~79', -> sum(ca

数据转换bug花了半天时间 Java.math.BigDecimal cannot be cast to java.lang.String

从数据库取出一个 Count函数 统计的值 在代码中要转成Integer类型的时候 Integer.parseInt((String)map.get("ID_")) 报了一下错误: Java.math.BigDecimal cannot be cast to java.lang.String 解决方法: Object ob = map.get("ID_");先将数据转成 Integer.parseInt(ob.toString()); http://blog.csd

java.math.BigDecimal cannot be cast to java.lang.String &nbsp; ----解决方案

今天在开发的时候遇到了一个问题,在此记录一下,问题的大概意思就是类型不一致不能进行强转 代码如下: rolf.setFuncd((String) rece.get("FUN_CD"));//强转 rolf.setFunid((String) rece.get("FUN_ID")); //强转 rolf.setFunnm((String) rece.get("FUN_NM")); //强转 因为我第二段的代码Orc数据库的字段类型是number,而

[记录]java.math.biginteger cannot be cast to java.lang.long

可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID",new Scala) java.math.BigInteger cannot be cast to java.lang.Integer java.math.BigInteger cannot be cast to java.lang.Long

报错:java.lang.Long cannot be cast to java.lang.Integer

Long 无法转化成Integer类型. 这个异常 经常出现在hinbernate分页查询的时候. 原因: 这里在Hibernate2.0之前版本list.get(0)返回的是Integer类型. 但是在Hibernate3.0以后版本list.get(0)返回的是Long类型. 解决方法 public Integer getUsersCount() { String hql = "select count(*) from Users"; List list = super.pageQ

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

1.错误描写叙述 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String service.impl.ArrivalBillServiceImpl.exportBillExcel(BillServiceImpl.java:266) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.Na

java.lang.Long cannot be cast to java.lang.Integer解决办法

struts与hibernate整合分页显示时:出现Java.lang.Long cannot be cast to java.lang.Integer 问题时, 原因是:        这里在Hibernate2.0之前版本list.get(0)返回的是Integer类型.         但是在Hibernate3.0以后版本list.get(0)返回的是Long类型.         所以在这里不可以由Long型强转成Integer类型. Integer属于不可更改类型,而且Long和In

使用redis时出现java.util.ArrayList cannot be cast to java.lang.Long

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long at redis.clients.jedis.Connection.getIntegerReply(Connection.java:222) at redis.clients.jedis.Jedis.exists(Jedis.java:127) at redis.clients.jedis.ShardedJedis.exists(S

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map

1.错误描述 java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map at service.impl.BillServiceImpl.exportBillExcel(BillServiceImpl.java:336) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMet