Java 使用泛型类型调用方法时发生编译错误
这将是一个非常愚蠢的问题,但我无法理解为什么在使用下面的代码时会出现编译错误。我有一个类似的方法Java 使用泛型类型调用方法时发生编译错误,java,oop,generics,jdbc,Java,Oop,Generics,Jdbc,这将是一个非常愚蠢的问题,但我无法理解为什么在使用下面的代码时会出现编译错误。我有一个类似的方法 public <T> List<T> mapGenericEntity(ResultSet resultSet, Class<T> type) throws SQLException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetEx
public <T> List<T> mapGenericEntity(ResultSet resultSet, Class<T> type) throws SQLException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
List<T> entities = new ArrayList<T>();
while(resultSet.next()) {
T instance = type.newInstance();
for(Field field: type.getDeclaredFields()) {
if(excludeFields.indexOf(field.getName()) != -1) {
continue;
}
Object value = resultSet.getObject(field.getName());
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
Method method = propertyDescriptor.getWriteMethod();
method.invoke(instance, value);
}
entities.add(instance);
}
return entities;
}
但我得到的编译错误如下
incompatible types: no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to com.company.Product
不兼容类型:不存在类型变量T的实例,因此java.util.List符合com.company.Product
我错过了什么
编辑:
解决方案
好吧,所以我很愚蠢。MapGenericeEntity方法返回产品对象列表,我将其分配给单个实例。我在做类似的事情
Product-Product=MapGenericeEntity(结果集,Product.class)代码>
但在Intellij中,下面有一条红色下划线
mapGenericEntity(结果集、产品类)代码>
不低于
产品产品
此外,错误信息应该更容易理解。我无法重现此信息。错误具体发生在何时何地?当我使用mvn clean package命令编译此文件时,无法再现错误(Eclipse Mars 4.5.0)。编译错误在我调用该方法的行上。我正在使用java 8,但仍然无法复制。您可能会帮助我们使用一个。当然会这样做,顺便说一句,这个错误只发生在一个特定的方法中,而不会发生在其他方法中。我将尝试调试更多。
incompatible types: no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to com.company.Product