Title: A cast breaks inside its type when the call around it stays on one line · Issue #60 · openjavaformat/open-java-format · GitHub
Open Graph Title: A cast breaks inside its type when the call around it stays on one line · Issue #60 · openjavaformat/open-java-format
X Title: A cast breaks inside its type when the call around it stays on one line · Issue #60 · openjavaformat/open-java-format
Description: What Found while formatting the JDK 21 sources with main (86cdc74e). sun.rmi.transport.tcp.TCPTransport comes out as: @SuppressWarnings("removal") static final Log tcpLog = Log.getLog( "sun.rmi.transport.tcp", "tcp", LogStream.parseLevel...
Open Graph Description: What Found while formatting the JDK 21 sources with main (86cdc74e). sun.rmi.transport.tcp.TCPTransport comes out as: @SuppressWarnings("removal") static final Log tcpLog = Log.getLog( "sun.rmi.tra...
X Description: What Found while formatting the JDK 21 sources with main (86cdc74e). sun.rmi.transport.tcp.TCPTransport comes out as: @SuppressWarnings("removal") static final Log tcpLog = Log.getLog( &q...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/60
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A cast breaks inside its type when the call around it stays on one line","articleBody":"### What\n\nFound while formatting the JDK 21 sources with main (`86cdc74e`). `sun.rmi.transport.tcp.TCPTransport` comes out as:\n\n```java\n @SuppressWarnings(\"removal\")\n static final Log tcpLog = Log.getLog(\n \"sun.rmi.transport.tcp\", \"tcp\", LogStream.parseLevel(AccessController.doPrivileged((PrivilegedAction\u003c\n String>)\n () -> System.getProperty(\"sun.rmi.transport.tcp.logLevel\"))));\n```\n\nThe cast `(PrivilegedAction\u003cString>)` is cut between `\u003c` and `String`, and the lambda it applies to lands on a third line. Formatting the result again changes nothing, so this is what the file settles on.\n\nWanted: the arguments of `getLog` one per line, and the cast whole, on the line with its lambda:\n\n```java\n @SuppressWarnings(\"removal\")\n static final Log tcpLog = Log.getLog(\n \"sun.rmi.transport.tcp\",\n \"tcp\",\n LogStream.parseLevel(AccessController.doPrivileged(\n (PrivilegedAction\u003cString>) () -> System.getProperty(\"sun.rmi.transport.tcp.logLevel\"))));\n```\n\nOne call shallower, the same cast is right today:\n\n```java\n static final Level level = LogStream.parseLevel(AccessController.doPrivileged(\n (PrivilegedAction\u003cString>) () -> System.getProperty(\"sun.rmi.transport.tcp.logLevel.and.some.more\")));\n```\n\nSo are a cast lambda as the only argument, a cast of a method reference instead of a lambda, and the same statement in a local variable. The code is palantir-java-format 2.98.0's, so it should format this the same way; google-java-format does not have this layout logic.\n\n### Why\n\nThe arguments of `getLog` are laid out with `breakOnlyIfInnerLevelsThenFitOnOneLine`: keep them on one line when a \"sensible prefix\" fits, where the prefix is everything up to the first break (`CountWidthUntilBreakVisitor`). The first break inside a cast is not the one before its expression but the one `visitParameterizedType` emits right after `\u003c` in its type. The prefix up to there is 113 columns, so it fits, and the type then has to take that break because `String>)` does not fit behind it.\n\n`visitTypeCast` opens its level with `preferBreakingLastInnerLevel` and `LastLevelBreakability.ACCEPT_INLINE_CHAIN`, whose javadoc asks for a level whose first non-level doc is a break. A cast starts with `(`.\n\nConfirmed with a spike that took the break after `\u003c` out of `visitParameterizedType`: with no break inside the type, the prefix runs to the cast's own break, is 121 columns, does not fit, the arguments break one per line, and the statement comes out as in \"wanted\". That spike changes the layout of every long parameterized type (6 goldens), so it is not the fix.\n\n### How\n\nTwo narrow options, neither tried:\n\n- In `visitTypeCast`, lay the type out without a break after `\u003c`, the way `addTypeArguments` does for `ImmutableList.\u003cFoo>of()`. A cast's type is short by nature, and the first break in a cast is then the one before the expression.\n- Or give the cast level a `LastLevelBreakability` that ends the inline chain when the level does not start with a break (`CHECK_INNER`), and check what that does to the shallower shape above, which is right today.\n\n### Compatibility\n\nIt changes the output of that one shape: one statement in the 15,747 files of the JDK 21 sources. The README allows a bug fix to change the output in 2.x, and the Migrate page lists such fixes.\n\n### Done when\n\n- a golden with the `getLog` statement fails today and passes with the fix, and the shallower shape stays as it is;\n- a JDK 21 corpus run shows only that statement moving, and the release notes and the Migrate page say so.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-09-24T11:03:09Z","url":"https://github.com/openjavaformat/open-java-format/issues/60"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cd7a1518-6083-1c02-ad41-83bca9bf6ada |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8250:3D2B40:594ADC:76F884:6AFBC734 |
| html-safe-nonce | cdd22747807329b206eb5cb4c5849038fd390616f6ab83d797b207ed0118d534 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjUwOjNEMkI0MDo1OTRBREM6NzZGODg0OjZBRkJDNzM0IiwidmlzaXRvcl9pZCI6IjM5OTk2ODMzMjY5MTc3MjM5NTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7ba0aafdd56ef5eba54e861e47d57610a7f4ef5a7d9d65701fc645c1c0c92fa4 |
| hovercard-subject-tag | issue:5567302870 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/openjavaformat/open-java-format/60/issue_layout |
| twitter:image | https://opengraph.githubassets.com/81a382508ad0a8e6a88a428d177ef688f5ff500d72c77baa2c4b704065df2203/openjavaformat/open-java-format/issues/60 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/81a382508ad0a8e6a88a428d177ef688f5ff500d72c77baa2c4b704065df2203/openjavaformat/open-java-format/issues/60 |
| og:image:alt | What Found while formatting the JDK 21 sources with main (86cdc74e). sun.rmi.transport.tcp.TCPTransport comes out as: @SuppressWarnings("removal") static final Log tcpLog = Log.getLog( "sun.rmi.tra... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abashev |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| turbo-cache-control | no-preview |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width