From c375eab28d82fce5383e9dde601871a9bfae6453 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sun, 22 Nov 2026 22:35:12 +0300 Subject: [PATCH] Leave alone a long literal under a cast, .new or method reference Reflowing a long string literal failed the whole file when the literal sat under something that binds tighter than `+`: a cast, the enclosing expression of a qualified `.new`, or a method reference. Split into "a" + "b", the literal left the cast only its first piece, as in ((Object) "a") + "b", and the `.new` or the `::` only its last. The AST check at the end of wrapOnce caught this and threw, so the formatter printed "Something has gone terribly wrong" and formatted nothing in the file. LongStringsAndTextBlockScanner already left alone the expression of a member select ("...".length()) for this reason. It now skips the three other contexts too, and such a literal stays on its line as the formatter lays it out. A literal in parentheses, as in (Object) ("..."), is still reflowed inside them. --- .../javaformat/java/StringWrapper.java | 23 +++++++++++-- .../javaformat/java/StringWrapperTest.java | 32 ++++++++++++------- ...eralsUnderACastANewOrAMethodReference.case | 29 +++++++++++++++++ 3 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 open-java-format/src/test/resources/test-cases-java21/stringwrapper/longLiteralsUnderACastANewOrAMethodReference.case diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java b/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java index 205881096..17f4b6817 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java @@ -28,10 +28,13 @@ import com.palantir.javaformat.Utils; import com.sun.source.tree.BinaryTree; import com.sun.source.tree.LiteralTree; +import com.sun.source.tree.MemberReferenceTree; import com.sun.source.tree.MemberSelectTree; import com.sun.source.tree.MethodInvocationTree; +import com.sun.source.tree.NewClassTree; import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; +import com.sun.source.tree.TypeCastTree; import com.sun.source.util.TreePath; import com.sun.source.util.TreePathScanner; import com.sun.tools.javac.tree.JCTree; @@ -437,9 +440,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { textBlocks.add(getCurrentPath()); return null; } - Tree parent = getCurrentPath().getParentPath().getLeaf(); - if (parent instanceof MemberSelectTree memberSelectTree - && memberSelectTree.getExpression().equals(literalTree)) { + if (bindsTighterThanPlus(getCurrentPath().getParentPath().getLeaf(), literalTree)) { return null; } int endPosition = getEndPosition(unit, literalTree); @@ -455,6 +456,22 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { } } + /** + * Whether the parent of a literal applies to it more tightly than {@code +} does: as a member select + * ({@code "…".length()}), a cast, a qualified {@code .new} or a method reference. Reflowed into + * {@code "a" + "b"}, the literal would leave that parent only one of its pieces, as in + * {@code (Object) "a" + "b"}, so it stays whole. + */ + private static boolean bindsTighterThanPlus(Tree parent, LiteralTree literal) { + return (parent instanceof MemberSelectTree select + && select.getExpression().equals(literal)) + || (parent instanceof TypeCastTree cast + && cast.getExpression().equals(literal)) + || (parent instanceof NewClassTree newClass && literal.equals(newClass.getEnclosingExpression())) + || (parent instanceof MemberReferenceTree reference + && reference.getQualifierExpression().equals(literal)); + } + private void indentTextBlocks(TreeRangeMap replacements, List textBlocks) { // pjf specific - compute textBlock's parents & store indentation info for each parent Map textBlockToIndent = computeCustomTextBlocksIndent(textBlocks); diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java index fcb3ff7ee..db465bd33 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java @@ -16,7 +16,6 @@ import static com.google.common.truth.Truth.assertThat; import static com.palantir.javaformat.java.JavaFormatterOptions.Style; -import static org.junit.jupiter.api.Assertions.assertThrows; import com.google.common.base.Joiner; import com.google.common.collect.Iterables; @@ -128,8 +127,8 @@ class T { } @Test - void stopsAReflowThatWouldMoveACastToTheFirstPiece() { - // From google-java-format#1533: in `(Object) "a" + "b"` the cast takes only "a". + void stopsAReflowThatWouldMoveACastToTheFirstPiece() throws Exception { + // From google-java-format#1533: in `(Object) "a" + "b"` the cast takes only "a", so the literal stays whole. String input = """ class T { Object o = @@ -138,15 +137,13 @@ class T { } """; - FormatterException e = - assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create())); - - assertThat(e).hasMessageThat().contains("Something has gone terribly wrong"); + assertThat(StringWrapper.wrap(100, input, Formatter.create())).isEqualTo(input); } @Test - void stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece() { - // From google-java-format#1533: in `"a" + "b".new Inner()` the creation takes only "b". + void stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece() throws Exception { + // From google-java-format#1533: in `"a" + "b".new Inner()` the creation takes only "b", so the literal stays + // whole. String input = """ class T { Object o = @@ -155,10 +152,21 @@ class T { } """; - FormatterException e = - assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create())); + assertThat(StringWrapper.wrap(100, input, Formatter.create())).isEqualTo(input); + } + + @Test + void stopsAReflowThatWouldMoveAMethodReferenceToTheLastPiece() throws Exception { + // In `"a" + "b"::length` the method reference takes only "b", so the literal stays whole. + String input = """ + class T { + java.util.function.IntSupplier s = + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt" + ::length; + } + """; - assertThat(e).hasMessageThat().contains("Something has gone terribly wrong"); + assertThat(StringWrapper.wrap(100, input, Formatter.create())).isEqualTo(input); } @Test diff --git a/open-java-format/src/test/resources/test-cases-java21/stringwrapper/longLiteralsUnderACastANewOrAMethodReference.case b/open-java-format/src/test/resources/test-cases-java21/stringwrapper/longLiteralsUnderACastANewOrAMethodReference.case new file mode 100644 index 000000000..dd26b72d8 --- /dev/null +++ b/open-java-format/src/test/resources/test-cases-java21/stringwrapper/longLiteralsUnderACastANewOrAMethodReference.case @@ -0,0 +1,29 @@ +# A long literal stays whole under a cast, as the enclosing expression of a qualified .new and as the qualifier of a +# method reference, as it does as the expression of a member select: these bind tighter than +, so each would take +# only one piece of the reflowed literal, as in (Object) "a" + "b". A literal in parentheses, or under a +, is still +# reflowed. +---------- +class T { + Object o = (Object) "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"; + Object p = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything".new Inner(); + java.util.function.IntSupplier s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"::length; + Object q = (Object) ("one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"); + String fine = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"; +} +---------- +class T { + Object o = (Object) + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"; + Object p = + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything" + .new Inner(); + java.util.function.IntSupplier s = + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything" + ::length; + Object q = + (Object) ("one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to" + + " interrupt or to say anything"); + String fine = + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt" + + " or to say anything"; +}