java – -Xlint:在netbeans中未选中
发布时间:2020-12-15 08:47:04 所属栏目:Java 来源:网络整理
导读:清洁时在NetBeans中构建我的项目有一个警告,说“不安全的操作”,所以我使用-Xlint:unchecked来查看这些操作,但我无法理解我做错了什么.这些是警告,然后我的代码谢谢! UploadBean.java:40: warning: [unchecked] unchecked conversion private final List f
清洁时在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 } } } } 干杯 解决方法
您需要了解Java Generics.旧的1.4样式仍然会编译,但它会发出警告(一些人认为是错误的).
由于您使用的类需要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 } } } } 请注意,对于泛型,不再需要某些类型的转换.例如,fileList.get(0)将在上面的示例中返回FileDescription,而无需进行显式转换. 泛型参数指示存储在fileList中的任何内容必须是“至少”FileDescription.编译器检查是否无法在列表中放置非FileDescription项,并且输出代码实际上不执行任何运行时检查.因此,泛型实际上不会像其他语言中的类似技术那样遭受性能命中,但是编译器执行的“类型擦除”使得诸如通用参数的运行时类型分析之类的技术难以实现. 尝试一下,你会喜欢它们. 如果此代码是在Generics发布之前编写的,您可能希望使用向后兼容性标志(-source 1.4 -target 1.4)对其进行编译. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |