diamond operator with generic type as returned parameter

378 Views Asked by At

In Java I've found strange method declaration like that:

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

What means this <T> T? What means this <T> before T and why the returned type is not just "T"?

I have download the source code of org.apache.commons.dbutils.BeanProcessor class and found the following method:

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;
}

As you can see the returned bean object has T type not <T> T. Why?

0

There are 0 best solutions below