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
Benji - 08 Nov 2005 01:19 GMT
> 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.
> ar_RequestBeans = (ArrayList<RequestBean>)
> runner.query(cn,sql,h);
> return ar_RequestBeans;
> }

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Marc E - 08 Nov 2005 01:40 GMT
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.
>> 2) Assuming my code is weak, what changes are required to satisfy the
>> compiler such that I stop receiving the warning?
[quoted text clipped - 13 lines]
>> return ar_RequestBeans;
>> }
Thomas Hawtin - 08 Nov 2005 01:43 GMT
> All,
> I'm giving generics a shot, and I'm getting this warning: Type safety:
[quoted text clipped - 3 lines]
> 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
[quoted text clipped - 3 lines]
> 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<RequestBean>?
> FYI, runner is an apache DBUtils.QueryRunner object, and query() returns
> Object.
<http://jakarta.apache.org/commons/dbutils/apidocs/org/apache/commons/dbutils/Que
ryRunner.html#query(java.sql.Connection,
java.lang.String, org.apache.commons.dbutils.ResultSetHandler)>
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 08 Nov 2005 02:38 GMT
On Tue, 08 Nov 2005 01:43:22 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
>@SuppressWarnings("unchecked") and JDK 1.5.0_06.
I think you mean JDK 1.6

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 08 Nov 2005 02:47 GMT
> On Tue, 08 Nov 2005 01:43:22 +0000, Thomas Hawtin
> <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 3 lines]
>
> I think you mean JDK 1.6
Either 1.5.0_06 or 1.6 is sufficient (or apparently Eclipse JDT thingy).
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 08 Nov 2005 04:43 GMT
On Tue, 08 Nov 2005 02:47:53 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
>Either 1.5.0_06
Is this available anywhere?

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 08 Nov 2005 07:50 GMT
> On Tue, 08 Nov 2005 02:47:53 +0000, Thomas Hawtin
> <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 3 lines]
>
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Andrew Thompson - 08 Nov 2005 09:29 GMT
>>> Either 1.5.0_06
>>
[quoted text clipped - 5 lines]
> 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.
Roedy Green - 08 Nov 2005 02:35 GMT
>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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.