Search code examples
javaeclipsejava-14ecjjava-text-blocks

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


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
}

Solution

  • 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.