[英]Java - Is it safe to suppress unchecked cast warning with WatchEvent?
I have the following test code: 我有以下测试代码:
FileSystem fs = FileSystems.getDefault();
Path conf = fs.getPath(".");
WatchKey key = null;
try {
WatchService watcher = fs.newWatchService();
conf.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
while(true) {
key = watcher.take(); // waits
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (StandardWatchEventKinds.OVERFLOW == kind) continue;
WatchEvent<Path> ev = (WatchEvent<Path>)event;
Path file = ev.context();
System.out.println(file);
}
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
The compiler issues an unchecked cast
warning related to the line 编译器发出与该行相关的
unchecked cast
警告
WatchEvent<Path> ev = (WatchEvent<Path>)event;
since event
comes out of key.pollEvents()
as a WatchEvent<?>
, and the compiler can't tell if during runtime it's really going to contain a Path
, and not something else. 因为
event
来自key.pollEvents()
作为WatchEvent<?>
,并且编译器无法判断在运行时它是否真的要包含Path
,而不是其他东西。
Regarding this, I was wondering if it's possible to get rid of this warning without explicitly suppressing it. 对此,我想知道是否有可能在没有明确压制它的情况下摆脱这个警告。 I found some hint, although related to quite different situations, like this , but here it seems that they can control how the generic list is built, while in my case this isn't possible.
我找到了一些提示,虽然与完全不同的情况有关,就像这样 ,但在这里似乎他们可以控制通用列表的构建方式,而在我的情况下,这是不可能的。
I also found this , where they suggest to suppress the warning, checking at the same time if the actual type is the correct one (since the compiler can't do it on its own), but I couldn't manage to do something along these lines in my case. 我也发现了这一点 ,他们建议抑制警告,同时检查实际类型是否正确(因为编译器不能单独执行),但我无法做到在我的情况下这些线。 Is it possible?
可能吗? How would you do it?
你会怎么做?
On the other hand, in my case I'm getting these WatchEvent
's from a WatchService
registered with a Path
object: is this fact alone enough to prove that every WatchEvent<?>
coming out from this WatchService<?>
will have a Path
type implementation? 另一方面,在我的情况下,我从使用
Path
对象注册的WatchService
获取这些WatchEvent
:这个事实足以证明从这个WatchService<?>
出来的每个WatchEvent<?>
都有一个Path
类型实现? If this is true, can I safely assume that the cast will always be correct and suppress the warning? 如果这是真的,我可以安全地假设演员阵容总是正确并且禁止警告吗? Is there any way to avoid it without suppressing it in this case?
在这种情况下,有没有办法避免它而没有压制它?
Thank you very much. 非常感谢你。
I could have immediately checked the references that explicitly state that: 我可以立即检查明确说明的引用 :
T context()
T context()
Returns the context for the event.
返回事件的上下文。
In the case of ENTRY_CREATE, ENTRY_DELETE, and ENTRY_MODIFY events the context is a Path that is the relative path between the directory registered with the watch service, and the entry that is created, deleted, or modified.
对于ENTRY_CREATE,ENTRY_DELETE和ENTRY_MODIFY事件,上下文是Path,它是向watch服务注册的目录与创建,删除或修改的条目之间的相对路径。
So in my case I'm watching for ENTRY_MODIFY
events, hence my T
type is definetely a Path
. 所以在我的情况下,我正在观察
ENTRY_MODIFY
事件,因此我的T
类型定义为Path
。
I think the best option is to just suppress it 我认为最好的选择就是压制它
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>)event;
it's perfectly safe, it can only be <Path>
and nothing else. 它是完全安全的,它只能是
<Path>
而不是别的。 The API designer went a little crazy of being too general. API设计师有点过于笼统。
WatchService
is kind of difficult to use. WatchService
很难使用。 I have the following utility class you might be intereted in 我有以下实用工作类,你可能会在这里
https://github.com/zhong-j-yu/bayou/blob/0.9/src/_bayou/_tmp/_FileMonitor.java https://github.com/zhong-j-yu/bayou/blob/0.9/src/_bayou/_tmp/_FileMonitor.java
For example 例如
_FileMonitor monitor = new _FileMonitor( ROOT_DIR );
List<Set<Path>> changes = monitor.pollFileChanges( TIMEOUT )
// return 3 sets, [0]=created, [1]=modified, [2]=deleted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.