changeset 57747:2ca0201e99f3

8232681: ArrayIndexOutOfBoundsException with text blocks in javac when using -Xlint Reviewed-by: sundar
author jlaskey
date Thu, 05 Dec 2019 09:22:21 -0400
parents 97d531d87bd7
children 0a94d3675782
files src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java test/langtools/tools/javac/TextBlockAPI.java
diffstat 2 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java	Thu Dec 05 12:52:07 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java	Thu Dec 05 09:22:21 2019 -0400
@@ -322,8 +322,8 @@
             String[] lines = string.split("\\R");
             int length = lines.length;
             // Extract last line.
-            String lastLine = lines[length - 1];
-            if (!optOut) {
+            String lastLine = length == 0 ? "" : lines[length - 1];
+             if (!optOut) {
                 // Prime with the last line indentation (may be blank.)
                 outdent = indent(lastLine);
                 for (String line : lines) {
--- a/test/langtools/tools/javac/TextBlockAPI.java	Thu Dec 05 12:52:07 2019 +0100
+++ b/test/langtools/tools/javac/TextBlockAPI.java	Thu Dec 05 09:22:21 2019 -0400
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 8223967
+ * @bug 8232681
  * @summary Unit tests for Text Block language changes
  * @library /tools/lib
  * @modules jdk.compiler/com.sun.tools.javac.api
@@ -49,6 +50,7 @@
         test5();
         test6();
         test7();
+        test8();
    }
 
     /*
@@ -195,6 +197,23 @@
                  "}");
     }
 
+    static void test8() {
+        String code = "class C {\n" +
+                      "\n" +
+                      "    void x() {\n" +
+                      "        String s = \"\"\"\n" +
+                      "\n" +
+                      "\"\"\";\n" +
+                      "    }\n" +
+                      "}\n";
+
+        new JavacTask(TOOLBOX)
+            .sources(code)
+            .classpath(".")
+            .options("--enable-preview", "-source", JDK_VERSION, "-encoding", "utf8", "-Xlint")
+            .run();
+     }
+
     /*
      * Test source for successful compile.
      */