Java Is it safe to turn off an alert without 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 string

 WatchEvent<Path> ev = (WatchEvent<Path>)event; 

since event exits key.pollEvents() as WatchEvent<?> , and the compiler cannot tell if it will contain Path at run time, and not something else.

Regarding this, I wondered if it was possible to get rid of this warning without explicitly suppressing it. I found some hint, although it was connected with completely different situations, for example, but here it seems that they can control how the general list is created, while in my case it isn Maybe.

I also found this one where they offer to suppress the warning, checking at the same time if the actual type is correct (since the compiler cannot do it on its own), but in my case I could not do anything in this direction. Is it possible? How do you do this?

On the other hand, in my case, I get these WatchEvent from the WatchService registered with the Path object: is this fact enough to prove that every WatchEvent<?> WatchService<?> out of this WatchService<?> Will have an implementation of type Path ? If this is true, can I safely assume that the cast will always be correct and suppress the warning? Is there a way to avoid this without suppressing it in this case?

Thank you very much.

EDIT

I could immediately check for links that explicitly state that:

T context ()

Returns the context for the event.

In the case of the events ENTRY_CREATE, ENTRY_DELETE and ENTRY_MODIFY, the context is the path that is the relative path between the directory registered in the monitoring service and the record that was created, deleted or modified.

So, in my case, I am observing the ENTRY_MODIFY events, so my type T definitely Path .

+6
source share
1 answer

I think the best option is to just crush it

  @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>)event; 

it is completely safe, it can only be <Path> and nothing more. The API designer was a little crazy about being too general.

WatchService is quite difficult to use. I have the following utility class that you can use in

https://github.com/zhong-j-yu/bayou/blob/0.9/src/_bayou/_tmp/_FileMonitor.java

for instance

 _FileMonitor monitor = new _FileMonitor( ROOT_DIR ); List<Set<Path>> changes = monitor.pollFileChanges( TIMEOUT ) // return 3 sets, [0]=created, [1]=modified, [2]=deleted 
+4
source

Source: https://habr.com/ru/post/986998/


All Articles