From 13ca73ebbfa86f6aca5f86be16e6829de6d5014c Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 7 Mar 2022 09:06:56 -0800 Subject: [PATCH 01/31] Remove documentation for `--add-exports` and `--add-opens` flags These are now specified in the manifest: unknown commit PiperOrigin-RevId: 432959471 --- README.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/README.md b/README.md index 793a66c8b..cdb82c900 100644 --- a/README.md +++ b/README.md @@ -27,21 +27,6 @@ To reformat changed lines in a specific patch, use formatting. This is a deliberate design decision to unify our code formatting on a single format.* -#### JDK 16 - -The following flags are required when running on JDK 16, due to -[JEP 396: Strongly Encapsulate JDK Internals by Default](https://openjdk.java.net/jeps/396): - -``` -java \ - --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ - --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \ - --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ - --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ - --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \ - -jar google-java-format-${GJF_VERSION?}-all-deps.jar [files...] -``` - ### IntelliJ, Android Studio, and other JetBrains IDEs A From e9410fc57a4a112bfb101a74c787e0ea66081229 Mon Sep 17 00:00:00 2001 From: Michael Plump Date: Thu, 10 Mar 2022 21:57:26 -0800 Subject: [PATCH 02/31] Update the IntelliJ plugin to gjf 1.15.0. PiperOrigin-RevId: 433928217 --- idea_plugin/build.gradle | 2 +- idea_plugin/resources/META-INF/plugin.xml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/idea_plugin/build.gradle b/idea_plugin/build.gradle index 1a8bca26c..294d77e73 100644 --- a/idea_plugin/build.gradle +++ b/idea_plugin/build.gradle @@ -23,7 +23,7 @@ repositories { } ext { - googleJavaFormatVersion = "1.14.0" + googleJavaFormatVersion = "1.15.0" } apply plugin: "org.jetbrains.intellij" diff --git a/idea_plugin/resources/META-INF/plugin.xml b/idea_plugin/resources/META-INF/plugin.xml index 09360c15d..42d5f3b31 100644 --- a/idea_plugin/resources/META-INF/plugin.xml +++ b/idea_plugin/resources/META-INF/plugin.xml @@ -29,6 +29,8 @@ +
1.15.0.0
+
Updated to use google-java-format 1.15.0.
1.14.0.0
Updated to use google-java-format 1.14.
1.13.0.0
From b316fffe106496b4fc12184b50fb9bbca59c725c Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 14 Mar 2022 10:39:23 -0700 Subject: [PATCH 03/31] Mention `--add-exports` in the docs for using g-j-f as a library https://github.com/google/google-java-format/pull/760#pullrequestreview-902082926 PiperOrigin-RevId: 434504969 --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index cdb82c900..02ef5c5fc 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,18 @@ The formatter can be used in software which generates java to output more legible java code. Just include the library in your maven/gradle/etc. configuration. +`google-java-format` uses internal javac APIs for parsing Java source. The +following JVM flags are required when running on JDK 16 and newer, due to +[JEP 396: Strongly Encapsulate JDK Internals by Default](https://openjdk.java.net/jeps/396): + +``` +--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED +--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED +--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED +--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED +--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED +``` + #### Maven ```xml From 27670bee4fb54b237344dc66bd7e243c7e16c94b Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 25 Mar 2022 15:33:05 -0700 Subject: [PATCH 04/31] Avoid ignoring constructor results PiperOrigin-RevId: 437348911 --- .../main/java/com/google/googlejavaformat/java/JavaInput.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java index 165bdebb1..eee210e20 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java @@ -49,6 +49,7 @@ import javax.tools.Diagnostic; import javax.tools.DiagnosticCollector; import javax.tools.DiagnosticListener; +import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; import javax.tools.JavaFileObject.Kind; import javax.tools.SimpleJavaFileObject; @@ -349,7 +350,8 @@ static ImmutableList buildToks(String text, ImmutableSet stopTok stopTokens = ImmutableSet.builder().addAll(stopTokens).add(TokenKind.EOF).build(); Context context = new Context(); Options.instance(context).put("--enable-preview", "true"); - new JavacFileManager(context, true, UTF_8); + JavaFileManager fileManager = new JavacFileManager(context, false, UTF_8); + context.put(JavaFileManager.class, fileManager); DiagnosticCollector diagnosticCollector = new DiagnosticCollector<>(); context.put(DiagnosticListener.class, diagnosticCollector); Log log = Log.instance(context); From 1551cc251b0cb41ae5669a7fd4ad7068d7890f77 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Sun, 8 May 2022 14:24:47 -0700 Subject: [PATCH 05/31] Update ci.yml Add 19-ea, use released 18 --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 016b8e776..9072785f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,19 +29,19 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - java: [ 17, 11 ] + java: [ 18, 17, 11 ] experimental: [ false ] include: # Only test on macos and windows with a single recent JDK to avoid a # combinatorial explosion of test configurations. - os: macos-latest - java: 17 + java: 18 experimental: false - os: windows-latest - java: 17 + java: 18 experimental: false - os: ubuntu-latest - java: 18-ea + java: 19-ea experimental: true runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.experimental }} From 04fbd4fe3e6bd40cd09eb8e504b2489ddd69063e Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 11 May 2022 18:43:34 -0700 Subject: [PATCH 06/31] Update tycho version to see if it helps w/ JDK 19-EA Startblock: unknown commit is submitted PiperOrigin-RevId: 448133464 --- eclipse_plugin/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eclipse_plugin/pom.xml b/eclipse_plugin/pom.xml index 534e685f0..4f108340f 100644 --- a/eclipse_plugin/pom.xml +++ b/eclipse_plugin/pom.xml @@ -32,7 +32,7 @@ UTF-8 - 2.6.0 + 2.7.3 From e0132d0fb62624d6616426e8c9e450a814f2db02 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 17 Jun 2022 10:34:23 -0700 Subject: [PATCH 07/31] Bump guava.version from 31.0.1-jre to 31.1-jre Bumps `guava.version` from 31.0.1-jre to 31.1-jre. Updates `guava` from 31.0.1-jre to 31.1-jre
Release notes

Sourced from guava's releases.

31.1

Maven

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>31.1-jre</version>
  <!-- or, for Android: -->
  <version>31.1-android</version>
</dependency>

Jar files

Guava requires one runtime dependency, which you can download here:

Javadoc

JDiff

Changelog

  • base: Deprecated the Throwables methods lazyStackTrace and lazyStackTraceIsLazy. They are no longer useful on any current platform. (6ebd7d8648)
  • collect: Added a new method ImmutableMap.Builder.buildKeepingLast(), which keeps the last value for any given key rather than throwing an exception when a key appears more than once. (68500b2c09)
  • collect: As a side-effect of the buildKeepingLast() change, the idiom ImmutableList.copyOf(Maps.transformValues(map, function)) may produce different results if function has side-effects. (This is not recommended.) (68500b2c09)
  • hash: Added Hashing.fingerprint2011(). (13f703c25f)
  • io: Changed ByteStreams.nullOutputStream() to follow the contract of OutputStream.write by throwing an exception if the range of bytes is out of bounds. (1cd85d01c9)
  • net: Added @CheckReturnValue to the package (with a few exceptions). (a0e2577de6)
  • net: Added HttpHeaders constant for Access-Control-Allow-Private-Network. (6dabbdf9c9)
  • util.concurrent: Added accumulate/update methods for AtomicDouble and AtomicDoubleArray. (2d875d327a)

APIs promoted from @Beta

  • base: Throwables methods getCausalChain and getCauseAs (dd462afa6b)
  • collect: Streams methods mapWithIndex and findLast (8079a29463)
  • collect: the remaining methods in Comparators: min, max, lexicographical, emptiesFirst, emptiesLast, isInOrder, isInStrictOrder (a3e411c3a4)
  • escape: various APIs (468c68a6ac)

