Skip to content

NullPointerException in ASTConverter when a record pattern contains an unnamed pattern _ (Java 22+) #486

Backlog

Description

@Pauliman

Sonar Environment

  • spring-javaformat-maven-plugin 0.0.48 (latest release)
  • Apache Maven 3.10.1
  • JDK 25.0.1 (Eclipse Temurin), maven.compiler.release = 25
  • Windows 11

Description

Both validate and apply fail with a NullPointerException when a source file uses an unnamed pattern (_, JEP 456, final since Java 22) as a component of a record pattern. The file compiles fine with javac. Because validate runs as part of our build, any such file breaks the whole build.

Minimal reproduction

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId>
  <artifactId>javaformat-repro</artifactId>
  <version>1</version>
  <properties>
    <maven.compiler.release>25</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>io.spring.javaformat</groupId>
        <artifactId>spring-javaformat-maven-plugin</artifactId>
        <version>0.0.48</version>
      </plugin>
    </plugins>
  </build>
</project>

src/main/java/demo/Shape.java:

package demo;

public sealed interface Shape {

    record Point(int x, int y) implements Shape {
    }

    static int x(Shape shape) {
        return switch (shape) {
            case Point(var x, _) -> x;
        };
    }

}

Run:

mvn io.spring.javaformat:spring-javaformat-maven-plugin:0.0.48:validate

(apply fails the same way, reported as "Unable to format file".)

Expected: The file is validated or formatted like any other Java 22+ source.

Actual:

Caused by: io.spring.javaformat.formatter.FileFormatterException: java.lang.NullPointerException:
Cannot invoke "io.spring.javaformat.eclipse.jdt.jdk17.internal.compiler.ast.TypeReference.extraDimensions()"
because "typeReference" is null
    at io.spring.javaformat.formatter.FileFormatterException.wrap (FileFormatterException.java:47)
    at io.spring.javaformat.formatter.FileFormatter.formatFile (FileFormatter.java:123)
    at io.spring.javaformat.formatter.FileFormatter.lambda$formatFiles$1 (FileFormatter.java:96)
    at io.spring.format.maven.ValidateMojo.execute (ValidateMojo.java:56)
    at io.spring.format.maven.FormatMojo.execute (FormatMojo.java:137)
Caused by: java.lang.NullPointerException: Cannot invoke "...TypeReference.extraDimensions()" because "typeReference" is null
    at io.spring.javaformat.eclipse.jdt.jdk17.core.dom.ASTConverter.convertToSingleVariableDeclaration (ASTConverter.java:3970)
    at io.spring.javaformat.eclipse.jdt.jdk17.core.dom.ASTConverter.convert (ASTConverter.java:3537)
    at io.spring.javaformat.eclipse.jdt.jdk17.core.dom.ASTConverter.convert (ASTConverter.java:2865)
    ...
    at io.spring.javaformat.eclipse.jdt.jdk17.core.dom.ASTConverter.buildBodyDeclarations (ASTConverter.java:208)

What works

  • case Point(var x, var _) -> x; validates and formats without problems.
  • Other unnamed variables are fine in the same code base: lambda parameters (_ -> …) and catch parameters (catch (NumberFormatException _)).

So the failure seems limited to a bare _ used as a component pattern of a record pattern, where there is no type reference to convert.

Possible cause (unverified): The bundled, shaded Eclipse JDT (eclipse.jdt.jdk17) may predate full support for unnamed patterns. Upgrading the embedded JDT may resolve it.

Impact / workaround: We use var _ instead. This conflicts with Sonar rule java:S7475 ("Remove unused type from unnamed pattern"), which recommends the bare _, so we currently suppress that rule where it applies.

I searched existing issues for "unnamed" and "extraDimensions" and found no matching report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions