palantir / palantir/palantir-java-format

Java 21 / JEP 441: Pattern Matching for switch result in com.palantir.javaformat.java.FormatterException

Open
#952 22 comments 33 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
907
Forks
101
Avg merge
1d 3h
Merged PRs (30d)
19

Description

Java 21 / JEP 441 : Pattern Matching for switch code result in com.palantir.javaformat.java.FormatterException

What happened?

This method compiles using JDK 21:

class Example {
    static String test(CharSequence charSequence) {
        return switch (charSequence) {
            case String s when s.isEmpty() -> "empty string";
            case String s -> "non-empty string";
            default -> "not a String";
        };
    }
}

However, when using the spotless-maven-plugin together with <palantirJavaFormat> results in an exception:

mvn spotless:apply
[...]
Unable to format file [path to Example.java]: com.palantir.javaformat.java.FormatterException: error: expected token: 'when'; generated - instead -> [Help 1]

Maven config:

                <plugin>
                    <groupId>com.diffplug.spotless</groupId>
                    <artifactId>spotless-maven-plugin</artifactId>
                    <version>2.40.0</version>
                    <configuration>
                        <java>
                            <palantirJavaFormat>
                                <version>2.38.0</version>
                            </palantirJavaFormat>
                        </java>
                    </configuration>
                </plugin>

Maven:

mvn -v
Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)
Java version: 21, vendor: Amazon.com Inc.
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "13.6.1", arch: "x86_64", family: "mac"

What did you want to happen?

The maven-spotless-plugin and palanter-java-format should accept or reformat code that is supported by JDK 21

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with JDK 21 and mvn spotless:apply using the shown spotless-maven-plugin and palantirJavaFormat configuration. Compare the formatter's handling of the JEP 441 when switch syntax with the reported expected token: 'when' error; done means the example is accepted or reformatted without FormatterException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.