Java 14 text block leading \r\n inserted when used in Eclipse 4.15.0

1.1k views Asked by At

I am in the process of learning how to cope with java 14 (preview) text blocks. When using following text block in a Junit test I run across the following unexpected feature (simplified code example, in the real test I use an HTML fragment):

assertEquals("""
    test""", "test");

Executing this test results in an error since "\r\ntest" does not match "test":

org.junit.ComparisonFailure: expected:<[
]test> but was:<[]test>

When I consult the documentation (https://docs.oracle.com/javase/specs/jls/se14/preview/specs/text-blocks-jls.html), it literally states:

The content of a text block is the sequence of characters that begins immediately after the line terminator of the opening delimiter, and ends immediately before the first double quote of the closing delimiter.

Did I miss something?
Update after several questions and suggestions:
I create a small test class:

public class Tester {

    public static void main(String[] args) {
        System.out.println(">>" + """
            test""");
    }
}

which, when executed, prints the following:

>>
test

The bytecode of this class is

Compiled from "Tester.java"
public class nl.paul.testapp.testutil.Tester {
  public nl.paul.testapp.testutil.Tester();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #13                 // String >>test
       5: invokevirtual #15                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}
3

There are 3 answers

5
howlger On BEST ANSWER

Eclipse 2020-03 (4.15) does not support Java 14. You have installed Java 14 Support for Eclipse 2020-03 (4.15) from the Eclipse Marketplace, which is the preview/beta version of the upcoming Java 14 support of Eclipse 2020-06 (4.16) which will be released on June 17th, 2020.

In the current release candidate, Eclipse 2020-06 (4.16) RC1, it works as expected. So it is a already fixed Eclipse Compiler for Java (ecj) bug in a build, not in a release (according to the Eclipse Development Process).

The Marketplace entry still refers to an outdated preview version and will probably be updated shortly after the release (as it happened with other new supported Java versions before).

Please note, text blocks are preview features of Java 13 (first preview) and Java 14 (second preview; improved) and therefore not yet part of a Java language specification. It is not intended to be used in production.

3
PaulN On

At last, the problem is clear. It is Eclipse related. When I compile and run the code from a command prompt (same java version as Eclipse uses), everything works as expected. When I compile and run the same code from within Eclipse, the text block is prepended with \r\n.
I will edit the question phrasing.

10
ZhekaKozlov On

It looks you're doing something wrong. The JLS says:

The content of a text block is the sequence of characters that begins immediately after the line terminator of the opening delimiter, and ends immediately before the first double quote of the closing delimiter.

JEP 378 (Text Blocks) confirms it:

The opening delimiter is a sequence of three double quote characters (""") followed by zero or more white spaces followed by a line terminator. The content begins at the first character after the line terminator of the opening delimiter.

...

"""
line 1
line 2
line 3
"""
is equivalent to the string literal:

"line 1\nline 2\nline 3\n"

I also checked it on my machine:

System.out.println("""  
        test""".equals("test")); // Prints true