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

Google Java Format fails on complex switch expressions with Java 21 #983

Closed
bmeier-pros opened this issue Oct 30, 2023 · 2 comments
Closed
Labels

Comments

@bmeier-pros
Copy link

Version 1.18.1 formatter throws an exception when trying to handle code like this:

      return switch (obj1) {
          case null -> obj2 == null;
          case T1 myT1 when obj2 instanceof T1 otherT1 -> myT1.equalsStrict(otherT1);
          // exception ^ at the when (69:46 in the original source)
          case T2 myT2 when obj2 instanceof T2 otherT2 -> T2.isDeepEquals(myT2, otherT2);
          default -> obj1.equals(obj2);
      };

The exception is:

Step 'google-java-format' found problem in 'src/test/java/com/example/test/acceptance/ExpectedResult.java':
69:46: error: : or -> expected
com.google.googlejavaformat.java.FormatterException: 69:46: error: : or -> expected
        at com.google.googlejavaformat.java.FormatterException.fromJavacDiagnostics(FormatterException.java:51)
        at com.google.googlejavaformat.java.Formatter.format(Formatter.java:149)
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:273)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:247)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:213)
        at com.diffplug.spotless.glue.java.GoogleJavaFormatFormatterFunc.apply(GoogleJavaFormatFormatterFunc.java:65)
        at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:32)
        at com.diffplug.spotless.FormatterStepImpl$Standard.format(FormatterStepImpl.java:82)
        at com.diffplug.spotless.FormatterStep$Strict.format(FormatterStep.java:103)
        at com.diffplug.spotless.Formatter.compute(Formatter.java:246)
        at com.diffplug.spotless.PaddedCell.calculateDirtyState(PaddedCell.java:203)
        at com.diffplug.spotless.PaddedCell.calculateDirtyState(PaddedCell.java:190)
        at com.diffplug.gradle.spotless.SpotlessTaskImpl.processInputFile(SpotlessTaskImpl.java:105)
        at com.diffplug.gradle.spotless.SpotlessTaskImpl.performAction(SpotlessTaskImpl.java:89)
<gradle stack trace elided>
@bmeier-pros
Copy link
Author

There are similar issues in #973 and #922, but this is with base Java 21 language support through Spotless and Gradle.

@cushon cushon added the Java 21 label Dec 7, 2023
copybara-service bot pushed a commit that referenced this issue Dec 7, 2023
This PR adds support for `switch` statements where a `case` has a guard clause.

See Issue #983

Fixes #988

FUTURE_COPYBARA_INTEGRATE_REVIEW=#988 from TheCK:master 4771486
PiperOrigin-RevId: 588901660
copybara-service bot pushed a commit that referenced this issue Dec 7, 2023
This PR adds support for `switch` statements where a `case` has a guard clause.

See Issue #983

Fixes #988

COPYBARA_INTEGRATE_REVIEW=#988 from TheCK:master 4771486
PiperOrigin-RevId: 588913297
@cushon
Copy link
Collaborator

cushon commented Dec 8, 2023

After the fixes in 430ba3b and b86c508, the formatter handles this code:

class T {
  boolean f(Object obj1, Object obj2) {
    return switch (obj1) {
      case null -> obj2 == null;
      case T1 myT1 when obj2 instanceof T1 otherT1 -> myT1.equalsStrict(otherT1);
        // exception ^ at the when (69:46 in the original source)
      case T2 myT2 when obj2 instanceof T2 otherT2 -> T2.isDeepEquals(myT2, otherT2);
      default -> obj1.equals(obj2);
    };
  }
}

@cushon cushon closed this as completed Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants