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

JDK-8236048: Cleanup use of Utils.normalizeNewlines #9691

Conversation

jonathan-gibbons
Copy link
Contributor

@jonathan-gibbons jonathan-gibbons commented Jul 30, 2022

Please review a medium-size cleanup related to handling newlines in Content/HtmlTree/Text[Builder] trees.
This is the 3rd and last in the recent series of cleanup tasks in the code to generate HTML.

Until recently, the Text/TextBuilder objects were "ready-for-writing", meaning that characters like <, &, etc were encoded as entities, and newlines were represented by the platform line separator. This caused difficulties when counting the size of generated text, since these characters had to be detected and accounted for. A recent change addressed entities, but stayed clear of the newline issue, which is now addressed here.

A related performance problem is that the method Utils.normalizeNewlines always created new strings, even when it was not necessary.

With this change, newlines in Text, TextBuilder and other subtypes of Content containing text are always represented by \n, and are converted to the platform line separator as needed.

I note the following assumptions:

  • the input may contain any flavor of newlines in the user-provided documentation comments
  • the output should aways use the platform line separator (\n on Linux and macOS, \r\n on Windows etc.)

Together, these imply that some amount of the input will need to be scanned to normalize newlines at some point in the process. While it would be nice to not have to normalize newlines, it's a case or "pay now, or pay later".

Notable parts of this change:

  • Utils.normalizeNewlines is moved to be a static method in Text and rewritten to not create new strings unless needed. This allows a number of questionable imports of toolkit.utils.DocletConstants to be removed from the html.markup package. While it would be possible to call this method automatically on all strings passed to Text, TextBuilder etc, in many cases it is not necessary: put another way, it only needs to be called on strings that have come from the user, either in documentation comments or in command-line options.
  • Content.write, and the implementations thereof, are given a new parameter which gives the newline sequence to use. This is set to \n for toString and the platform separator when writing to a file.
  • DocletConstants.NL goes away and is superseded by:
    • Text.NL: always \n, to be used when adding text to a Content tree (this replaces most uses of DocletConstants.NL)
    • DocFile.PLATFORM_LINE_SEPARATOR: the platform line separator, to be used when writing to files
  • The various charCount methods no longer have to worry about use of the platform line separator
  • Assertions are used to verify that there are no \r characters added into Text/TextBuilder/RawHtml/Content

Other cleanup:

  • DocletConstants.DEFAULT_TAB_STOP_LENGTH is moved to BaseOptions which is the only class that uses it. This leaves just two remaining constants in DocletConstants which should probably be moved elsewhere as well, and the DocletConstants class deleted. That is a step too far for the work here.
  • Minor IDE-suggestions, for lambdas, enhanced switch, adding braces, etc

One test was affected by the DocletConstants.NL change, but apart from that, this is a pure-cleanup change, with no (intentional) externally visible effect.


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

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 9691

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 30, 2022

👋 Welcome back jjg! 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 openjdk bot added the rfr Pull request is ready for review label Jul 30, 2022
@openjdk
Copy link

openjdk bot commented Jul 30, 2022

@jonathan-gibbons The following label will be automatically applied to this pull request:

  • javadoc

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.

@openjdk openjdk bot added the javadoc javadoc-dev@openjdk.org label Jul 30, 2022
@mlbridge
Copy link

mlbridge bot commented Jul 30, 2022

Webrevs

Copy link
Member

@hns hns left a comment

Choose a reason for hiding this comment

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

This looks quite good. Apart from the inline comments, I wondered about the following:

  • Would the methods and constant you moved to Text be better placed in Content as they are used in other Content instances?
  • There are quite a few uses of the "\n" string literal in a content or pre-content context where the NL constant couuld be used.

@jonathan-gibbons
Copy link
Contributor Author

This looks quite good. Apart from the inline comments, I wondered about the following:

  • Would the methods and constant you moved to Text be better placed in Content as they are used in other Content instances?
  • There are quite a few uses of the "\n" string literal in a content or pre-content context where the NL constant couuld be used.
  1. I did wonder about moving the methods and constant into Content instead of Text but I think it is better to keep Content more abstract, and the new methods and constant are more "text-y". I also like the definitions being up in the markup package.

  2. Yes, there may be references in the code to \n, but I don't think we need to go overboard to find and change them. To me, this is (generally) another reason to use simple \n in text strings, because that is the more usual convention in Java source code. Note that sometimes these occurrences originate in properties files, and so cannot use the NL constant, and it seems silly to use .replace("\n", NL) on all such strings.

Copy link
Member

@hns hns left a comment

Choose a reason for hiding this comment

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

Looks good!

@openjdk
Copy link

openjdk bot commented Aug 18, 2022

@jonathan-gibbons This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8236048: Cleanup use of Utils.normalizeNewlines

Reviewed-by: hannesw

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 26 new commits pushed to the master branch:

  • f567fa0: 8292498: java/lang/invoke/lambda/LogGeneratedClassesTest.java failed with "AssertionError: only show error once expected [1] but found [0]"
  • d00e7b9: 8292544: G1: Remove virtual specifier for methods in G1EdenRegions and G1SurvivorRegions
  • f2773f6: 8292556: Clean up unused Hashtable instantiations
  • d543564: 8289334: Use CSS variables to define fonts and colors
  • 2ee9491: 8289051: C2: Cleanup PhaseCCP::analyze()
  • 32d675c: 8291775: C2: assert(r != __null && r->is_Region()) failed: this phi must have a region
  • 0d96546: 8292054: Test runtime/posixSig/TestPosixSig.java fails with 'Test failed, bad output.'
  • e8bc879: 8292443: Weak CAS VarHandle/Unsafe tests should test always-failing cases
  • 494d387: 8280152: AArch64: Reuse runtime call trampolines
  • 8b4e6ba: 8289332: Auto-generate ids for user-defined headings
  • ... and 16 more: https://git.openjdk.org/jdk/compare/e44e3f0c1976a513c6637545f49f49de84cbac02...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 18, 2022
@jonathan-gibbons
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Aug 18, 2022

Going to push as commit 1b756bf.
Since your change was applied there have been 30 commits pushed to the master branch:

  • 97e2689: 8288121: [JVMCI] Re-export the TerminatingThreadLocal functionality to the graal compiler.
  • 833bf06: 8292608: [AIX] Broken build after 8291945
  • 62a7fc6: 8292315: Tests should not rely on specific JAR file names (hotspot)
  • 20a3cb7: 8292488: JFR: Incorrect comment in ActiveRecordingEvent
  • f567fa0: 8292498: java/lang/invoke/lambda/LogGeneratedClassesTest.java failed with "AssertionError: only show error once expected [1] but found [0]"
  • d00e7b9: 8292544: G1: Remove virtual specifier for methods in G1EdenRegions and G1SurvivorRegions
  • f2773f6: 8292556: Clean up unused Hashtable instantiations
  • d543564: 8289334: Use CSS variables to define fonts and colors
  • 2ee9491: 8289051: C2: Cleanup PhaseCCP::analyze()
  • 32d675c: 8291775: C2: assert(r != __null && r->is_Region()) failed: this phi must have a region
  • ... and 20 more: https://git.openjdk.org/jdk/compare/e44e3f0c1976a513c6637545f49f49de84cbac02...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 18, 2022
@openjdk openjdk bot closed this Aug 18, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 18, 2022
@openjdk
Copy link

openjdk bot commented Aug 18, 2022

@jonathan-gibbons Pushed as commit 1b756bf.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@jonathan-gibbons jonathan-gibbons deleted the 8236048.normalizeNewlines branch November 18, 2022 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated javadoc javadoc-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

2 participants