【发布时间】:2014-05-23 11:21:45
【问题描述】:
我只是想在数据库(ORMLite API)上编写一些通用帮助程序,我使用 Maven 编译它,编译过程失败
invalid inferred types for D; inferred type does not conform to declared bound(s)
[ERROR] inferred: com.j256.ormlite.dao.RuntimeExceptionDao<T,U>
[ERROR] bound(s): com.j256.ormlite.dao.RuntimeExceptionDao<capture#2 of ?,?>
我尝试使用 JDK 1.6 和 1.7 构建它。
public class BaseDbHelper<T,U> extends DatabaseHelper<T, U> {
private Dao<T, U> dao = null;
private RuntimeExceptionDao<T, U> runtimeDao = null;
Class<?> entityClass;
public BaseDbHelper(Context context) {
super(context);
entityClass = (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
@Override
public Dao<T, U> getDao() throws SQLException {
if (dao == null) {
// !!! ERROR
dao = getDao(entityClass);
}
return dao;
}
@Override
public RuntimeExceptionDao<T, U> getDataDao() {
if (runtimeDao == null) {
// !!! ERROR
runtimeDao = getRuntimeExceptionDao(entityClass);
}
return runtimeDao;
}
}
这里是标有错误的行中两个父类的签名
public <D extends com.j256.ormlite.dao.RuntimeExceptionDao<T,?>, T> D getRuntimeExceptionDao(java.lang.Class<T> clazz) { }
public <D extends com.j256.ormlite.dao.Dao<T,?>, T> D getDao(java.lang.Class<T> clazz) throws java.sql.SQLException { }
【问题讨论】:
-
entityClass是Class<?>类型,dao是Dao<T, U>类型,所以getDao的结果将是Dao<?,?>类型,这显然与Dao<T, U>不一致, 因为编译器根本不知道?代表哪种类型。