views:

301

answers:

6

Hi,

I am quite new to Java ... I wrote a class called DLPFile which is basically a container of other objects like Strings, ints, floats, etc.

When putting my files into a List and then saving it in my session (which is from Map class) variable is easy;

DLPFile file = new DLPFile();
List <DLPFile >fileList =  new ArrayList <DLPFile>();
fileList.add(file);
session.put("filesList", fileList);

but how do I retrieve the list from the session var? When I do:

List <DLPFile files = (List) session.get("fileslist");

I got some warnings:

"List is a raw type.References to generic type List<E> should be parameterized."

I tried

List <DLPFile files = (List <DLPFile> ) session.get("fileslist");   
List <DLPFile files = (List ) session.get("fileslist")<DLPFile>; and
List <DLPFile files = (List) <DLPFile>  session.get("fileslist");

but none works

I suppose this is kind of a "casting" problem... (maybe?)

Thanks in advance ;)

+2  A: 

Are you aware that you are missing a > at the start? i.e. 'List <DLPFile files' should be 'List <DLPFile>' files'.

DaveJohnston
I think that character is only not showing up here... but anyway, that is not the origin of the warning.
nacho4d
I am also using session object to store some other objects that are not Lists of DLPFile, this is the reason for not mapping it as you suggested. BTW: Stackoverflow is very useful!!
nacho4d
A: 

How is session defined?

Session is a Map class instance. Right Know I can not write the exact code but, Is a session I got using Struts.... ActionSomething.
nacho4d
+2  A: 

This option should be fine

List <DLPFile> files = (List <DLPFile>) session.get("fileslist");

Although you'll get an unchecked cast warning since I don't expect your session to be a Map<String, List<DLPFile>>.

Look through the Java Generics FAQ http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

pjp
+2  A: 

This is because of the Generics Type erasure. The compiler has no way to determine the actual generic type argument when you get it from your session (except if you session.get takes a Class<T> argument to cast it accordingly), because the session probably only returns the type of object. You can make sure that your object is an instance of List, but the generic type information is lost (basically the compiler converts it to a List<Object> internally). That is why you get the warning, because only you as the programmer can know if the generic type parameter you want to cast it to is the right one.

If you don't like the warning you may add an

@SuppressWarnings("unchecked")

Annotation at the beginning of your method.

Daff
Thanks,... but is there a way I can solve the warning instead of suppresing it?
nacho4d
You could have a 'List<?>' but you want a list of your file type, so no
pjp
No there isn't a way because of how Generics are implemented in Java. The only way is for the session object to support a generic parameter in the get method (but it woon't do anything different than an unchecked cast). It is just a warning, not an error. List<?> is the equivalent of List<Object>. Best way would be to derive your own class from List<DLPFile> cause then you are checking against an actual class and not a generic interface.
Daff
+1  A: 

The first option you tried is the one you need:

List<DLPFile> files = (List<DLPFile>) session.get("fileslist");

The first warning you get doesn't have anything to do with generics: You would get the warning when casting to, for example, String as well. The compiler just tells you it can't ensure the returned object is a List<DLPFile>.

The second one, about the raw type, does have to do with generics. If you use the option above, you shouldn't get it, but if you cast to just List, you will get it. It tells you you shouldn't use List without the type parameter, in your case <DLPFile>.

Jorn
I agree with you. But I don't know why the warning didn't dissapear with the suggested code. (Is the first I tried)
nacho4d
Are you sure the warning for this option is the same one as the others? Because there aren't any unparameterized references in it...
Jorn
@nacho4d - the warning doesn't disappear because of the way generics work. They're essentially a compile-time trick only; casting to `List<DLPFile>` is basically exactly the same as casting to `List`, since there's no way for *either* the compiler *or* the runtime to check that the list really "is" a `List<DLPFile>` and not, say, a `List<String>`. It's just making you aware that your cast isn't really protecting you from ClassCastExceptions on this line.
Andrzej Doyle
A: 

As others have said, in Java you can't safely cast generic types.

Rather than ignoring the compiler, a better way to fix this problem is to put something a bit more meaningful into the session. Wrap the List in a class that handles this sort of collection of the type. Heck, even add a few instance methods that do something meaningful with the list, rather than a getFiles method.

Tom Hawtin - tackline