简体   繁体   中英

Parametrizing raw type in Java

I use Eclipse IDe and Apache Commons DBUtils library and I have some own row processor classes extended from BasicRowProcessor class . In this classes I am overriding these two DButils' methods:

public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException

and

public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException

My implementation of toBeanList method is as follows:

   @Override
    public LinkedList<Object> toBeanList(ResultSet rs, Class clazz) {
        try {
            LinkedList<Object> newlist = new LinkedList<Object>();
            while (rs.next()) {
                newlist.add(toBean(rs, clazz));
            }
            return newlist;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

I loop through a ResultSet and call toBean method for its all records. It works, but Eclipse reports some problem (probably related to Java generic), please see this screenshot:

在此输入图像描述

When I set parametrize clazz argument as Class<TaskItem> clazz Eclipse reports another problem and it doesn't override method in upper class BasicRowProcessor .

在此输入图像描述

How can I solve this issue? What am I doing wrong? Or is it safe to ignore this warning? Probably it will be very easy to solve this, but I haven't found a solution.

Edit:

I tried to parametrize my two methods but Eclipse reports that I need to override super type.

toBeanList method:

@Override
    public LinkedList<TaskItem> toBeanList(ResultSet rs, Class<TaskItem> clazz) {
        try {
            LinkedList<TaskItem> newlist = new LinkedList<TaskItem>();
            while (rs.next()) {
                newlist.add(toBean(rs, clazz));
            }
            return newlist;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

And toBean method:

@Override
    public TaskItem toBean(ResultSet rs, Class<TaskItem> clazz) throws SQLException {

        TaskItem item = new TaskItem();
     // some stuff....       
        return item;

    }

And this Eclipse reports:

在此输入图像描述

You use

public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException

The placeholder for the type is T . This means the the result list is a list of T and the Class that is passed as a parameter is a Class<T> . Therefore when you want a LinkedList<Object> you need to pass a Class<Object> to the method.

When you want to pass a Class<TaskItem> you will get a LinkedList<TaskItem> . Therefore it will be

public List<TaskItem> toBeanList(ResultSet rs, Class<TaskItem> type) throws SQLException

尝试以这种方式声明您的方法:

public LinkedList<Object> toBeanList(ResultSet rs, Class<Object> clazz) 

The T in that declaration is a type variable, and it has to be consistent across wherever you're using it. In this case, all of the T s on the method (and elsewhere in the class, such as on toBean ) have to match. The point of having the class variable is to tell Java what type of object you're extracting from the ResultSet , so you would need to declare your method like this:

public LinkedList<TaskItem> toBeanList(ResultSet rs, Class<TaskItem> class)

Note that in this case, if you already know that you're using the TaskItem specifically, passing it to toBeanList wouldn't be necessary except that the method you're overriding requires it.

As mentioned in the method definition:

public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException

The 'T' of Class should be same as the T of the List.

So your method definition should be:

public List<TaskItem> toBeanList(ResultSet rs, Class<TaskItem> type) throws SQLException{
//Code
}

Is this how your method looks?

Try this.

toBeanList method

@Override
public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException {
    List<T> list = new ArrayList<T>();
    while (rs.next()) {
        list.add(toBean(rs, type));
    }
    return list;
}

toBean method

@Override
public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException {
    TaskItem item = new TaskItem();

    //populate item ...

    return type.cast(item);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM