spotify / fmt-maven-plugin

Opinionated Maven Plugin that formats your Java code.
MIT License
335 stars 65 forks source link

Breaks with JDK 13 Switch Arrow Syntax #101

Closed atb-brown closed 2 years ago

atb-brown commented 3 years ago

Describe the bug I'm getting a formatting exception when I use the -> style switch statements that are now possible through JDK 13.

[ERROR] Failed to format file 'C:\Users\me\files\LoggingHelper.java'.
com.google.googlejavaformat.java.FormatterException: 18:39: error: expected token: 'case'; generated ; instead
    at com.google.googlejavaformat.java.Formatter.getFormatReplacements (Formatter.java:293)
    at com.google.googlejavaformat.java.Formatter.formatSource (Formatter.java:267)
    at com.google.googlejavaformat.java.Formatter.formatSource (Formatter.java:233)
    at com.coveo.AbstractFMT.formatSourceFile (AbstractFMT.java:216)
    at com.coveo.AbstractFMT.lambda$formatSourceFilesInDirectory$1 (AbstractFMT.java:157)
    at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:195)
    at java.util.stream.ReferencePipeline$2$1.accept (ReferencePipeline.java:177)
    at java.util.stream.ReferencePipeline$2$1.accept (ReferencePipeline.java:177)
    at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:195)
    at java.util.stream.ReferencePipeline$2$1.accept (ReferencePipeline.java:177)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining (ArrayList.java:1625)
    at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:484)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:474)
    at java.util.stream.ReduceOps$ReduceTask.doLeaf (ReduceOps.java:952)
    at java.util.stream.ReduceOps$ReduceTask.doLeaf (ReduceOps.java:926)
    at java.util.stream.AbstractTask.compute (AbstractTask.java:327)
    at java.util.concurrent.CountedCompleter.exec (CountedCompleter.java:746)
    at java.util.concurrent.ForkJoinTask.doExec (ForkJoinTask.java:290)
    at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec (ForkJoinPool.java:1016)
    at java.util.concurrent.ForkJoinPool.scan (ForkJoinPool.java:1665)
    at java.util.concurrent.ForkJoinPool.runWorker (ForkJoinPool.java:1598)
    at java.util.concurrent.ForkJoinWorkerThread.run (ForkJoinWorkerThread.java:183)

To Reproduce

  // An example method where I encountered this issue; this switch is on an enum.
  public void log(@Nonnull final String message) {
      switch (log.getLevel()) {
          case TRACE -> logger.trace(message);
          case DEBUG -> logger.debug(message);
          case INFO -> logger.info(message);
          case WARN -> logger.warn(message);
          case ERROR -> logger.error(message);
      }
  }

Using Maven 3.6.3 and Google's Java format: mvn fmt:format (Also breaks with mvn fmt:check)

Expected behavior I would figure this would

  1. Not error out with an exception.
  2. Though it looks fine as is, I would also expect some reasonable formatting.

Additional context While the new switch syntax comes from JDK13, I'm using JDK14. I also reverted the above switch statement code to the traditional switch syntax and everything ran fine.

atb-brown commented 3 years ago

The good news is that the new syntax is completely opt-in, so I can just use the traditional syntax for now.

klaraward commented 2 years ago

Assuming that this works with the latest versions of the plugin which uses a newer version of google-java-format?

klaraward commented 2 years ago

This works for me with the latest release if you run the formatting with a newer JDK version which supports the syntax.