简体   繁体   English

-Xlint:在netbeans中未选中

[英]-Xlint:unchecked in netbeans

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. 在NetBeans中清理和构建我的项目时,会出现“不安全操作”的警告,所以我使用-Xlint:unchecked来查看这些操作,但我无法理解我做错了什么。 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. 您需要了解Java Generics。 The old 1.4 style will still compile, but it will do so with warnings (considered errors by some). 旧的1.4样式仍然会编译,但它会发出警告(一些人认为是错误的)。

Since the classes you are using expect Generic Type parameters, they need to be specified to the compiler, like so: 由于您使用的类需要Generic Type参数,因此需要将它们指定给编译器,如下所示:

 //This is line 40    
 private final List<FileDescription> fileList = Collections.synchronizedList(new ArrayList<FileDescription>());

 //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
                }
            }
        }
    }

Note that with generics, some types of casting is no longer necessary. 请注意,对于泛型,不再需要某些类型的转换。 For example, fileList.get(0) will return a FileDescription in the above example, without the need to do an explicit cast. 例如, fileList.get(0)将在上面的示例中返回FileDescription ,而无需进行显式转换。

The generics parameter indicates that whatever is stored within the fileList must be "at least" a FileDescription. 泛型参数指示存储在fileList中的任何内容必须是“至少”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. 编译器检查是否无法在列表中放置非FileDescription项,并且输出代码实际上不执行任何运行时检查。 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). 如果此代码是在Generics发布之前编写的,您可能希望使用向后兼容性标志(-source 1.4 -target 1.4)对其进行编译。

You have to use type in your list like : 您必须在列表中使用类型:

List<Object> l = new ArrayList<Object>();

Or with jdk7 and diamond operator you can use : 或者使用jdk7和diamond操作符,您可以使用:

List<Object> l = new ArrayList<>();

So In your code use : 所以在你的代码中使用:

private final List <FileDescription> fileList = Collections.synchronizedList(new ArrayList<FileDescription>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM