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

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

Open
matsev opened this issue Nov 16, 2023 · 4 comments

Comments

@matsev
Copy link

matsev commented Nov 16, 2023

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
Copy link
Contributor

koppor commented Nov 20, 2023

Can you manage to compile and try #935?

@matsev
Copy link
Author

matsev commented Nov 21, 2023

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

@matsev matsev changed the title JEP 441: Pattern Matching for switch result in com.palantir.javaformat.java.FormatterException Java 21 / JEP 441: Pattern Matching for switch result in com.palantir.javaformat.java.FormatterException Dec 4, 2023
@mandrean
Copy link

mandrean commented Jan 4, 2024

Bump:

Java 21 is now supported from Gradle ~8.4 @matsev

image

@matsev
Copy link
Author

matsev commented Jan 8, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants