Skip to content

Support Module Import Declarations (JEP 476, JDK 23) #1213

Description

@sebastiaanspeck

Consider the following code:

// this will import e.g. all the classes from package java.util, java.util.stream, java.io, java.lang, java.nio.file
import module java.base;

When trying to use google-java-format 1.24.0, this results in: com.google.googlejavaformat.java.FormatterException: 2:1: error: expected token: 'module'; generated java instead

Activity

  1. cushon commented on Mar 23, 2025

    @cushon
    Collaborator

    Note that this is currently a preview feature, there's a draft JEP to finalize the feature in Java 25: https://openjdk.org/jeps/8344700

  2. sormuras commented on Mar 26, 2025

    @sormuras
    Contributor

    I wonder where GJF will put import MODULE; declarations in relation to the static and non-static block as described in https://google.github.io/styleguide/javaguide.html#s3.3-import-statements

    3.3.3 Ordering and spacing
    Imports are ordered as follows:

    1. All static imports in a single block.
    2. All non-static imports in a single block.

    If there are both static and non-static imports, a single blank line separates the two blocks. There are no other blank lines between import statements.

  3. sormuras commented on Jul 15, 2025

    @sormuras
    Contributor

    Finalized as "JEP 511: Module Import Declarations" in Java 25.

  4. cushon commented on Jul 16, 2025

    @cushon
    Collaborator

    The style guide update for module imports is still TBD, but that should happen in time for the Java 25 GA.

    It's possible it will discourage module imports in favor of single-type imports (similar to how wildcard imports are already discouraged).

    But either way, google-java-format should support the syntax for files that use it.

  5. cushon commented on Oct 13, 2025

    @cushon
    Collaborator

    I pushed a release that adds support for import module.

    The style guide will be updated to disallow import module (similar to how wildcard imports are disallowed): google/styleguide#946

  6. sormuras commented on Oct 13, 2025

    @sormuras
    Contributor

    Great news, Liam.

    The guide will disallow them, but gjf will keep them? I.e. gjf will not expand them to normal type imports?

  7. cushon commented on Oct 13, 2025

    @cushon
    Collaborator

    g-j-f will preserve them.

    It doesn't have enough information to expand them even if we wanted to do that in the formatter. That might be something we'd add to Error Prone eventually, similar to https://errorprone.info/bugpattern/WildcardImport.

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