rxfilewatcher icon indicating copy to clipboard operation
rxfilewatcher copied to clipboard

Path from events must be absolute

Open msangel opened this issue 6 years ago • 2 comments

As in usage place we might have no context information about the parent, and relative path not enough.

msangel avatar Dec 17 '19 01:12 msangel

This is how this problem is fixed in one of forks: https://github.com/mingcheng/rx-filewatcher.java/commit/80c64f1e7f65c5f5c75309ee926d218954aa3112

msangel avatar Dec 17 '19 01:12 msangel

But that code has a problem: not every event is Path event. Here's my function based on that but with this extra check:

private WatchEvent<?> withAbsolutePath(WatchEvent<?> event, Path fileAbsolutePath) {
    Class<?> type = event.kind().type();
    if (type.isAssignableFrom(Path.class)) {
        return new WatchEvent<Path>() {
            @Override
            public Kind kind() {
                return event.kind();
            }
            @Override
            public int count() {
                return event.count();
            }
            @Override
            public Path context() {
                return fileAbsolutePath;
            }
        };
    } else {
        return event;
    }
}

msangel avatar Dec 17 '19 01:12 msangel