... (truncated)

Commits

Updates `guava-testlib` from 31.0.1-jre to 31.1-jre
Release notes

Sourced from guava-testlib's releases.

31.1

Maven

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>31.1-jre</version>
  <!-- or, for Android: -->
  <version>31.1-android</version>
</dependency>

Jar files

Guava requires one runtime dependency, which you can download here:

Javadoc

JDiff

Changelog

  • base: Deprecated the Throwables methods lazyStackTrace and lazyStackTraceIsLazy. They are no longer useful on any current platform. (6ebd7d8648)
  • collect: Added a new method ImmutableMap.Builder.buildKeepingLast(), which keeps the last value for any given key rather than throwing an exception when a key appears more than once. (68500b2c09)
  • collect: As a side-effect of the buildKeepingLast() change, the idiom ImmutableList.copyOf(Maps.transformValues(map, function)) may produce different results if function has side-effects. (This is not recommended.) (68500b2c09)
  • hash: Added Hashing.fingerprint2011(). (13f703c25f)
  • io: Changed ByteStreams.nullOutputStream() to follow the contract of OutputStream.write by throwing an exception if the range of bytes is out of bounds. (1cd85d01c9)
  • net: Added @CheckReturnValue to the package (with a few exceptions). (a0e2577de6)
  • net: Added HttpHeaders constant for Access-Control-Allow-Private-Network. (6dabbdf9c9)
  • util.concurrent: Added accumulate/update methods for AtomicDouble and AtomicDoubleArray. (2d875d327a)

APIs promoted from @Beta

  • base: Throwables methods getCausalChain and getCauseAs (dd462afa6b)
  • collect: Streams methods mapWithIndex and findLast (8079a29463)
  • collect: the remaining methods in Comparators: min, max, lexicographical, emptiesFirst, emptiesLast, isInOrder, isInStrictOrder (a3e411c3a4)
  • escape: various APIs (468c68a6ac)

... (truncated)

Commits

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Fixes #754 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/754 from google:dependabot/maven/guava.version-31.1-jre 70a8d22d3181232e19775b25083779900e213972 PiperOrigin-RevId: 455648330 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f6c7b590d..3e3c865e9 100644 --- a/pom.xml +++ b/pom.xml @@ -87,7 +87,7 @@ UTF-8 1.8 - 31.0.1-jre + 31.1-jre 1.1.3 3.21.2 2.11.0 From 5c6f6bcc7a465b084d069ca014629330d587bc8b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 9 Aug 2022 08:39:08 -0700 Subject: [PATCH 08/31] Bump native-maven-plugin from 0.9.9 to 0.9.13 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bumps [native-maven-plugin](https://github.com/graalvm/native-build-tools) from 0.9.9 to 0.9.13.
Release notes

Sourced from native-maven-plugin's releases.

0.9.12

What's Changed

Read what's new in the documentation.

Full Changelog: https://github.com/graalvm/native-build-tools/compare/0.9.11...0.9.12

0.9.11

What's Changed

Read what's new in the documentation.

New Contributors

Full Changelog: https://github.com/graalvm/native-build-tools/compare/0.9.10...0.9.11

0.9.10

What's Changed

Read what's new in the documentation.

... (truncated)

Commits

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=org.graalvm.buildtools:native-maven-plugin&package-manager=maven&previous-version=0.9.9&new-version=0.9.13)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Fixes #799 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/799 from google:dependabot/maven/org.graalvm.buildtools-native-maven-plugin-0.9.13 dacb9ae64ffe78550813e9f1737b5e5fd6b735cf PiperOrigin-RevId: 466369202 --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pom.xml b/core/pom.xml index 1d4715957..07716f615 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -256,7 +256,7 @@ org.graalvm.buildtools native-maven-plugin - 0.9.9 + 0.9.13 true From 858e6c6d6be56ed2b705b388c09c316ae09a1f02 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 9 Aug 2022 08:39:21 -0700 Subject: [PATCH 09/31] Bump maven-javadoc-plugin from 3.3.1 to 3.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bumps [maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.3.1 to 3.4.0.
Release notes

Sourced from maven-javadoc-plugin's releases.

3.3.2

What's Changed

... (truncated)

