Can't avoid "References to generic type List<E> should beparameterized" warning here can I ?

  • Thread starter Sébastien de Mapias
  • Start date
S

Sébastien de Mapias

Hi,
I'm writing some kind of gateway between 2 packages, whose a
few classes have the same name (I need to bind these classes).

So I'm retrieving the attributes of my source class1 and set my
equivalent class2 in the other pkg with these attributes when
methods names -and attributes- match.

Excerpt from my method:
1 public my.persistent.Pst mapFromProtocol(my.protocol.Pst src)
2 {
3 my.persistent.Pst target = new my.persistent.Pst();
4 for (Iterator<my.protocol.Remark> it =
src.getRemark().iterator(); it.hasNext(); )
5 target.getRemark().add(it.next());
6
7 ... //many other attributes/objects to set too
8
9 return target;
10 }

In RAD the line 5 is underlined in yellow and it says "Type safety:
The method add(Object) belongs to the raw type List. References
to generic type List<E> should be parameterized".

Is there a way to avoid this warning in my case, as I can't
instantiate
a List<Remark> lr = new ArrayList() of course... (because I have no
method setRemark() in target)

Thanks.
Regards,
Seb
 
J

Jens Mattke

Hello Sébastien.

You could add an annotation to your method:

@SuppressWarnings("unchecked")
1 public my.persistent.Pst mapFromProtocol(my.protocol.Pst src)
2 { ...
10 }

But that would only be a cosmetic solution.

If it is possible for you to alter the my.persistent.Pst class, you
could change the remark's type to List<Object>.
The add method will accept everything, then.

Regards,
Jens
 
R

Roedy Green

for (Iterator<my.protocol.Remark> it =
src.getRemark().iterator(); it.hasNext(); )
5 target.getRemark().add(it.next());

if you use a for:each Iterable, you would not need the <>.
 
S

Sébastien de Mapias

I just replaced
~ for (Iterator<my.protocol.Remark> it = src.getRemark().iterator();
it.hasNext(); )
~ target.getRemark().add(it.next());
with
~ for (my.protocol.Remark r : src.getRemark())
~ target.getRemark().add(r);
but I still have this beige serrated underline and the same warning.

Thanks for your replies.
Seb
 
V

voorth

I just replaced
~ for (Iterator<my.protocol.Remark> it = src.getRemark().iterator();
it.hasNext(); )
~ target.getRemark().add(it.next());
with
~ for (my.protocol.Remark r : src.getRemark())
~ target.getRemark().add(r);
but I still have this beige serrated underline and the same warning.

Thanks for your replies.
Seb

Can you change the return type of getRemark() to List<Remark>?
And while you'r at it, change its name to getRemarks(). After all,
you're returning a collection of remarks.

Oh, and the for loop is probably not needed; try :
target.getRemark().addAll(src.getRemark());

Henk
 
D

Daniele Futtorovic

I just replaced
~ for (Iterator<my.protocol.Remark> it = src.getRemark().iterator();
it.hasNext(); )
~ target.getRemark().add(it.next());
with
~ for (my.protocol.Remark r : src.getRemark())
~ target.getRemark().add(r);
but I still have this beige serrated underline and the same warning.

:shrug:

If you've got your mind set on getting rid of that "beige serrated
underline" that much, you can always cast:

Collection<my.protocol.Remark> c = (Collection<my.protocol.Remark>)
src.getRemark();
 
D

Daniele Futtorovic

:shrug:

If you've got your mind set on getting rid of that "beige serrated
underline" that much, you can always cast:

Collection<my.protocol.Remark> c = (Collection<my.protocol.Remark>)
src.getRemark();

Ah, wait -- no you probably can't. Too bad.
 
D

Daniel Pitts

Sébastien de Mapias said:
Hi,
I'm writing some kind of gateway between 2 packages, whose a
few classes have the same name (I need to bind these classes).

So I'm retrieving the attributes of my source class1 and set my
equivalent class2 in the other pkg with these attributes when
methods names -and attributes- match.

Excerpt from my method:
1 public my.persistent.Pst mapFromProtocol(my.protocol.Pst src)
2 {
3 my.persistent.Pst target = new my.persistent.Pst();
4 for (Iterator<my.protocol.Remark> it =
src.getRemark().iterator(); it.hasNext(); )
5 target.getRemark().add(it.next());
6
7 ... //many other attributes/objects to set too
8
9 return target;
10 }

In RAD the line 5 is underlined in yellow and it says "Type safety:
The method add(Object) belongs to the raw type List. References
to generic type List<E> should be parameterized".

Is there a way to avoid this warning in my case, as I can't
instantiate
a List<Remark> lr = new ArrayList() of course... (because I have no
method setRemark() in target)

Thanks.
Regards,
Seb

Change target.getRemark() and src.getRemark() both to return List<Remark>

While your at it:
target.getRemark().addAll(src.getRemark()) might help you out some.

What I don't get is that if Pst.getRemark().iterator() returns an
Iterator<my.protocal.Remark>, then there shouldn't be a problem.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top