-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Use try-with-resources where possible #6765
Conversation
try { | ||
InputStream is = CLI.class.getResourceAsStream("/jenkins/cli/jenkins-cli-version.properties"); | ||
if (is != null) { | ||
try { | ||
try (is) { | ||
props.load(is); | ||
} finally { | ||
is.close(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try-with-resources is null
safe.
Why not
try (InputStream is = CLI.class.getResourceAsStream("/jenkins/cli/jenkins-cli-version.properties")) {
if (is != null) {
props.load(is);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-existing issue
Archiver a = factory.create(out); | ||
try { | ||
try (a) { | ||
scanner.scan(f, ignoringSymlinks(a, verificationRoot, noFollowLinks)); | ||
} finally { | ||
a.close(); | ||
} | ||
return a.countEntries(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would also seem more readable as:
try (Archiver a = factory.create(out)) {
scanner.scan(f, ignoringSymlinks(a, verificationRoot, noFollowLinks));
return a.countEntries();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-existing issue
I agree that your suggestions are good improvements, but I am not going to fix pre-existing issues not caused by this PR. |
@jtnord I had some extra time on my day off and the changes seemed relatively simple, so I have accommodated your requests. In the future, kindly refrain from commenting on my PRs with requests for change regarding pre-existing issues. |
Before Java 9, we could only use fresh variables inside a try-with-resources block. As of Java 9 and as part of JEP 213, we can now use
final
or even effectively final variables inside atry-with-resources
block. The benefit of try-with-resources is that they suppress exceptions thrown during close, propagating the original exception instead.Proposed changelog entries
N/A
Proposed upgrade guidelines
N/A
Submitter checklist
Proposed changelog entries
section only if there are breaking changes or other changes which may require extra steps from users during the upgrade@Restricted
or have@since TODO
Javadoc, as appropriate.@Deprecated(since = "TODO")
or@Deprecated(forRemoval = true, since = "TODO")
if applicable.Desired reviewers
@mention
Maintainer checklist
Before the changes are marked as
ready-for-merge
:Proposed changelog entries
are accurate, human-readable, and in the imperative moodupgrade-guide-needed
label is set and there is aProposed upgrade guidelines
section in the PR title. (example)lts-candidate
to be considered (see query).