Reputation: 1323
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:
Upvotes: 1
Views: 293
Reputation: 352
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);
}
Upvotes: 0
Reputation: 2896
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
Upvotes: 2
Reputation: 489
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?
Upvotes: 0
Reputation: 77177
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.
Upvotes: 0
Reputation: 2871
Try declaring your method this way:
public LinkedList<Object> toBeanList(ResultSet rs, Class<Object> clazz)
Upvotes: 0