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

8345249: Apply some conservative cleanups in FileURLConnection #22459

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

eirbjo
Copy link
Contributor

@eirbjo eirbjo commented Nov 29, 2024

Please review this PR which applies various cleanups to sun.net.www.protocol.file.FileURLConnection.

This class is known to be an old, intricate, and hard to maintain piece of code. However, there are some relatively straightforward refactorings / cleanups possible which improve readability and makes it easier to reason about what's going on in this class.

In this PR, I have chosen to make each individual small change a separate commit. This to assist review of each individual change, which can otherwise disappear a bit when reviewing the PR as a whole.

A detailed listing of each commit follows in a separate comment.

This is a pure cleanup PR, no tests are added or updated in this PR.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8345249: Apply some conservative cleanups in FileURLConnection (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/22459/head:pull/22459
$ git checkout pull/22459

Update a local copy of the PR:
$ git checkout pull/22459
$ git pull https://git.openjdk.org/jdk.git pull/22459/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 22459

View PR using the GUI difftool:
$ git pr show -t 22459

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/22459.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 29, 2024

👋 Welcome back eirbjo! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Nov 29, 2024

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Nov 29, 2024

@eirbjo The following label will be automatically applied to this pull request:

  • net

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@eirbjo
Copy link
Contributor Author

eirbjo commented Nov 29, 2024

This PR consists of the following commits:

  • 46ed3d1 removes the filename instance field and replaces its uses with file.getPath()
  • 280e82d makes the file field final
  • b19b893 renames the files field to directoryListing to better express what the field contains
  • 6d9cf02 removes the contentType instance field, this can be a local variable in initializeHeaders()
  • 9ab8de1 removes an unnecessary catch/rethrow of IOException
  • 95ce976 reorders the instance fields to make the grouping more logical
  • 633b18b makes instance fields private
  • eccf281 removes three unused local variables in getInputStream
  • d990e83 updates the copyright year
  • 06b2a23 removes an explicit type parameter when calling Arrays::asList
  • 9efa3b uses enhanced for loop when constructing directory listing
  • 16ee08c replaces a String::valueOf call with Long.toString

String[] fileList = file.list();
if (fileList == null)
throw new FileNotFoundException(file.getPath() + " exists, but is not accessible");
directoryListing = Arrays.<String>asList(fileList);
Copy link
Member

Choose a reason for hiding this comment

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

Is the <String> parameterization necessary? I tested on jshell, and it seems since the destination variable's type is List<String> we don't need this explicit parameterization to distinguish varargs.

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, indeed this can be dropped. See 06b2a23.

for (int i = 0 ; i < files.size() ; i++) {
String fileName = files.get(i);
for (int i = 0; i < directoryListing.size() ; i++) {
String fileName = directoryListing.get(i);
sb.append(fileName);
sb.append("\n");
Copy link
Member

Choose a reason for hiding this comment

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

We can use a StringJoiner or stream to join the strings instead. (Note there is a suffix \n in the strings)

Copy link
Contributor Author

@eirbjo eirbjo Nov 29, 2024

Choose a reason for hiding this comment

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

StringJoiner will not include the trailing line break. I want to be conservative in this PR so reluctant to introduce streams for this.

Opted to use an enhanced for-loop. See f9efa3b.

EDIT: Aha, StringJoiner, not String.join :-) Still, I think I prefer keeping the explicit iteration for this PR.

@eirbjo eirbjo marked this pull request as ready for review November 29, 2024 18:18
@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 29, 2024
@mlbridge
Copy link

mlbridge bot commented Nov 29, 2024

Webrevs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants