Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ErrorProne errors #3623

Merged
merged 20 commits into from
Jan 10, 2018
Merged

Fix ErrorProne errors #3623

merged 20 commits into from
Jan 10, 2018

Conversation

koppor
Copy link
Member

@koppor koppor commented Jan 9, 2018

This fixes

/home/ubuntu/jabref/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java:156: error: [StreamResourceLeak] Streams that encapsulate a closeable resource should be closed using try-with-resources
                List<Path> allStyles = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl")).collect(Collectors.toList());
                                                 ^
    (see http://errorprone.info/bugpattern/StreamResourceLeak)
  Did you mean 'List<Path> allStyles ;'?
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

List<Path> allStyles = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl")).collect(Collectors.toList());

List<Path> allStyles;
try (Stream<Path> stylefileStream = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl"))) {
Copy link
Member

Choose a reason for hiding this comment

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

This is again one of the ugly cases where an unchecked exception could be thrown. Because streams and lambdas can't throw checked exceptions. In this case it could be that the stream is already closed when calling collect.
Similar to #3606

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please catch IOUncheckedException and rethrow it as IOException.

Copy link
Member

@tobiasdiez tobiasdiez left a comment

Choose a reason for hiding this comment

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

Except the stuff concerning unchecked IO exceptions, this looks good.

List<Path> allStyles = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl")).collect(Collectors.toList());

List<Path> allStyles;
try (Stream<Path> stylefileStream = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl"))) {
Copy link
Member

Choose a reason for hiding this comment

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

Yes, please catch IOUncheckedException and rethrow it as IOException.

@@ -331,8 +331,8 @@ public static String createDirNameFromPattern(BibDatabase database, BibEntry ent
* @return the path to the first file that matches the defined conditions
*/
public static Optional<Path> find(String filename, Path rootDirectory) {
try {
return Files.walk(rootDirectory)
try (Stream<Path> pathStream = Files.walk(rootDirectory)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you please check if walk may also throw an IOUncheckedEx and if yes, add it to the catch block.

Copy link
Member

@LinusDietz LinusDietz Jan 10, 2018

Choose a reason for hiding this comment

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

Checked the JavaDoc and the sources... does not seem so.

EDIT: it does :/

res.addAll(findFile(entry, subElement, restOfFileString, extensionRegExp));
}
});
try (Stream<Path> pathStream = Files.walk(actualDirectory)) {
Copy link
Member

Choose a reason for hiding this comment

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

here also

}

private List<Path> collectMatchingFiles(Path actualDirectory, BiPredicate<Path, BasicFileAttributes> matcher) {
try (Stream<Path> pathStream = Files.find(actualDirectory, 1, matcher)) {
Copy link
Member

Choose a reason for hiding this comment

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

and here

@LinusDietz LinusDietz changed the title This fixes the error prone errors Fix ErrorProne errors Jan 9, 2018
@LinusDietz LinusDietz added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Jan 10, 2018
@LinusDietz
Copy link
Member

@tobiasdiez @Siedlerchr Can you have a look if the exception handling is correct now? see c93ae63 for that

try (Stream<Path> pathStream = Files.find(actualDirectory, 1, matcher)) {
return pathStream.collect(Collectors.toList());
} catch (UncheckedIOException | IOException ioe) {
return Collections.emptyList();
Copy link
Member

Choose a reason for hiding this comment

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

I think this method should just rethrow as IOException.

@tobiasdiez
Copy link
Member

@LinusDietz Thanks, the code looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants