Skip to content

Format inside the IDE's JVM in IntelliJ IDEA, with a separate JVM or the native binary to choose instead - #259

Merged
abashev merged 7 commits into
21.X.Xfrom
abashev/intellij-plugin-format-in-the-ides-jvm-without-a
Nov 23, 2026
Merged

abashev merged 7 commits into
21.X.Xfrom
abashev/intellij-plugin-format-in-the-ides-jvm-without-a

Conversation

@abashev

@abashev abashev commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #223. Independent of the other open pull requests; based on 21.X.X.

The IntelliJ plugin started a process for every Reformat and every Optimize imports: a JVM on the IDE's runtime, 0.3 s to 1 s a file, or the native binary. Optimize imports runs under a read action, and "Optimize imports on the fly" runs it while typing (#253). The plugin now loads the formatter into the IDE's own JVM, where a call takes 124 ms on a file of 4,026 lines and 9 ms to fix its imports once warm (the times of #223), and the settings page offers the two other ways to run it.

The settings page

Use native binary gives way to three radio buttons under Run the formatter, one of them chosen for a project:

  • In the IDE process, the default. Where the formatter cannot reach javac in the IDE's JVM, the button is greyed out, with the reason under it.
  • In a separate JVM, as before, with a JDK list: the IDE's runtime, which is the default and what the plugin ran on, the project SDK, and the JDKs registered in the IDE. A JDK that cannot run the formatter, older than Java 21 for one, is listed in grey and cannot be chosen.
  • Native binary, as before: the embedded version to download, or a binary of one's own.

The options of the ways not chosen stay on the page, greyed, rather than hidden as the native options were. Active formatter still names what runs once the page is applied, from the same FormatterSelection that FormatterProvider starts the formatter with.

Existing projects

A project whose settings switch the native binary on keeps it. Every other project moves from a JVM for each file into the IDE process.

The settings file

formatterMode alone says how a project formats, in .idea/open-java-format.xml, as in-process, jvm or native. With no mode, or one this plugin does not know, a project formats inside the IDE.

  • useNativeImage, which a file from before the modes has, is read once, when the file is loaded: true switches the project to native, and the setting is emptied, so the IDE writes the file without it from then on. The plugin no longer writes it.
  • A setting of one mode has that mode at the start of its name: jvmJdk for the JDK of the separate JVM, and nativeImagePath, which keeps the name it was released with. A mode added later, such as a daemon with a JDK of its own, then has settings no other mode reads.

Inside the IDE

  • javac. The formatter parses with javac's own classes, in packages that jdk.compiler exports to nobody. IntelliJ IDEA starts its JVM with --add-opens=java.base/java.lang=ALL-UNNAMED: it is in the product-info.json of 2024.2.6, the oldest IDE the plugin supports, and of 2026.3. With java.lang open, JavacAccess calls Module.implAddExports, the method --add-exports ends in, for the formatter's class loader alone. Nobody edits VM options, and javac stays unexported to the plugin and to the rest of the IDE. The method is not public API: JavacAccess.unavailable says beforehand whether it can be called, and the page greys the IDE process out when it cannot. A user who did add --add-exports to the VM options, as google-java-format asks, gets the IDE process without the call.
  • Class loading. FormatterClassLoader's parent is the JDK's platform class loader, which finds javac and nothing of the IDE. From the plugin come only the types the two exchange: the classes of open-java-format-spi, and Guava, which their signatures name. InProcessFormatters keeps a formatter loaded per set of jars, for every project that formats with them, and compares the jars' size and time for every file, so that a jar rebuilt in place formats the next file, as in a JVM started for it.
  • Syntax errors. The formatter itself throws on one, where the command line exits and FormatterCommandRunner leaves the file alone without a message. InProcessFormatterService does the same: otherwise "Optimize imports on the fly" would show a balloon on every highlighting pass of a file being typed in.
  • When it fails. A file the formatter fails on with an Error, a stack overflow for one, is formatted in a JVM on the IDE's runtime. A formatter that does not load or link in the IDE switches the IDE process off until the IDE restarts: the page says why, and idea.log gets one error, for the plugin's error reports. The formatter is loaded with the first file, in the background, not when FormatterProvider is asked for it, which can be on the UI thread.

ADR-005 has the alternatives: a formatter daemon, asking users for --add-exports, the plugin's class loader as the formatter's parent, and leaving a JVM for each call as the default.

What falls back

With a warning in idea.log and a line under Active formatter, and no balloon:

  • the IDE process, where javac cannot be reached, to a separate JVM on the chosen JDK;
  • a JDK that cannot run the formatter, to the IDE's runtime.

A missing native binary falls back to a separate JVM with its notification, as before.

What changes for users

  • Reformat and Optimize imports start no process, unless the project runs the native binary.
  • The javac of the IDE's runtime decides which syntax parses inside the IDE: Java 21 in 2024.2, Java 25 in 2026.3. A project with newer syntax chooses a separate JVM on a newer JDK.
  • A formatter from a build is loaded into the IDE's JVM, in a trusted project only, as it was run only there before.
  • One failure of a formatter to load or link inside the IDE switches the IDE process off for every project until the IDE restarts.
  • Apply no longer checks the binary and the JDK while Enable open-java-format is unticked: the page shows neither then.
  • A plugin from before the modes that reads a settings file written since finds no useNativeImage, and formats in a JVM of its own.
  • The plugin's description on the Marketplace describes the three ways.
  • The openjavaformat.dev page for IntelliJ IDEA needs a follow-up in openjavaformat/docs, with new screenshots of the settings page.

Tests

  • JavacAccessTest starts JVMs of its own, since the build gives the JVM of the tests --add-exports for javac to everything:
    • with the IDE's flag alone, the formatter gives the command line's file for A.input, by all three methods of FormatterService, leaves a syntax error alone, and javac is not exported to the plugin's own module;
    • with the exports a user adds by hand, it formats without implAddExports;
    • with neither, it says why it cannot format, and refuses to load the formatter.
  • InProcessFormattersTest
    • the formatter in this JVM gives what a JVM of its own gives, for a file and for a syntax error;
    • a formatter stays loaded while its jars stay the same, and a touched jar gives a new one;
    • the formatter's own classes and Jackson come from its class loader, the SPI and Guava from the plugin's, and a class of the IDE is not found;
    • a formatter that does not load, one that does not link (its jars without functionaljava) and a file that overflows the stack are each formatted by the stand-in for the other JVM; the first two switch the IDE process off, the third does not.
  • FormatterClassLoaderTest: every class in the SPI's jar is among those taken from the plugin, so a class added to the SPI cannot go unshared unnoticed.
  • FormatterJdksTest: the java and the version of the IDE's runtime, the project SDK and a named JDK, and the reason for each JDK that cannot run the formatter.
  • FormatterSelectionTest: every mode with and without what it needs, and that only what would run is looked for.
  • FormatterProviderTest: the service of each mode, and that a change of JDK is not served from the cache.
  • PalantirJavaFormatSettingsTest: a file from before the modes switches to native and loses useNativeImage, with false or without it a project formats inside the IDE, the text of each mode, and the names of the modes' settings.
  • PalantirJavaFormatConfigurableTest: the radio buttons, the greyed IDE process with its reason, the list of JDKs with one that cannot be chosen, and what Apply refuses.

./gradlew check passes on JDK 21, and :open-java-format-idea-plugin:check passes on each of the first two commits on its own. verifyPlugin: compatible with IC-242.26775.15 and IU-263.7307.200, with no new usage of experimental or deprecated API.

To check in a running IDE

Not tried yet. The tests run in the headless test IDE, whose class loader is not the PluginClassLoader a plugin gets in a running IDE:

  • Reformat Code and Optimize Imports in each of the three ways; idea.log has open-java-format: using in-process formatter for the first.
  • The settings page: the three ways, the list of JDKs with one older than Java 21, Apply and reopen.
  • A project that had Use native binary ticked still has it after the update.
  • "Optimize imports on the fly" while typing, in the IDE process.

Not done here

Found on the way, each with an issue of its own:

The IntelliJ plugin starts the formatter's JVM on the IDE's own runtime
and nothing else. FormatterJdk is a JDK that a project can name for it
instead: the IDE's runtime, whichever SDK the project has, or a JDK
registered in the IDE, by name. FormatterJdks finds the java executable
and the Java version each stands for, and says why one cannot run the
formatter: it is older than Java 21, which the formatter is compiled
for, the IDE has no JDK of that name, the project has no SDK, or there
is no java to start in it, as for a JDK in WSL.

Nothing asks for them yet. The settings page lists them and
FormatterSelection resolves the chosen one once the formatter can run
in more ways than a JVM on the IDE's runtime (#223).
The IntelliJ plugin starts a process for every Reformat and every
Optimize imports: 0.3 s to 1 s for a JVM, where the formatter, once
loaded and warm, takes 124 ms on a file of 4,026 lines and 9 ms to fix
its imports (#223). This is what loads it into the IDE's JVM. Nothing
calls it yet.

The formatter parses with javac's own classes, in packages that
jdk.compiler exports to nobody, and the IDE's JVM is already running.
IntelliJ IDEA starts it with --add-opens=java.base/java.lang=ALL-UNNAMED,
in 2024.2.6, the oldest IDE the plugin supports, and in 2026.3. With
java.lang open JavacAccess can call Module.implAddExports, the method
--add-exports ends in: for the formatter's class loader alone, so that
nothing else in the IDE reaches javac through it, and nobody edits VM
options. The method is not public API, so JavacAccess first says
whether it is there to call, and why not if it is not.

FormatterClassLoader keeps the formatter apart from the IDE. Its parent
is the JDK's platform class loader, which finds javac and nothing of
the IDE: the formatter's jars are everything it runs with in a JVM of
its own, and the IDE has other versions of the same libraries. Only the
types the plugin and the formatter exchange come from the plugin, the
classes of open-java-format-spi and Guava, which their signatures name.
InProcessFormatters keeps a formatter loaded for as long as its jars
stay the same, and is asked for every file, so that a jar rebuilt in
place formats the next one, as in a JVM started for it.

InProcessFormatterService leaves a file with a syntax error alone, as
the formatters that run the command line do: the formatter itself
throws, and "Optimize imports on the fly" would show that on every
highlighting pass. What it cannot format in this JVM it hands to the
service it is given for that: a file the formatter fails on with an
Error, a stack overflow for one, and every file once the formatter
turns out not to load or link here. The IDE process is the faster way,
never the only one.

The JVM of the tests has javac exported to everything, as the build
compiles the formatter, so JavacAccessTest starts JVMs of its own: one
with the IDE's flag alone, where the formatter gives the command line's
file and javac stays unexported to the plugin; one with the exports a
user of google-java-format adds by hand; and one with neither, which
says why it cannot format.
The settings page of the IntelliJ plugin had one box, Use native
binary, and without it the plugin started a JVM on the IDE's runtime
for every file. It now has three ways to run the formatter, one of
them chosen for a project:

- In the IDE process: the formatter loaded into the IDE's own JVM,
  with nothing to start for a file. It is greyed out, with the reason
  under it, where the formatter cannot reach javac there.
- In a separate JVM, as before, on a JDK from a list: the IDE's
  runtime, which is the default and what the plugin ran on, the project
  SDK, or a JDK registered in the IDE. One that cannot run the
  formatter, older than Java 21 for one, is listed in grey and cannot
  be chosen.
- Native binary, as before.

The options of the ways not chosen stay on the page, greyed: hidden,
they would move the page about with every choice.

A project whose settings switch the native binary on keeps it. Every
other project moves into the IDE process: a file written before there
were modes says only useNativeImage, and formatterMode is new beside
it. useNativeImage goes on being written with the mode, for a plugin
from before the modes that reads the same .idea/open-java-format.xml.

What cannot run falls back, with a warning in idea.log and a line
under Active formatter, and no balloon: the IDE process to a separate
JVM, a JDK that cannot run the formatter to the IDE's runtime. A
missing native binary falls back to a separate JVM with its
notification, as before. Inside the IDE, a file the formatter fails on
with an Error is formatted in a JVM on the IDE's runtime, and a
formatter that does not load or link there switches the IDE process
off until the IDE restarts, with one error in idea.log for the
plugin's error reports.

Apply no longer checks the binary and the JDK while Enable
open-java-format is unticked: the page shows neither then, so there
was nothing on it to put right.

Closes #223.
Why the formatter runs inside the IDE by default, how it reaches javac
there without anyone editing VM options, why it is loaded apart from
the IDE's classes, and what was rejected: a formatter daemon, asking
users for --add-exports, the plugin's class loader as the formatter's
parent, and leaving a JVM for each call as the default.
…fter it

The settings said how the formatter runs twice: formatterMode, and
beside it useNativeImage, written with every mode for a plugin from
before the modes. And the JDK of the separate JVM was formatterJdk,
which a mode added later, a daemon with a JDK of its own for one,
could not tell from its own.

formatterMode alone says it now, as in-process, jvm or native: words
of the file's own, which stay when a constant is renamed. A file from
before the modes has useNativeImage instead. It is read once, when the
file is loaded: true switches the project to native, and the setting
is emptied, so the IDE writes the file without it from then on. With
neither, or with a mode this plugin does not know, a project formats
inside the IDE.

A setting of one mode has that mode at the start of its name:
jvmJdk, which was formatterJdk and was not released under that name,
and nativeImagePath, which keeps the name it was released with.

A plugin from before the modes that reads a file written since finds
no useNativeImage, and formats in a JVM of its own. ADR-005 says so.
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

Successfully merging this pull request may close these issues.

IntelliJ plugin: format in the IDE's JVM, without a new JVM for each call

1 participant