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

Use try-with-resources where possible #6765

Merged
merged 4 commits into from
Jul 5, 2022

Conversation

basil
Copy link
Member

@basil basil commented Jul 3, 2022

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 a try-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

  • (If applicable) Jira issue is well described
  • Changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developer, depending on the change) and are in the imperative mood. Examples
    • Fill-in the Proposed changelog entries section only if there are breaking changes or other changes which may require extra steps from users during the upgrade
  • Appropriate autotests or explanation to why this change has no tests
  • New public classes, fields, and methods are annotated with @Restricted or have @since TODO Javadoc, as appropriate.
  • New deprecations are annotated with @Deprecated(since = "TODO") or @Deprecated(forRemoval = true, since = "TODO") if applicable.
  • For dependency updates: links to external changelogs and, if possible, full diffs

Desired reviewers

@mention

Maintainer checklist

Before the changes are marked as ready-for-merge:

  • There are at least 2 approvals for the pull request and no outstanding requests for change
  • Conversations in the pull request are over OR it is explicit that a reviewer does not block the change
  • Changelog entries in the PR title and/or Proposed changelog entries are accurate, human-readable, and in the imperative mood
  • Proper changelog labels are set so that the changelog can be generated automatically
  • If the change needs additional upgrade steps from users, upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the PR title. (example)
  • If it would make sense to backport the change to LTS, a Jira issue must exist, be a Bug or Improvement, and be labeled as lts-candidate to be considered (see query).

@basil basil added the skip-changelog Should not be shown in the changelog label Jul 3, 2022
@basil basil added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Jul 4, 2022
Comment on lines 485 to 491
try {
InputStream is = CLI.class.getResourceAsStream("/jenkins/cli/jenkins-cli-version.properties");
if (is != null) {
try {
try (is) {
props.load(is);
} finally {
is.close();
}
}
Copy link
Member

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);
                }
            }

Copy link
Member Author

Choose a reason for hiding this comment

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

Pre-existing issue

Comment on lines 535 to 539
Archiver a = factory.create(out);
try {
try (a) {
scanner.scan(f, ignoringSymlinks(a, verificationRoot, noFollowLinks));
} finally {
a.close();
}
return a.countEntries();
Copy link
Member

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();
                }

Copy link
Member Author

Choose a reason for hiding this comment

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

Pre-existing issue

@basil
Copy link
Member Author

basil commented Jul 4, 2022

I agree that your suggestions are good improvements, but I am not going to fix pre-existing issues not caused by this PR.

@basil
Copy link
Member Author

basil commented Jul 4, 2022

@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.

@basil basil requested a review from jtnord July 4, 2022 19:30
@basil basil merged commit 76e5c98 into jenkinsci:master Jul 5, 2022
@basil basil deleted the try-with-resources branch July 5, 2022 00:36
@MarkEWaite MarkEWaite mentioned this pull request Oct 1, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback skip-changelog Should not be shown in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants