具有泛型类型的菱形运算符作为返回参数

时间:2017-05-23 09:38:30

标签: java diamond-operator

在Java中,我发现了类似的奇怪方法声明:

<T> T org.apache.commons.dbutils.BeanProcessor.toBean(ResultSet rs, Class<T> type) throws SQLException

这个<T> T是什么意思?在<T>之前T的含义是什么,以及为什么返回的类型不仅仅是"T"

我已经下载了org.apache.commons.dbutils.BeanProcessor类的源代码,并找到了以下方法:

private <T> T createBean(ResultSet rs, Class<T> type,
        PropertyDescriptor[] props, int[] columnToProperty)
        throws SQLException {
    T bean = this.newInstance(type);

    for (int i = 1; i < columnToProperty.length; i++) {
        if (columnToProperty[i] == PROPERTY_NOT_FOUND) {
            continue;
        }
        PropertyDescriptor prop = props[columnToProperty[i]];
        Class<?> propType = prop.getPropertyType();

        Object value = null;
        if(propType != null) {
            value = this.processColumn(rs, i, propType);

            if (value == null && propType.isPrimitive()) {
                value = primitiveDefaults.get(propType);
            }
        }
        this.callSetter(bean, prop, value);
    }
    return bean;
}

如您所见,返回的bean对象的T类型不是<T> T。为什么呢?

0 个答案:

没有答案