Commits
  • 40cc602 [maven-release-plugin] prepare release maven-javadoc-plugin-3.4.0
  • 0c6b32f [MJAVADOC-714] Upgrade to Maven 3.2.5
  • 506cb74 [MJAVADOC-696] Invalid anchors in Javadoc and plugin mojo
  • 47d03d3 [MJAVADOC-712] Remove remains of org.codehaus.doxia.sink.Sink
  • 5fae3b6 [MJAVADOC-711] Upgrade plugins in ITs
  • 03ca843 Bump maven-archiver from 3.5.1 to 3.5.2
  • 5dcfa6e Bump plexus-archiver from 4.2.6 to 4.2.7
  • ca00601 Bump junit in /src/it/projects/MJAVADOC-498_modulepath
  • 2583554 Bump commons-io from 2.2 to 2.7 in /src/it/projects/MJAVADOC-437/module2
  • 9dd7bdd use shared gh action/release-drafter (#128)
  • Additional commits viewable in compare view

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=org.apache.maven.plugins:maven-javadoc-plugin&package-manager=maven&previous-version=3.3.1&new-version=3.4.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Fixes #776 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/776 from google:dependabot/maven/org.apache.maven.plugins-maven-javadoc-plugin-3.4.0 84e66973c58e17a4d0e336c5577ad73ca0ba060b PiperOrigin-RevId: 466369247 --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 3e3c865e9..4cd7afeea 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,7 @@ 2.11.0 1.9 1.0.1 - 3.3.1 + 3.4.0 3.2.1
@@ -173,7 +173,7 @@ maven-javadoc-plugin - 3.3.1 + 3.4.0 maven-gpg-plugin @@ -264,7 +264,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.3.1 + 3.4.0 none From f70db1b041492e4048f9c2c768667330098d0247 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 9 Aug 2022 08:39:46 -0700 Subject: [PATCH 10/31] Bump maven-dependency-plugin from 3.2.0 to 3.3.0 Bumps [maven-dependency-plugin](https://github.com/apache/maven-dependency-plugin) from 3.2.0 to 3.3.0.
Commits
  • e52bc02 [maven-release-plugin] prepare release maven-dependency-plugin-3.3.0
  • 0ec0a52 Fix Jenkins url
  • 77e42ca [MDEP-796] Upgrade Maven Parent to 35
  • 78976c0 [MDEP-795] Update Jetty to 9.4.45.v20220203 (#202)
  • a8d4690 [MDEP-788] Upgrade maven-reporting-impl to version 3.1.0
  • 25ca833 (doc) Update link to Github PR docs
  • 76d59f0 [MDEP-789] Improve documentation of analyze - Non-test scoped
  • b66d2b2 Bump mockito-core from 4.2.0 to 4.3.1
  • b057234 Bump slf4j-simple from 1.7.32 to 1.7.36
  • f64d4f7 [MDEP-787] allow ignoring non-test-scoped dependencies
  • Additional commits viewable in compare view

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=org.apache.maven.plugins:maven-dependency-plugin&package-manager=maven&previous-version=3.2.0&new-version=3.3.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Fixes #794 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/794 from google:dependabot/maven/org.apache.maven.plugins-maven-dependency-plugin-3.3.0 1ca666e76b569ca5d01c404fd18d57138ec3d62b PiperOrigin-RevId: 466369343 --- eclipse_plugin/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eclipse_plugin/pom.xml b/eclipse_plugin/pom.xml index 4f108340f..5bd3a829b 100644 --- a/eclipse_plugin/pom.xml +++ b/eclipse_plugin/pom.xml @@ -56,7 +56,7 @@ org.apache.maven.plugins maven-dependency-plugin - 3.2.0 + 3.3.0 copy-dependencies From 309d92ae66a37477f0d2a365475ab99ee85e73e0 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 30 Aug 2022 16:00:56 -0700 Subject: [PATCH 11/31] Adjust parameter comment style PiperOrigin-RevId: 471112648 --- .../java/com/google/googlejavaformat/java/Formatter.java | 6 +++--- .../google/googlejavaformat/java/RemoveUnusedImports.java | 5 ++++- .../com/google/googlejavaformat/java/StringWrapper.java | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index aac829dd0..9965a6ff8 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -136,9 +136,9 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept JavacParser parser = parserFactory.newParser( javaInput.getText(), - /*keepDocComments=*/ true, - /*keepEndPos=*/ true, - /*keepLineMap=*/ true); + /* keepDocComments= */ true, + /* keepEndPos= */ true, + /* keepLineMap= */ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; diff --git a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java index 20e55e945..ecb30eed6 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java +++ b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java @@ -252,7 +252,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept ParserFactory parserFactory = ParserFactory.instance(context); JavacParser parser = parserFactory.newParser( - javaInput, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); + javaInput, + /* keepDocComments= */ true, + /* keepEndPos= */ true, + /* keepLineMap= */ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; Iterable> errorDiagnostics = diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index c0f16e9b5..1d309308c 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -396,7 +396,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) { ParserFactory parserFactory = ParserFactory.instance(context); JavacParser parser = parserFactory.newParser( - source, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); + source, /* keepDocComments= */ true, /* keepEndPos= */ true, /* keepLineMap= */ true); unit = parser.parseCompilationUnit(); unit.sourcefile = sjfo; Iterable> errorDiagnostics = From 3d03777aed468582c82d0fed4b7ada62a595ebe5 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 6 Sep 2022 14:54:00 -0700 Subject: [PATCH 12/31] Remove a link to an unmaintained project Fixes https://github.com/google/google-java-format/issues/822 PiperOrigin-RevId: 472563830 --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 02ef5c5fc..2d53f8ae5 100644 --- a/README.md +++ b/README.md @@ -79,9 +79,6 @@ Implementation`. pre-commit git hook. * SBT plugins * [sbt/sbt-java-formatter](https://github.com/sbt/sbt-java-formatter) -* [maltzj/google-style-precommit-hook](https://github.com/maltzj/google-style-precommit-hook): - A pre-commit (pre-commit.com) hook that will automatically run GJF whenever - you commit code to your repository * [Github Actions](https://github.com/features/actions) * [googlejavaformat-action](https://github.com/axel-op/googlejavaformat-action): Automatically format your Java files when you push on github From 5496c3af114e74e0a385e044c09d10e96a9e4127 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 6 Oct 2022 10:11:48 -0700 Subject: [PATCH 13/31] Update ci.yml --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9072785f9..79a260eab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,19 +29,19 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - java: [ 18, 17, 11 ] + java: [ 19, 17, 11 ] experimental: [ false ] include: # Only test on macos and windows with a single recent JDK to avoid a # combinatorial explosion of test configurations. - os: macos-latest - java: 18 + java: 19 experimental: false - os: windows-latest - java: 18 + java: 19 experimental: false - os: ubuntu-latest - java: 19-ea + java: 20-ea experimental: true runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.experimental }} From 72248abd0092af2893bfb267832850ab7b3d47a4 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 6 Oct 2022 11:36:11 -0700 Subject: [PATCH 14/31] Update tycho version To include the fix for https://github.com/eclipse-tycho/tycho/issues/958 PiperOrigin-RevId: 479370465 --- eclipse_plugin/pom.xml | 2 +- pom.xml | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/eclipse_plugin/pom.xml b/eclipse_plugin/pom.xml index 5bd3a829b..9e6acdac0 100644 --- a/eclipse_plugin/pom.xml +++ b/eclipse_plugin/pom.xml @@ -32,7 +32,7 @@ UTF-8 - 2.7.3 + 3.0.0 diff --git a/pom.xml b/pom.xml index 4cd7afeea..7066ecb6d 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,6 @@ core - eclipse_plugin Google Java Format Parent @@ -362,5 +361,14 @@ + + eclipse + + [17,) + + + eclipse_plugin + + From 41d6e373ca84f1980f5ef52f683ae00cf6ec87b9 Mon Sep 17 00:00:00 2001 From: Alan Malloy Date: Fri, 7 Oct 2022 12:09:41 -0700 Subject: [PATCH 15/31] Improve error message for malformed edits PiperOrigin-RevId: 479639885 --- .../java/com/google/googlejavaformat/java/StringWrapper.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index 1d309308c..d801c9b86 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -96,7 +96,9 @@ static String wrap(final int columnLimit, String input, Formatter formatter) if (!expected.equals(actual)) { throw new FormatterException( String.format( - "Something has gone terribly wrong. Please file a bug: " + "Something has gone terribly wrong. We planned to make the below formatting change," + + " but have aborted because it would unexpectedly change the AST.\n" + + "Please file a bug: " + "https://github.com/google/google-java-format/issues/new" + "\n\n=== Actual: ===\n%s\n=== Expected: ===\n%s\n", actual, expected)); From 198cc39e650fb2c744aeac80b6b834691a6b4695 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 25 Oct 2022 09:28:51 -0700 Subject: [PATCH 16/31] Update Error Prone version PiperOrigin-RevId: 483696374 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7066ecb6d..150173ee6 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ 31.1-jre 1.1.3 3.21.2 - 2.11.0 + 2.16 1.9 1.0.1 3.4.0 From c1ea933ba37f27810d3ae01b5958514c02425009 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 27 Oct 2022 16:17:26 -0700 Subject: [PATCH 17/31] Stop supporting JDK 14 in google-java-format, and update to remove uses of reflection for APIs that can be accessed directly in JDK 17 PiperOrigin-RevId: 484376678 --- core/pom.xml | 6 +- .../googlejavaformat/java/Formatter.java | 4 +- .../Java17InputAstVisitor.java} | 82 ++++--------------- 3 files changed, 19 insertions(+), 73 deletions(-) rename core/src/main/java/com/google/googlejavaformat/java/{java14/Java14InputAstVisitor.java => java17/Java17InputAstVisitor.java} (72%) diff --git a/core/pom.xml b/core/pom.xml index 07716f615..10565c66a 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -227,7 +227,7 @@ jdk11 - (,14) + (,17) @@ -236,14 +236,14 @@ maven-compiler-plugin - **/Java14InputAstVisitor.java + **/Java17InputAstVisitor.java maven-javadoc-plugin - com.google.googlejavaformat.java.java14 + com.google.googlejavaformat.java.java17 diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index 9965a6ff8..841e88a8a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -151,10 +151,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept OpsBuilder builder = new OpsBuilder(javaInput, javaOutput); // Output the compilation unit. JavaInputAstVisitor visitor; - if (Runtime.version().feature() >= 14) { + if (Runtime.version().feature() >= 17) { try { visitor = - Class.forName("com.google.googlejavaformat.java.java14.Java14InputAstVisitor") + Class.forName("com.google.googlejavaformat.java.java17.Java17InputAstVisitor") .asSubclass(JavaInputAstVisitor.class) .getConstructor(OpsBuilder.class, int.class) .newInstance(builder, options.indentationMultiplier()); diff --git a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java similarity index 72% rename from core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java rename to core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java index 890687fe2..a0561e2fe 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java @@ -12,7 +12,7 @@ * the License. */ -package com.google.googlejavaformat.java.java14; +package com.google.googlejavaformat.java.java17; import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.Iterables.getOnlyElement; @@ -25,6 +25,7 @@ import com.sun.source.tree.AnnotationTree; import com.sun.source.tree.BindingPatternTree; import com.sun.source.tree.BlockTree; +import com.sun.source.tree.CaseLabelTree; import com.sun.source.tree.CaseTree; import com.sun.source.tree.ClassTree; import com.sun.source.tree.CompilationUnitTree; @@ -39,39 +40,23 @@ import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.tree.TreeInfo; -import java.lang.reflect.Method; import java.util.List; import java.util.Optional; import javax.lang.model.element.Name; /** - * Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified for - * Java 14. + * Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified in + * Java 17. */ -public class Java14InputAstVisitor extends JavaInputAstVisitor { - private static final Method COMPILATION_UNIT_TREE_GET_MODULE = - maybeGetMethod(CompilationUnitTree.class, "getModule"); - private static final Method CLASS_TREE_GET_PERMITS_CLAUSE = - maybeGetMethod(ClassTree.class, "getPermitsClause"); - private static final Method BINDING_PATTERN_TREE_GET_VARIABLE = - maybeGetMethod(BindingPatternTree.class, "getVariable"); - private static final Method BINDING_PATTERN_TREE_GET_TYPE = - maybeGetMethod(BindingPatternTree.class, "getType"); - private static final Method BINDING_PATTERN_TREE_GET_BINDING = - maybeGetMethod(BindingPatternTree.class, "getBinding"); - private static final Method CASE_TREE_GET_LABELS = maybeGetMethod(CaseTree.class, "getLabels"); +public class Java17InputAstVisitor extends JavaInputAstVisitor { - public Java14InputAstVisitor(OpsBuilder builder, int indentMultiplier) { + public Java17InputAstVisitor(OpsBuilder builder, int indentMultiplier) { super(builder, indentMultiplier); } @Override protected void handleModule(boolean first, CompilationUnitTree node) { - if (COMPILATION_UNIT_TREE_GET_MODULE == null) { - // Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464 - return; - } - ModuleTree module = (ModuleTree) invoke(COMPILATION_UNIT_TREE_GET_MODULE, node); + ModuleTree module = node.getModule(); if (module != null) { if (!first) { builder.blankLineWanted(BlankLineWanted.YES); @@ -84,30 +69,15 @@ protected void handleModule(boolean first, CompilationUnitTree node) { @Override protected List getPermitsClause(ClassTree node) { - if (CLASS_TREE_GET_PERMITS_CLAUSE != null) { - return (List) invoke(CLASS_TREE_GET_PERMITS_CLAUSE, node); - } else { - // Java < 15 - return super.getPermitsClause(node); - } + return node.getPermitsClause(); } @Override public Void visitBindingPattern(BindingPatternTree node, Void unused) { sync(node); - if (BINDING_PATTERN_TREE_GET_VARIABLE != null) { - VariableTree variableTree = (VariableTree) invoke(BINDING_PATTERN_TREE_GET_VARIABLE, node); - visitBindingPattern( - variableTree.getModifiers(), variableTree.getType(), variableTree.getName()); - } else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null) { - Tree type = (Tree) invoke(BINDING_PATTERN_TREE_GET_TYPE, node); - Name name = (Name) invoke(BINDING_PATTERN_TREE_GET_BINDING, node); - visitBindingPattern(/* modifiers= */ null, type, name); - } else { - throw new LinkageError( - "BindingPatternTree must have either getVariable() or both getType() and getBinding()," - + " but does not"); - } + VariableTree variableTree = node.getVariable(); + visitBindingPattern( + variableTree.getModifiers(), variableTree.getType(), variableTree.getName()); return null; } @@ -248,17 +218,9 @@ public Void visitCase(CaseTree node, Void unused) { sync(node); markForPartialFormat(); builder.forcedBreak(); - List labels; - boolean isDefault; - if (CASE_TREE_GET_LABELS != null) { - labels = (List) invoke(CASE_TREE_GET_LABELS, node); - isDefault = - labels.size() == 1 - && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL"); - } else { - labels = node.getExpressions(); - isDefault = labels.isEmpty(); - } + List labels = node.getLabels(); + boolean isDefault = + labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL"); if (isDefault) { token("default", plusTwo); } else { @@ -305,20 +267,4 @@ public Void visitCase(CaseTree node, Void unused) { } return null; } - - private static Method maybeGetMethod(Class c, String name) { - try { - return c.getMethod(name); - } catch (ReflectiveOperationException e) { - return null; - } - } - - private static Object invoke(Method m, Object target) { - try { - return m.invoke(target); - } catch (ReflectiveOperationException e) { - throw new LinkageError(e.getMessage(), e); - } - } } From dda43216576c4cb84e2497424b9ca9246f2687e5 Mon Sep 17 00:00:00 2001 From: google-java-format Team Date: Mon, 7 Nov 2022 05:11:20 -0800 Subject: [PATCH 18/31] Reformat parameter comments in the canonical Java format (`/* foo= */`). This only formats comments that would already trigger the `ParameterComment` check; it shouldn't make things parameter comments which could then cause compilation errors. PiperOrigin-RevId: 486631656 --- .../googlejavaformat/CommentsHelper.java | 19 +++++++ .../java/com/google/googlejavaformat/Doc.java | 5 +- .../java/JavaCommentsHelper.java | 10 ++-- .../java/testdata/B21283374.output | 56 +++++++++---------- .../java/testdata/B26694550.output | 6 +- .../java/testdata/ParameterComment.input | 26 +++++++++ .../java/testdata/ParameterComment.output | 34 +++++++++++ 7 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output diff --git a/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java b/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java index 45e507bbd..2b789612c 100644 --- a/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java +++ b/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java @@ -14,6 +14,10 @@ package com.google.googlejavaformat; +import com.google.googlejavaformat.Input.Tok; +import java.util.Optional; +import java.util.regex.Pattern; + /** * Rewrite comments. This interface is implemented by {@link * com.google.googlejavaformat.java.JavaCommentsHelper JavaCommentsHelper}. @@ -28,4 +32,19 @@ public interface CommentsHelper { * @return the rewritten comment */ String rewrite(Input.Tok tok, int maxWidth, int column0); + + static Optional reformatParameterComment(Tok tok) { + if (!tok.isSlashStarComment()) { + return Optional.empty(); + } + var match = PARAMETER_COMMENT.matcher(tok.getOriginalText()); + if (!match.matches()) { + return Optional.empty(); + } + return Optional.of(String.format("/* %s= */", match.group(1))); + } + + Pattern PARAMETER_COMMENT = + Pattern.compile( + "/\\*\\s*(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)(\\Q...\\E)?\\s*=\\s*\\*/"); } diff --git a/core/src/main/java/com/google/googlejavaformat/Doc.java b/core/src/main/java/com/google/googlejavaformat/Doc.java index 35acca367..d638ddb40 100644 --- a/core/src/main/java/com/google/googlejavaformat/Doc.java +++ b/core/src/main/java/com/google/googlejavaformat/Doc.java @@ -15,6 +15,7 @@ package com.google.googlejavaformat; import static com.google.common.collect.Iterables.getLast; +import static com.google.googlejavaformat.CommentsHelper.reformatParameterComment; import static java.lang.Math.max; import com.google.common.base.MoreObjects; @@ -727,7 +728,7 @@ float computeWidth() { // Account for line comments with missing spaces, see computeFlat. return tok.length() + 1; } else { - return tok.length(); + return reformatParameterComment(tok).map(String::length).orElse(tok.length()); } } return idx != -1 ? Float.POSITIVE_INFINITY : (float) tok.length(); @@ -741,7 +742,7 @@ String computeFlat() { if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) { return "// " + tok.getOriginalText().substring("//".length()); } - return tok.getOriginalText(); + return reformatParameterComment(tok).orElse(tok.getOriginalText()); } @Override diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java b/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java index 346324a45..d34ecc43f 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java @@ -53,11 +53,13 @@ public String rewrite(Tok tok, int maxWidth, int column0) { } if (tok.isSlashSlashComment()) { return indentLineComments(lines, column0); - } else if (javadocShaped(lines)) { - return indentJavadoc(lines, column0); - } else { - return preserveIndentation(lines, column0); } + return CommentsHelper.reformatParameterComment(tok) + .orElseGet( + () -> + javadocShaped(lines) + ? indentJavadoc(lines, column0) + : preserveIndentation(lines, column0)); } // For non-javadoc-shaped block comments, shift the entire block to the correct diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output index f98a37b2f..777303e61 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output @@ -14,41 +14,41 @@ class B21283374 { { f( - /*flagA=*/ Boolean.TRUE, - /*flagB=*/ Boolean.FALSE, - /*flagC=*/ Boolean.FALSE, - /*flagD=*/ Boolean.FALSE, - /*flagE=*/ Boolean.FALSE, - /*flagF=*/ Boolean.FALSE, - /*flagG=*/ Boolean.FALSE, - /*flagH=*/ Boolean.FALSE, - /*flagI=*/ Boolean.FALSE, - /*flagJ=*/ Boolean.FALSE, - /*flagK=*/ Boolean.FALSE); + /* flagA= */ Boolean.TRUE, + /* flagB= */ Boolean.FALSE, + /* flagC= */ Boolean.FALSE, + /* flagD= */ Boolean.FALSE, + /* flagE= */ Boolean.FALSE, + /* flagF= */ Boolean.FALSE, + /* flagG= */ Boolean.FALSE, + /* flagH= */ Boolean.FALSE, + /* flagI= */ Boolean.FALSE, + /* flagJ= */ Boolean.FALSE, + /* flagK= */ Boolean.FALSE); f( - /*flagA=*/ Boolean.TRUE, - /*flagB=*/ Boolean.FALSE, - /*flagC=*/ Boolean.FALSE, - /*flagD=*/ Boolean.FALSE, - /*flagE=*/ Boolean.FALSE, - /*flagF=*/ Boolean.FALSE, - /*flagG=*/ Boolean.FALSE, - /*flagH=*/ Boolean.FALSE, - /*flagI=*/ Boolean.FALSE, - /*flagJ=*/ Boolean.FALSE, - /*flagK=*/ Boolean.FALSE); + /* flagA= */ Boolean.TRUE, + /* flagB= */ Boolean.FALSE, + /* flagC= */ Boolean.FALSE, + /* flagD= */ Boolean.FALSE, + /* flagE= */ Boolean.FALSE, + /* flagF= */ Boolean.FALSE, + /* flagG= */ Boolean.FALSE, + /* flagH= */ Boolean.FALSE, + /* flagI= */ Boolean.FALSE, + /* flagJ= */ Boolean.FALSE, + /* flagK= */ Boolean.FALSE); - assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something("")); - assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something("")); - assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something("")); + assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something("")); + assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something("")); + assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something("")); - f(/*paramName=*/ false); + f(/* paramName= */ false); assertThat__________________________________________________________( - /*paramName=*/ false, thingy); + /* paramName= */ false, thingy); assertThat__________________________________________________________( - /*paramName=*/ false, thingy); + /* paramName= */ false, thingy); f( arg1, /* which arg is this attached to? */ diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output index 945a40fce..aedafb779 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output @@ -3,15 +3,15 @@ class B26694550 { /* === not a param comment */ fffffffffffffffffffffffffffffff( ImmutableList.copyOf(keys), /*&=*/ - /*keepGoing=*/ false, + /* keepGoing= */ false, ggggggggggggggggggggggggggggggggggggggggggg); fffffffffffffffffffffffffffffff( ImmutableList.copyOf(keys), - /*keepGoing=*/ false, + /* keepGoing= */ false, ggggggggggggggggggggggggggggggggggggggggggg); fffffffffffffffffffffffffffffff( ImmutableList.copyOf(keys), - /*foo_bar=*/ false, + /* foo_bar= */ false, /*foo-bar=*/ false, ggggggggggggggggggggggggggggggggggggggggggg); } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input new file mode 100644 index 000000000..58590ac4e --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input @@ -0,0 +1,26 @@ +package com.google.googlejavaformat.java.test; + +/** Tests for formatting of ParameterComments. */ +class Q { + static void f(int a) { + f(/*a=*/ 1); + f( + /* a= */ 1 + ); + } + static void g(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) { + g( + /*a=*/ 1, + /*b=*/ 1, + /*c=*/ 1, + /*d=*/ 1, + /*e=*/ 1, + /*f=*/ 1, + /*g=*/ 1, + /*h=*/ 1, + /*i=*/ 1, + /*j=*/ 1, + /*k=*/ 1); + g(/*a=*/ 1, /*b=*/ 1, /*c=*/ 1, /*d=*/ 1, /*e=*/ 1, /*f=*/ 1, /*g=*/ 1, /*h=*/ 1, /*i=*/ 1); + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output new file mode 100644 index 000000000..5616ec0da --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output @@ -0,0 +1,34 @@ +package com.google.googlejavaformat.java.test; + +/** Tests for formatting of ParameterComments. */ +class Q { + static void f(int a) { + f(/* a= */ 1); + f(/* a= */ 1); + } + + static void g(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) { + g( + /* a= */ 1, + /* b= */ 1, + /* c= */ 1, + /* d= */ 1, + /* e= */ 1, + /* f= */ 1, + /* g= */ 1, + /* h= */ 1, + /* i= */ 1, + /* j= */ 1, + /* k= */ 1); + g( + /* a= */ 1, + /* b= */ 1, + /* c= */ 1, + /* d= */ 1, + /* e= */ 1, + /* f= */ 1, + /* g= */ 1, + /* h= */ 1, + /* i= */ 1); + } +} From 198fa4236724f0305f1d4fb9447ce0b48984746f Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 8 Nov 2022 08:16:05 -0800 Subject: [PATCH 19/31] Format long exception lists one-per-line PiperOrigin-RevId: 486955708 --- .../java/JavaInputAstVisitor.java | 2 +- .../googlejavaformat/java/testdata/M.output | 108 +++++++++++++++--- 2 files changed, 91 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index daed2502e..75f36873b 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -2639,7 +2639,7 @@ private void visitThrowsClause(List thrownExceptionTyp for (ExpressionTree thrownExceptionType : thrownExceptionTypes) { if (!first) { token(","); - builder.breakToFill(" "); + builder.breakOp(" "); } scan(thrownExceptionType, null); first = false; diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output index dbad1bb67..d28d5dd7c 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output @@ -36,8 +36,16 @@ class M { /** JavaDoc. */ void methodName3(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 {} + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 {} /** JavaDoc. */ void methodName4( @@ -114,8 +122,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 {} + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 {} Pair< Pair, Pair>, Pair, Pair>>, @@ -135,8 +151,16 @@ class M { Pair, Pair>, Pair, Pair>>, Pair, Pair>, Pair, Pair>>> methodName11(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -225,8 +249,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -338,8 +370,16 @@ class M { T30, T31> T methodName22(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -518,8 +558,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -640,8 +688,16 @@ class M { Pair, Pair>, Pair, Pair>>, Pair, Pair>, Pair, Pair>>> methodName(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, - Exception6, Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -829,8 +885,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, - Exception6, Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -1011,6 +1075,14 @@ class M { ZZZZZZZZZZ z, ZZZZZZZZZZ z, ZZZZZZZZZZ z) - throws EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, - EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE {} + throws EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE {} } From e3227913ca7ca9f53c3e5098c5d702a50df0304c Mon Sep 17 00:00:00 2001 From: google-java-format Team Date: Wed, 16 Nov 2022 09:09:26 -0800 Subject: [PATCH 20/31] Preserve ellipses when formatting parameter comments. PiperOrigin-RevId: 488956140 --- .../main/java/com/google/googlejavaformat/CommentsHelper.java | 2 +- .../googlejavaformat/java/testdata/ParameterComment.input | 3 +++ .../googlejavaformat/java/testdata/ParameterComment.output | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java b/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java index 2b789612c..1e33003b0 100644 --- a/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java +++ b/core/src/main/java/com/google/googlejavaformat/CommentsHelper.java @@ -46,5 +46,5 @@ static Optional reformatParameterComment(Tok tok) { Pattern PARAMETER_COMMENT = Pattern.compile( - "/\\*\\s*(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)(\\Q...\\E)?\\s*=\\s*\\*/"); + "/\\*\\s*(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(\\Q...\\E)?)\\s*=\\s*\\*/"); } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input index 58590ac4e..decebe92a 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input @@ -23,4 +23,7 @@ class Q { /*k=*/ 1); g(/*a=*/ 1, /*b=*/ 1, /*c=*/ 1, /*d=*/ 1, /*e=*/ 1, /*f=*/ 1, /*g=*/ 1, /*h=*/ 1, /*i=*/ 1); } + static void h(Object... xs) { + h(/*xs...=*/ null); + } } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output index 5616ec0da..d26ca1135 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output @@ -31,4 +31,8 @@ class Q { /* h= */ 1, /* i= */ 1); } + + static void h(Object... xs) { + h(/* xs...= */ null); + } } From 8afb866e7c5f26846c475c0cf6d7e314861b072c Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 21 Nov 2022 14:38:29 -0800 Subject: [PATCH 21/31] Create a build rule for compiling deps at the Java STS language level This allows building the Java > 11 logic in google-java-format into the jar regardless of what language level is configured for the entire build, and then dynamically enabling it when running on a new enough JDK, while remaining compatible with earlier JDK versions. PiperOrigin-RevId: 490074589 --- .../com/google/googlejavaformat/java/testdata/B20844369.input | 2 +- .../com/google/googlejavaformat/java/testdata/B20844369.output | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input index 7317f17b9..86e46d50e 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input @@ -1,6 +1,6 @@ public class B20844369 { private static final String ID_PATTERN = - // TODO(user): add min/max lengths for the numbers here, e.g. android ID + // TODO(daw): add min/max lengths for the numbers here, e.g. android ID "(?:(?\\d+)\\+)?" // optional Android ID + "(?\\d+)" // type + ":" diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output index 62f9721b6..982dc2b5d 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output @@ -1,6 +1,6 @@ public class B20844369 { private static final String ID_PATTERN = - // TODO(user): add min/max lengths for the numbers here, e.g. android ID + // TODO(daw): add min/max lengths for the numbers here, e.g. android ID "(?:(?\\d+)\\+)?" // optional Android ID + "(?\\d+)" // type + ":" From d9ddb94db1d90a240f901949514f35df4e4c3fc4 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 15 Dec 2022 14:44:45 -0800 Subject: [PATCH 22/31] Recognize new JSpecify package name So that its `@Nullable` annotation is treated as a type-use annotation and formatted accordingly. See jspecify/jspecify#260. Fixes #869 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/869 from PicnicSupermarket:improvement/support-new-jpecify-package-name 7a0dc20b5cddc4d560b2be25441b1ab26c3229c2 PiperOrigin-RevId: 495701635 --- .../com/google/googlejavaformat/java/JavaInputAstVisitor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index 75f36873b..9b6283ece 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -285,6 +285,7 @@ private static ImmutableSetMultimap typeAnnotations() { ImmutableSetMultimap.Builder result = ImmutableSetMultimap.builder(); for (String annotation : ImmutableList.of( + "org.jspecify.annotations.Nullable", "org.jspecify.nullness.Nullable", "org.checkerframework.checker.nullness.qual.Nullable")) { String simpleName = annotation.substring(annotation.lastIndexOf('.') + 1); From d24deb9d982eaae896a57313f7486aaf130bf5aa Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 6 Jan 2023 08:20:43 -0800 Subject: [PATCH 23/31] Recognize a couple `@NonNull` annotations as type-use annotations. PiperOrigin-RevId: 500180559 --- .../com/google/googlejavaformat/java/JavaInputAstVisitor.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index 9b6283ece..394d396d9 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -285,8 +285,10 @@ private static ImmutableSetMultimap typeAnnotations() { ImmutableSetMultimap.Builder result = ImmutableSetMultimap.builder(); for (String annotation : ImmutableList.of( + "org.jspecify.annotations.NonNull", "org.jspecify.annotations.Nullable", "org.jspecify.nullness.Nullable", + "org.checkerframework.checker.nullness.qual.NonNull", "org.checkerframework.checker.nullness.qual.Nullable")) { String simpleName = annotation.substring(annotation.lastIndexOf('.') + 1); result.put(simpleName, annotation); From 90f9e5aeff9dde129cd79db386470b54bdec99e3 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 13 Jan 2023 21:53:10 -0500 Subject: [PATCH 24/31] Delete dependabot.yml --- .github/dependabot.yml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index daec31893..000000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: 2 -updates: - - package-ecosystem: "maven" - directory: "/" - schedule: - interval: "daily" From 4a22aab7b19a41d6267ea70c76f137a6fd49bc76 Mon Sep 17 00:00:00 2001 From: Zach Date: Mon, 16 Jan 2023 09:48:24 -0800 Subject: [PATCH 25/31] Return a non-zero exit code upon error This brings us in line with the Unix standard of returning non-zero for a failed command: https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html Also, this will allow any callers (like google-java-format-diff.py) to respond appropriately. [A change](http://r.android.com/2257232) needed to be reverted because `google-java-format-diff.py` was failing silently, so the author wasn't aware that their change caused a pre-upload hook to stop working. Fixes #848 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/848 from abstractlyZach:return_nonzero 06b2d3671d77c406be9b6d982321b17b920826df PiperOrigin-RevId: 502399096 --- core/src/main/java/com/google/googlejavaformat/java/Main.java | 3 ++- .../java/GoogleJavaFormatToolProviderTest.java | 2 +- .../test/java/com/google/googlejavaformat/java/MainTest.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java index 953ca5860..11c019234 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Main.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java @@ -79,7 +79,8 @@ static int main(PrintWriter out, PrintWriter err, String... args) { return formatter.format(args); } catch (UsageException e) { err.print(e.getMessage()); - return 0; + // We return exit code 2 to differentiate usage issues from code formatting issues. + return 2; } finally { err.flush(); out.flush(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java index 15e452297..d060fef1f 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java @@ -46,7 +46,7 @@ public void testUsageOutputAfterLoadingViaToolName() { int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help"); - assertThat(result).isEqualTo(0); + assertThat(result).isEqualTo(2); String usage = err.toString(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java index ac3eb3966..d4a410904 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java @@ -132,7 +132,7 @@ public void testMain() throws Exception { process.waitFor(); String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8); assertThat(err).contains("Usage: google-java-format"); - assertThat(process.exitValue()).isEqualTo(0); + assertThat(process.exitValue()).isEqualTo(2); } // end to end javadoc formatting test From 91223aa642bd64f973e5b147a7e7d93a2b454fe5 Mon Sep 17 00:00:00 2001 From: Kyle J Stiemann Date: Fri, 3 Feb 2023 16:58:42 -0800 Subject: [PATCH 26/31] Fix #846 Formatter leaks threads and memory I've signed the CLA. Fixes #847 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/847 from stiemannkj1:fix-846-mem-thread-leak 0ca1e9bf9c8cf877509ac39cc50707d0ade7d71c PiperOrigin-RevId: 507026969 --- .../main/java/com/google/googlejavaformat/java/Main.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java index 11c019234..628c8bb24 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Main.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java @@ -18,6 +18,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.io.ByteStreams; +import com.google.common.util.concurrent.MoreExecutors; import com.google.googlejavaformat.FormatterDiagnostic; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; import java.io.IOError; @@ -28,6 +29,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.Duration; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; @@ -187,6 +189,10 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti outWriter.write(formatted); } } + if (!MoreExecutors.shutdownAndAwaitTermination(executorService, Duration.ofSeconds(5))) { + errWriter.println("Failed to shut down ExecutorService"); + allOk = false; + } return allOk ? 0 : 1; } From 0a6a85e37f634fa870c920997d9270dca319cc03 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 14 Feb 2023 12:33:08 -0800 Subject: [PATCH 27/31] Refactor formatter's main to use `ExecutorCompletionService` PiperOrigin-RevId: 509606924 --- .../java/FormatFileCallable.java | 57 +++++++++++---- .../google/googlejavaformat/java/Main.java | 69 ++++++++++--------- 2 files changed, 83 insertions(+), 43 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java b/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java index 9d8ae41ca..3d68a23f3 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java +++ b/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java @@ -14,40 +14,73 @@ package com.google.googlejavaformat.java; +import com.google.auto.value.AutoValue; import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; +import java.nio.file.Path; import java.util.concurrent.Callable; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Encapsulates information about a file to be formatted, including which parts of the file to * format. */ -class FormatFileCallable implements Callable { +class FormatFileCallable implements Callable { + + @AutoValue + abstract static class Result { + abstract @Nullable Path path(); + + abstract String input(); + + abstract @Nullable String output(); + + boolean changed() { + return !input().equals(output()); + } + + abstract @Nullable FormatterException exception(); + + static Result create( + @Nullable Path path, + String input, + @Nullable String output, + @Nullable FormatterException exception) { + return new AutoValue_FormatFileCallable_Result(path, input, output, exception); + } + } + + private final Path path; private final String input; private final CommandLineOptions parameters; private final JavaFormatterOptions options; public FormatFileCallable( - CommandLineOptions parameters, String input, JavaFormatterOptions options) { + CommandLineOptions parameters, Path path, String input, JavaFormatterOptions options) { + this.path = path; this.input = input; this.parameters = parameters; this.options = options; } @Override - public String call() throws FormatterException { - if (parameters.fixImportsOnly()) { - return fixImports(input); - } + public Result call() { + try { + if (parameters.fixImportsOnly()) { + return Result.create(path, input, fixImports(input), /* exception= */ null); + } - Formatter formatter = new Formatter(options); - String formatted = formatter.formatSource(input, characterRanges(input).asRanges()); - formatted = fixImports(formatted); - if (parameters.reflowLongStrings()) { - formatted = StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, formatted, formatter); + Formatter formatter = new Formatter(options); + String formatted = formatter.formatSource(input, characterRanges(input).asRanges()); + formatted = fixImports(formatted); + if (parameters.reflowLongStrings()) { + formatted = StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, formatted, formatter); + } + return Result.create(path, input, formatted, /* exception= */ null); + } catch (FormatterException e) { + return Result.create(path, input, /* output= */ null, e); } - return formatted; } private String fixImports(String input) throws FormatterException { diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java index 628c8bb24..9c60fcb12 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Main.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java @@ -16,6 +16,7 @@ import static java.lang.Math.min; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Comparator.comparing; import com.google.common.io.ByteStreams; import com.google.common.util.concurrent.MoreExecutors; @@ -30,13 +31,14 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.time.Duration; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.Collections; +import java.util.List; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; /** The main class for the Java formatter CLI. */ public final class Main { @@ -123,50 +125,54 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti int numThreads = min(MAX_THREADS, parameters.files().size()); ExecutorService executorService = Executors.newFixedThreadPool(numThreads); - Map inputs = new LinkedHashMap<>(); - Map> results = new LinkedHashMap<>(); + ExecutorCompletionService cs = + new ExecutorCompletionService<>(executorService); boolean allOk = true; + int files = 0; for (String fileName : parameters.files()) { if (!fileName.endsWith(".java")) { errWriter.println("Skipping non-Java file: " + fileName); continue; } Path path = Paths.get(fileName); - String input; try { - input = new String(Files.readAllBytes(path), UTF_8); - inputs.put(path, input); - results.put( - path, executorService.submit(new FormatFileCallable(parameters, input, options))); + cs.submit(new FormatFileCallable(parameters, path, Files.readString(path), options)); + files++; } catch (IOException e) { errWriter.println(fileName + ": could not read file: " + e.getMessage()); allOk = false; } } - for (Map.Entry> result : results.entrySet()) { - Path path = result.getKey(); - String formatted; + List results = new ArrayList<>(); + while (files > 0) { try { - formatted = result.getValue().get(); + files--; + results.add(cs.take().get()); } catch (InterruptedException e) { errWriter.println(e.getMessage()); allOk = false; continue; } catch (ExecutionException e) { - if (e.getCause() instanceof FormatterException) { - for (FormatterDiagnostic diagnostic : ((FormatterException) e.getCause()).diagnostics()) { - errWriter.println(path + ":" + diagnostic); - } - } else { - errWriter.println(path + ": error: " + e.getCause().getMessage()); - e.getCause().printStackTrace(errWriter); + errWriter.println("error: " + e.getCause().getMessage()); + e.getCause().printStackTrace(errWriter); + allOk = false; + continue; + } + } + Collections.sort(results, comparing(FormatFileCallable.Result::path)); + for (FormatFileCallable.Result result : results) { + Path path = result.path(); + if (result.exception() != null) { + for (FormatterDiagnostic diagnostic : result.exception().diagnostics()) { + errWriter.println(path + ":" + diagnostic); } allOk = false; continue; } - boolean changed = !formatted.equals(inputs.get(path)); + String formatted = result.output(); + boolean changed = result.changed(); if (changed && parameters.setExitIfChanged()) { allOk = false; } @@ -205,9 +211,16 @@ private int formatStdin(CommandLineOptions parameters, JavaFormatterOptions opti } String stdinFilename = parameters.assumeFilename().orElse(STDIN_FILENAME); boolean ok = true; - try { - String output = new FormatFileCallable(parameters, input, options).call(); - boolean changed = !input.equals(output); + FormatFileCallable.Result result = + new FormatFileCallable(parameters, null, input, options).call(); + if (result.exception() != null) { + for (FormatterDiagnostic diagnostic : result.exception().diagnostics()) { + errWriter.println(stdinFilename + ":" + diagnostic); + } + ok = false; + } else { + String output = result.output(); + boolean changed = result.changed(); if (changed && parameters.setExitIfChanged()) { ok = false; } @@ -218,12 +231,6 @@ private int formatStdin(CommandLineOptions parameters, JavaFormatterOptions opti } else { outWriter.write(output); } - } catch (FormatterException e) { - for (FormatterDiagnostic diagnostic : e.diagnostics()) { - errWriter.println(stdinFilename + ":" + diagnostic); - } - ok = false; - // TODO(cpovirk): Catch other types of exception (as we do in the formatFiles case). } return ok ? 0 : 1; } From 8c85ac1e3a794cc42362b92fd4fd0de21e23790c Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 14 Feb 2023 15:00:23 -0800 Subject: [PATCH 28/31] Follow-up to https://github.com/google/google-java-format/commit/0a6a85e37f634fa870c920997d9270dca319cc03 See investigation in b/269345183 PiperOrigin-RevId: 509647966 --- core/src/main/java/com/google/googlejavaformat/java/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java index 9c60fcb12..62423f226 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Main.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java @@ -137,7 +137,8 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti } Path path = Paths.get(fileName); try { - cs.submit(new FormatFileCallable(parameters, path, Files.readString(path), options)); + String input = new String(Files.readAllBytes(path), UTF_8); + cs.submit(new FormatFileCallable(parameters, path, input, options)); files++; } catch (IOException e) { errWriter.println(fileName + ": could not read file: " + e.getMessage()); From 973cbff100aa1277ea97f26ffdbc2afd9cb0cb07 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 24 Feb 2023 13:44:10 -0800 Subject: [PATCH 29/31] Allow modifier reordering to be disabled This is currently the only non-whitespace change made as part of the core formatting logic. This is a requirement for updating the IntelliJ plugin to use the new supported plugin API. PiperOrigin-RevId: 512153706 --- .../googlejavaformat/java/Formatter.java | 4 +- .../java/JavaFormatterOptions.java | 50 +++++++------------ .../googlejavaformat/java/MainTest.java | 23 +++++++++ .../GoogleJavaFormatCodeStyleManager.java | 3 +- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index 841e88a8a..9ff702d5a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -262,7 +262,9 @@ public ImmutableList getFormatReplacements( // TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace, // and doesn't change the replacements that are output. This is not true in general for // 'de-linting' changes (e.g. import ordering). - javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges); + if (options.reorderModifiers()) { + javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges); + } String lineSeparator = Newlines.guessLineSeparator(input); JavaOutput javaOutput = diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java index fbb6fe7e4..67c13d0b3 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java @@ -14,6 +14,7 @@ package com.google.googlejavaformat.java; +import com.google.auto.value.AutoValue; import com.google.errorprone.annotations.Immutable; /** @@ -27,7 +28,8 @@ * preferences, and in fact it would work directly against our primary goals. */ @Immutable -public class JavaFormatterOptions { +@AutoValue +public abstract class JavaFormatterOptions { public enum Style { /** The default Google Java Style configuration. */ @@ -47,27 +49,17 @@ int indentationMultiplier() { } } - private final Style style; - private final boolean formatJavadoc; - - private JavaFormatterOptions(Style style, boolean formatJavadoc) { - this.style = style; - this.formatJavadoc = formatJavadoc; - } - /** Returns the multiplier for the unit of indent. */ public int indentationMultiplier() { - return style.indentationMultiplier(); + return style().indentationMultiplier(); } - public boolean formatJavadoc() { - return formatJavadoc; - } + public abstract boolean formatJavadoc(); + + public abstract boolean reorderModifiers(); /** Returns the code style. */ - public Style style() { - return style; - } + public abstract Style style(); /** Returns the default formatting options. */ public static JavaFormatterOptions defaultOptions() { @@ -76,28 +68,22 @@ public static JavaFormatterOptions defaultOptions() { /** Returns a builder for {@link JavaFormatterOptions}. */ public static Builder builder() { - return new Builder(); + return new AutoValue_JavaFormatterOptions.Builder() + .style(Style.GOOGLE) + .formatJavadoc(true) + .reorderModifiers(true); } /** A builder for {@link JavaFormatterOptions}. */ - public static class Builder { - private Style style = Style.GOOGLE; - private boolean formatJavadoc = true; + @AutoValue.Builder + public abstract static class Builder { - private Builder() {} + public abstract Builder style(Style style); - public Builder style(Style style) { - this.style = style; - return this; - } + public abstract Builder formatJavadoc(boolean formatJavadoc); - public Builder formatJavadoc(boolean formatJavadoc) { - this.formatJavadoc = formatJavadoc; - return this; - } + public abstract Builder reorderModifiers(boolean reorderModifiers); - public JavaFormatterOptions build() { - return new JavaFormatterOptions(style, formatJavadoc); - } + public abstract JavaFormatterOptions build(); } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java index d4a410904..ad91bfa26 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java @@ -613,4 +613,27 @@ public void noFormatJavadoc() throws Exception { assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0); assertThat(out.toString()).isEqualTo(joiner.join(input)); } + + @Test + public void reorderModifiersOptionTest() throws Exception { + String[] input = { + "class Test {", // + " static public void main(String... args) {}", + "}", + "", + }; + String[] fixed = { + "class Test {", // + " public static void main(String... args) {}", + "}", + "", + }; + String source = joiner.join(input); + assertThat(new Formatter(JavaFormatterOptions.builder().build()).formatSource(source)) + .isEqualTo(joiner.join(fixed)); + assertThat( + new Formatter(JavaFormatterOptions.builder().reorderModifiers(false).build()) + .formatSource(source)) + .isEqualTo(source); + } } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java index 3d56743ee..c9aa64a67 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java @@ -145,7 +145,8 @@ private void formatInternal(PsiFile file, Collection ranges */ private void format(Document document, Collection ranges) { Style style = GoogleJavaFormatSettings.getInstance(getProject()).getStyle(); - Formatter formatter = new Formatter(JavaFormatterOptions.builder().style(style).build()); + Formatter formatter = + new Formatter(JavaFormatterOptions.builder().style(style).reorderModifiers(false).build()); performReplacements( document, FormatterUtil.getReplacements(formatter, document.getText(), ranges)); } From eb7edafefc0cd2a2b2aaa35dff9ccdbf92d76eb4 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 24 Feb 2023 13:51:57 -0800 Subject: [PATCH 30/31] Update release.yml --- .github/workflows/release.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a3f066df0..6e72789c7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,16 +12,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: write - steps: - - name: Setup Signing Key - run: | - gpg-agent --daemon --default-cache-ttl 7200 - echo -e "${{ secrets.GPG_SIGNING_KEY }}" | gpg --batch --import --no-tty - echo "hello world" > temp.txt - gpg --detach-sig --yes -v --output=/dev/null --pinentry-mode loopback --passphrase "${{ secrets.GPG_PASSPHRASE }}" temp.txt - rm temp.txt - gpg --list-secret-keys --keyid-format LONG - + steps: - name: Checkout uses: actions/checkout@v2.4.0 @@ -34,6 +25,8 @@ jobs: server-id: sonatype-nexus-staging server-username: CI_DEPLOY_USERNAME server-password: CI_DEPLOY_PASSWORD + gpg-private-key: ${{ secrets.GPG_SIGNING_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE - name: Bump Version Number run: | @@ -51,6 +44,7 @@ jobs: env: CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }} CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }} + GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} run: mvn --no-transfer-progress -pl '!eclipse_plugin' -P sonatype-oss-release clean deploy -Dgpg.passphrase="${{ secrets.GPG_PASSPHRASE }}" From 28c504c282ddf36ee26b9c5ff8c0ff50ce428dad Mon Sep 17 00:00:00 2001 From: cushon Date: Fri, 24 Feb 2023 21:54:02 +0000 Subject: [PATCH 31/31] Release google-java-format 1.16.0 --- core/pom.xml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 10565c66a..372656aef 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -22,7 +22,7 @@ com.google.googlejavaformat google-java-format-parent - HEAD-SNAPSHOT + 1.16.0 google-java-format diff --git a/pom.xml b/pom.xml index 150173ee6..ce7dfae98 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ com.google.googlejavaformat google-java-format-parent pom - HEAD-SNAPSHOT + 1.16.0 core