From: iksrazal on
Hi all,

Allow my code to explain:

Bsc bsc = BscHelper.createBsc();
SiteType siteType = BscHelper.createSiteType(id,name);
bsc.getSite().add(siteType);

getSite() returns List. It was written without generics in mind.
However, I get a warning in eclipse:

Type safety: The method add(Object) belongs to the raw type List.
References to generic
type List<E> should be parameterized

What it wants is:

List<SiteType> list = new ArrayList<SiteType>();

How can I use method chaining with generics?

iksrazal

From: Thomas Hawtin on
iksrazal(a)terra.com.br wrote:
>
> Bsc bsc = BscHelper.createBsc();
> SiteType siteType = BscHelper.createSiteType(id,name);
> bsc.getSite().add(siteType);
>
> getSite() returns List. It was written without generics in mind.
> However, I get a warning in eclipse:
>
> Type safety: The method add(Object) belongs to the raw type List.
> References to generic
> type List<E> should be parameterized
>
> What it wants is:
>
> List<SiteType> list = new ArrayList<SiteType>();
>
> How can I use method chaining with generics?

I don't think it's anything to do with chaining. You just need to return
a generified List.

interface Bsc {
List<SiteType> getSite();
}
From: Hemal Pandya on


> Hi all,
>
> Allow my code to explain:
>
> Bsc bsc = BscHelper.createBsc();
> SiteType siteType = BscHelper.createSiteType(id,name);
> bsc.getSite().add(siteType);
>
> getSite() returns List. It was written without generics in mind.
> However, I get a warning in eclipse:
>
> Type safety: The method add(Object) belongs to the raw type List.
> References to generic
> type List<E> should be parameterized
>
> What it wants is:
>
> List<SiteType> list = new ArrayList<SiteType>();

Almost. javac accepts if getSite returns any compatible generic type:
List<SiteType> or even List<Object>. So Bsc is open this is simple.

Otherwise, you can replace with
((List<Object>)(bsc.getSites())).add(siteType);
for which javac produces a different warnign that is IMHO less severe.

Actually I don't quite understand why cast from List to List<Object> is
a warning at all.
>
> How can I use method chaining with generics?
>
> iksrazal

From: iksrazal on
Thanks for the response.

The class is autogenerated via JAXB, so I can't really just do:

interface Bsc {
List<SiteType> getSite();

However, as stated I do get a different warning now:

((List<Object>)(bsc.getSite())).add(siteType);

Type safety: The cast from List to List<Object> is actually checking
against the erased type List

Hmm. I work in an environment where warnings are discouraged. Any idea
on how to get this to work, ie no warnings, without changing the Bsc
interface?

Thanks,
iksrazal

From: Oliver Wong on
"Hemal Pandya" <hemalpandya(a)gmail.com> wrote in message
news:1125423582.687861.85530(a)z14g2000cwz.googlegroups.com...
> Actually I don't quite understand why cast from List to List<Object> is
> a warning at all.

The warning says that the runtime environment can't actually tell
whether the cast succeeds or not (due to type erasure), so basically, the
code that you wrote down is not actually hapenning. I think that warrants a
warning.

- Oliver