-Xlint:在 netbeans 中未选中

发布于 2024-12-29 04:03:34 字数 1785 浏览 0 评论 0原文

清洁时在 NetBeans 中构建我的项目时,出现一条警告,提示“不安全操作”,因此我使用 -Xlint:unchecked 来查看这些操作,但我无法理解我做错了什么。这些是警告,然后是我的代码,谢谢!

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

代码

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

欢呼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2025-01-05 04:03:34

您需要了解 Java 泛型。旧的 1.4 样式仍然可以编译,但会出现警告(有些人认为是错误)。

由于您使用的类需要通用类型参数,因此需要将它们指定给编译器,如下所示:

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

请注意,使用泛型时,不再需要某些类型的转换。例如,在上面的示例中,fileList.get(0) 将返回一个 FileDescription,无需进行显式转换。

generics 参数指示 fileList 中存储的任何内容都必须“至少”是 FileDescription。编译器检查不可能将非 FileDescription 项放入列表中,并且输出代码实际上不执行任何运行时检查。因此,泛型实际上不会像其他语言中的类似技术那样遭受性能影响,但是编译器执行的“类型擦除”使得某些技术(例如泛型参数的运行时类型分析)变得不可能。

尝试一下,您会喜欢它们的。

如果此代码是在泛型发布之前编写的,您可能需要使用向后兼容性标志(-source 1.4 -target 1.4)来编译它。

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:

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

The generics parameter indicates that whatever is stored within the fileList must 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).

九命猫 2025-01-05 04:03:34

您必须在列表中使用类型,例如:

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

或者使用jdk7和钻石运算符,您可以使用:

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

所以在您的代码中使用:

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

You have to use type in your list like :

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

Or with jdk7 and diamond operator you can use :

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

So In your code use :

private final List <FileDescription> fileList = Collections.synchronizedList(new ArrayList<FileDescription>());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文