Java 14 个文本块前导 \r\n 在 Eclipse 4.15.0 中使用时插入
Java 14 text block leading \r\n inserted when used in Eclipse 4.15.0
我正在学习如何处理 java 14 个(预览)文本块。
在 Junit 测试中使用以下文本块时,我 运行 遇到以下意外功能(简化的代码示例,在实际测试中我使用 HTML 片段):
assertEquals("""
test""", "test");
执行此测试会导致错误,因为 "\r\ntest"
与 "test"
不匹配:
org.junit.ComparisonFailure: expected:<[
]test> but was:<[]test>
当我查阅文档 (https://docs.oracle.com/javase/specs/jls/se14/preview/specs/text-blocks-jls.html) 时,它的字面意思是:
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.
我是不是漏掉了什么?
几个问题和建议后更新:
我创建一个小测试 class:
public class Tester {
public static void main(String[] args) {
System.out.println(">>" + """
test""");
}
}
执行时打印以下内容:
>>
test
这个class的字节码是
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
}
看来你做错了什么。 JLS 说:
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.
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"
我也在我的机器上检查过:
System.out.println("""
test""".equals("test")); // Prints true
终于,问题搞清楚了。它与 Eclipse 相关。当我从命令提示符(与 Eclipse 使用相同的 java 版本)编译和 运行 代码时,一切都按预期工作。当我从 Eclipse 中编译和 运行 相同的代码时,文本块前面加上 \r\n。
我将编辑问题措辞。
Eclipse 2020-03(4.15)不支持Java14.您已经从Eclipse Marketplace安装了Java 14 Support for Eclipse 2020-03 (4.15),是即将推出的[=25]的preview/beta版本=] 14支持2020年6月17日发布的Eclipse 2020-06(4.16)
在当前候选版本中,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)。
Marketplace 条目仍然指的是过时的预览版,并且可能会在发布后不久进行更新(就像之前其他新支持的 Java 版本一样)。
请注意,文本块是 Java 13 (first preview) and Java 14 (second preview; improved) and therefore not yet part of a Java language specification 的预览功能。它不打算用于生产。
我正在学习如何处理 java 14 个(预览)文本块。 在 Junit 测试中使用以下文本块时,我 运行 遇到以下意外功能(简化的代码示例,在实际测试中我使用 HTML 片段):
assertEquals("""
test""", "test");
执行此测试会导致错误,因为 "\r\ntest"
与 "test"
不匹配:
org.junit.ComparisonFailure: expected:<[
]test> but was:<[]test>
当我查阅文档 (https://docs.oracle.com/javase/specs/jls/se14/preview/specs/text-blocks-jls.html) 时,它的字面意思是:
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.
我是不是漏掉了什么?
几个问题和建议后更新:
我创建一个小测试 class:
public class Tester {
public static void main(String[] args) {
System.out.println(">>" + """
test""");
}
}
执行时打印以下内容:
>>
test
这个class的字节码是
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
}
看来你做错了什么。 JLS 说:
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.
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"
我也在我的机器上检查过:
System.out.println("""
test""".equals("test")); // Prints true
终于,问题搞清楚了。它与 Eclipse 相关。当我从命令提示符(与 Eclipse 使用相同的 java 版本)编译和 运行 代码时,一切都按预期工作。当我从 Eclipse 中编译和 运行 相同的代码时,文本块前面加上 \r\n。
我将编辑问题措辞。
Eclipse 2020-03(4.15)不支持Java14.您已经从Eclipse Marketplace安装了Java 14 Support for Eclipse 2020-03 (4.15),是即将推出的[=25]的preview/beta版本=] 14支持2020年6月17日发布的Eclipse 2020-06(4.16)
在当前候选版本中,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)。
Marketplace 条目仍然指的是过时的预览版,并且可能会在发布后不久进行更新(就像之前其他新支持的 Java 版本一样)。
请注意,文本块是 Java 13 (first preview) and Java 14 (second preview; improved) and therefore not yet part of a Java language specification 的预览功能。它不打算用于生产。