generics and type checking

M

Marc E

All,
I'm giving generics a shot, and I'm getting this warning: Type safety:
The cast from Object to ArrayList<RequestBean> is actually checking against
the erased type

What I'm wondering is:
1) is this really a problem?
2) Assuming my code is weak, what changes are required to satisfy the
compiler such that I stop receiving the warning?
3) Are generics really beneficial here at all? It seems the only thing I
gained by adding the 4 instances of <RequestBean> was not having to cast to
(RequestBean) in the code that calls this method. Of course, when I remove
all parameters and do it the "old" way, I get the warning "Type safety: The
method get(int) belongs to the raw type ArrayList. References to generic
type ArrayList<E> should be parameterized". But that's neither here nor
there.

Here's the offending code, with the junk removed:

public ArrayList<RequestBean> getPendingRequests(String envIDs,
Integer serverGroupID){
ArrayList<RequestBean> ar_RequestBeans = new
ArrayList<RequestBean>();

ar_RequestBeans = (ArrayList<RequestBean>)
runner.query(cn,sql,h);

return ar_RequestBeans;
}

FYI, runner is an apache DBUtils.QueryRunner object, and query() returns
Object.

Thanks in advance for the teaching.

Marc
 
B

Benji

Marc said:
2) Assuming my code is weak, what changes are required to satisfy the
compiler such that I stop receiving the warning?

your code is weak - because it uses the loosely typed runner.query() call.
(there's no way to fix that)
ArrayList<RequestBean> ar_RequestBeans = new
ArrayList<RequestBean>();

don't create a new ArrayList if you're just going to reassign ar_RB on
the next line.
 
M

Marc E

Sorry Benji,
I cut out a good deal of code...all the stuff in between the instantiation
and reassignment.

So if I understand correctly, the problem here is that since runner.query()
returns an Object, that no matter what I do i'll not get rid of the compiler
warning since there's no real way for it to know what's in that Object?

Thanks again.
 
T

Thomas Hawtin

Marc said:
All,
I'm giving generics a shot, and I'm getting this warning: Type safety:
The cast from Object to ArrayList<RequestBean> is actually checking against
the erased type

What I'm wondering is:
1) is this really a problem?

It means you might get ClassCastExceptions out of nowhere if the cast is
incorrect.
2) Assuming my code is weak, what changes are required to satisfy the
compiler such that I stop receiving the warning?

@SuppressWarnings("unchecked") and JDK 1.5.0_06.

Or more sensibly DBUtils needs to use generics.

Or you could subclass the ArrayList as something like:

class RequestBeanList extends ArrayList<RequestBean> {
}

I think I prefer the first option to that.
3) Are generics really beneficial here at all? It seems the only thing I
gained by adding the 4 instances of <RequestBean> was not having to cast to
(RequestBean) in the code that calls this method. Of course, when I remove
all parameters and do it the "old" way, I get the warning "Type safety: The
method get(int) belongs to the raw type ArrayList. References to generic
type ArrayList<E> should be parameterized". But that's neither here nor
there.

Code intimately involved with non-generic code is going to be a bit of a
mess.
public ArrayList<RequestBean> getPendingRequests(String envIDs,

What's wrong with just List said:
FYI, runner is an apache DBUtils.QueryRunner object, and query() returns
Object.

<http://jakarta.apache.org/commons/d...ls/QueryRunner.html#query(java.sql.Connection,
java.lang.String, org.apache.commons.dbutils.ResultSetHandler)>

Tom Hawtin
 
R

Roedy Green

Are generics really beneficial here at all?

They provide some documentation on just what you intend to keep in the
various collections and they do enforce those rules. Whether that
benefit is worth all the futzing about is another matter.
 
T

Thomas Hawtin

Roedy said:
Is this available anywhere?

Not publicly as of 5 minutes ago.

I meant to write "wait for", but I think I got distracted deciding
whether I wanted to write JDK 1.5.0_06, 1.5.0u6, J2SE 5.0 update 6, or
something else.

Tom Hawtin
 
A

Andrew Thompson

Thomas said:
Not publicly as of 5 minutes ago.

I meant to write "wait for", but I think I got distracted deciding
whether I wanted to write JDK 1.5.0_06, 1.5.0u6, J2SE 5.0 update 6, or
something else.

(Shrugs) Pick any one at random, if it is not
called that right at this instant, it might well be -
in ten minutes time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top