when cleaning & building my project in NetBeans there’s a warning that says “unsafe operations” so I use -Xlint:unchecked to see those operations but I cannot understand what am I doing wrong. These are the warnings and then my code thanks !
UploadBean.java:40: warning: [unchecked] unchecked conversion
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:40: warning: [unchecked] unchecked method invocation: method synchronizedList in class Collections is applied to given types
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:97: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
fileList.add(fd);
where E is a type-variable:
E extends Object declared in interface List
3 warnings
CODE
//This is line 40
private final List fileList = Collections.synchronizedList(new ArrayList());
//This is line 88
public void doUpload(FileEntryEvent e) {
FileEntry file = (FileEntry) e.getSource();
FileEntryResults result = file.getResults();
for (FileInfo fileInfo : result.getFiles()) {
if (fileInfo.isSaved()) {
FileDescription fd =
new FileDescription(
(FileInfo) fileInfo.clone(), getIdCounter());
synchronized (fileList) {
fileList.add(fd); //This is line 97
}
}
}
}
cheers
You need to learn about Java Generics. The old 1.4 style will still compile, but it will do so with warnings (considered errors by some).
Since the classes you are using expect Generic Type parameters, they need to be specified to the compiler, like so:
Note that with generics, some types of casting is no longer necessary. For example,
fileList.get(0)will return aFileDescriptionin the above example, without the need to do an explicit cast.The generics parameter indicates that whatever is stored within the
fileListmust be “at least” a FileDescription. The compiler checks that it is impossible to place non-FileDescription items in the list, and the output code actually doesn’t do any run-time checks. Thus generics actually doesn’t suffer performance hits like similar techniques in other languages, however the “type erasure” preformed by the compiler makes certain techinques like Run Time Type Analysis of a Generic parameter impossible.Try them out, you’ll like them.
If this code was written prior to the release of Generics, you might want to compile it with backwards compatibility flags (-source 1.4 -target 1.4).