palantir / palantir-java-format

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

NoSuchMethodError when formatting with JDK 21 #885

Closed gnodet closed 1 year ago

gnodet commented 1 year ago

What happened?

When compiling maven master branch using JDK 21, I have the following exception:

Caused by: java.lang.NoSuchMethodError: 'com.sun.tools.javac.tree.JCTree com.sun.tools.javac.tree.JCTree$JCImport.getQualifiedIdentifier()'
    at com.palantir.javaformat.java.RemoveUnusedImports.getSimpleName(RemoveUnusedImports.java:245)
    at com.palantir.javaformat.java.RemoveUnusedImports.buildReplacements(RemoveUnusedImports.java:225)
    at com.palantir.javaformat.java.RemoveUnusedImports.removeUnusedImports(RemoveUnusedImports.java:209)
    at com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc.apply(PalantirJavaFormatFormatterFunc.java:42)

What did you want to happen?

The formatting should work !

Analysis

The getSimpleName method needs to adapted somehow. The JDK internal class JCImport has changed recently. In JDK 17, the method was defined as:

public JCTree getQualifiedIdentifier() { return qualid; }

and in JDK 21:

public JCFieldAccess getQualifiedIdentifier() { return qualid; }

One possibility to fix that would be to fix the getSimpleName() method to use an argument of type com.sun.source.tree.ImportTree and call the getQualifiedIdentifier() method which returns a com.sun.source.tree.Tree and has not changed.

    private static String getSimpleName(ImportTree importTree) {
        return importTree.getQualifiedIdentifier() instanceof JCIdent
                ? ((JCIdent) importTree.getQualifiedIdentifier()).getName().toString()
                : ((JCFieldAccess) importTree.getQualifiedIdentifier())
                        .getIdentifier()
                        .toString();
    }
basil commented 1 year ago

openjdk version "21-ea" 2023-09-19 OpenJDK Runtime Environment (build 21-ea+32-2482) OpenJDK 64-Bit Server VM (build 21-ea+32-2482, mixed mode, sharing)

Still broken for me on 2.33.0 and 2.34.0:

[ERROR] Step 'palantir-java-format' found problem in 'src/test/java/hudson/plugins/textfinder/test/TestEchoBuilder.java':
'com.sun.tools.javac.tree.JCTree com.sun.tools.javac.tree.JCTree$JCImport.getQualifiedIdentifier()'
java.lang.NoSuchMethodError: 'com.sun.tools.javac.tree.JCTree com.sun.tools.javac.tree.JCTree$JCImport.getQualifiedIdentifier()'
    at com.palantir.javaformat.java.RemoveUnusedImports.isUnused (RemoveUnusedImports.java:258)
    at com.palantir.javaformat.java.RemoveUnusedImports.buildReplacements (RemoveUnusedImports.java:226)
    at com.palantir.javaformat.java.RemoveUnusedImports.removeUnusedImports (RemoveUnusedImports.java:209)
    at com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc.apply (PalantirJavaFormatFormatterFunc.java:42)
    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.spotless.maven.SpotlessCheckMojo.process (SpotlessCheckMojo.java:54)
    at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute (AbstractSpotlessMojo.java:229)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:342)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:330)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:175)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:76)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:163)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:160)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:910)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)