palantir / palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Apache License 2.0
426 stars 45 forks source link

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

Open matsev opened 10 months ago

matsev commented 10 months ago

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 planter-java-format should accept or reformat code that is supported by JDK 21

koppor commented 10 months ago

Can you manage to compile and try https://github.com/palantir/palantir-java-format/pull/935?

matsev commented 9 months ago

I have checked out PR #935 locally and I manage to build it if I use JDK 17 when running Cradle.

./gradlew -v

------------------------------------------------------------
Gradle 7.6.2
------------------------------------------------------------

Build time:   2023-06-30 15:42:51 UTC
Revision:     dab132169006b16e7ada4ab2456e0c9d6415b52a

Kotlin:       1.7.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17.0.8 (Amazon.com Inc. 17.0.8+7-LTS)
OS:           Mac OS X 13.6.1 x86_64
./gradlew build
[...]
BUILD SUCCESSFUL in 29s
93 actionable tasks: 56 executed, 25 from cache, 12 up-to-date

However, that PR fails when tried to build using JDK 21 as it is not supported for running Gradle according to its compatibility matrix:

./gradlew -v

------------------------------------------------------------
Gradle 7.6.2
------------------------------------------------------------

Build time:   2023-06-30 15:42:51 UTC
Revision:     dab132169006b16e7ada4ab2456e0c9d6415b52a

Kotlin:       1.7.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          21 (Amazon.com Inc. 21+35-LTS)
OS:           Mac OS X 13.6.1 x86_64
./gradlew build
[...]
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':gradle-palantir-java-format:compileGroovy'.
> Unrecoverable compilation error: startup failed:
  General error during conversion: Unsupported class file major version 65
mandrean commented 8 months ago

Bump:

Java 21 is now supported from Gradle ~8.4 @matsev

image

matsev commented 8 months ago

@mandrean Thanks for the notification. I made an attempt to upgrade the Gradle wrapper to use Gradle ver 8.5, but there is some more work required to make this work, please see #977 for details

thijmenketel commented 3 months ago

Any progress on this? We're starting to use more JDK21 features and this is preventing us to use when.

piotrooo commented 1 month ago

:+1: urgent and necessary

rohan-changejar commented 3 weeks ago

Any update on this, we have moved to java 21 and using the new switch case features seem impossible without this change

talios commented 1 week ago

Do you happen to have any updates on this issue? Is there anything specific holding it up that we can do to help resolve this?

Java 23 is just around the corner now, and I fear the lack of support for some of these newer constructs will become more and more of a problem for adoption (of both palantir-java-format and modern java itself).