Skip to content

Conversation

@sungshik
Copy link
Contributor

@sungshik sungshik commented Jan 31, 2025

In preparation of work to improve support for overflows (#12), this PR refactors some common features of the existing JDK...Watch classes out of those classes into a shared base class. This PR is supposed to be purely refactoring, so it's intended to leave the code functionally exactly the same.

The commit messages should give a good overview of the changes.

@codecov
Copy link

codecov bot commented Jan 31, 2025

Codecov Report

Attention: Patch coverage is 85.88235% with 12 lines in your changes missing coverage. Please review.

Project coverage is 80.2%. Comparing base (b07727f) to head (fee22fd).
Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
.../engineering/swat/watch/impl/jdk/JDKFileWatch.java 70.5% 1 Missing and 4 partials ⚠️
.../engineering/swat/watch/impl/jdk/JDKBaseWatch.java 87.5% 2 Missing and 2 partials ⚠️
...neering/swat/watch/impl/jdk/JDKDirectoryWatch.java 75.0% 0 Missing and 2 partials ⚠️
...wat/watch/impl/jdk/JDKRecursiveDirectoryWatch.java 95.0% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##              main     #13     +/-   ##
=========================================
+ Coverage     78.1%   80.2%   +2.0%     
- Complexity      84      89      +5     
=========================================
  Files           10      11      +1     
  Lines          421     415      -6     
  Branches        44      41      -3     
=========================================
+ Hits           329     333      +4     
+ Misses          65      57      -8     
+ Partials        27      25      -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor Author

@sungshik sungshik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few clarifying comments

@sungshik sungshik marked this pull request as ready for review February 17, 2025 09:04
Copy link
Member

@DavyLandman DavyLandman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, a few small nits. Thanks for refactoring this into a base class that shares more of the similar code.

* @throws IOException When an I/O exception of some sort (e.g., a nested
* watch failed to start) has occurred
*/
protected abstract boolean runIfFirstTime() throws IOException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but as an implementer I'm not so sure who should check if it's the first time? I originally thought this base class would take care, but it looks like it doesn't? Why don't we keep track of this fact in this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's cleaner. Fixed.

throw new IllegalArgumentException("Unexpected watch event: " + jdkEvent);
}
var rootPath = path;
var relativePath = kind == WatchEvent.Kind.OVERFLOW ? rootPath : (@Nullable Path)jdkEvent.context();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come rootPath is also a valid relativePath? should it not be an empty path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that makes more sense. Fixed.

throw new IOException("Could not register file watcher for: " + file, e);
// Use local variables to check null before field assignments (Checker
// Framework doesn't like it the other way around)
var parent = path.getParent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternative is to use Objects::requireNonNull as that makes the code a bit more compact (and CF correctly models the guarantees of requireNonNull

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects.requireNonNull doesn't work well with CF here because it still throws an NPE. I like the idea of having a small function to do the heavy lifting (well, not that heavy...), though, so I added a requireNonNull for Paths that throws an IllegalArgumentException. It makes the constructor more compact.

Copy link
Contributor Author

@sungshik sungshik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Davy!

throw new IllegalArgumentException("Unexpected watch event: " + jdkEvent);
}
var rootPath = path;
var relativePath = kind == WatchEvent.Kind.OVERFLOW ? rootPath : (@Nullable Path)jdkEvent.context();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that makes more sense. Fixed.

throw new IOException("Could not register file watcher for: " + file, e);
// Use local variables to check null before field assignments (Checker
// Framework doesn't like it the other way around)
var parent = path.getParent();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects.requireNonNull doesn't work well with CF here because it still throws an NPE. I like the idea of having a small function to do the heavy lifting (well, not that heavy...), though, so I added a requireNonNull for Paths that throws an IllegalArgumentException. It makes the constructor more compact.

* @throws IOException When an I/O exception of some sort (e.g., a nested
* watch failed to start) has occurred
*/
protected abstract boolean runIfFirstTime() throws IOException;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's cleaner. Fixed.

@sungshik sungshik merged commit e23a194 into main Feb 17, 2025
15 checks passed
@sungshik sungshik deleted the jdk-base-watcher branch February 17, 2025 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants