1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8206986
  27  * @summary Ensure than parser can parse incomplete sources
  28  * @modules jdk.compiler
  29  */
  30 
  31 import java.io.StringWriter;
  32 import java.net.URI;
  33 import java.util.Arrays;
  34 import java.util.List;
  35 
  36 import javax.tools.*;
  37 
  38 import com.sun.source.util.JavacTask;
  39 
  40 public class ParseIncomplete {
  41 
  42     private static final String CODE =
  43             "public class C {" +
  44             "    void t1(Integer i) {" +
  45             "        switch (i) {" +
  46             "            case null: i++; break;" +
  47             "            case 0, 1: i++; break;" +
  48             "            default: i++; break;" +
  49             "        }" +
  50             "    }" +
  51             "    int t2(Integer i) {" +
  52             "        return switch (i) {" +
  53             "            case null: break 0;" +
  54             "            case 0, 1: break 1;" +
  55             "            default: break 2;" +
  56             "        }" +
  57             "    }" +
  58             "}";
  59 
  60     public static void main(String[] args) throws Exception {
  61         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  62         assert tool != null;
  63         DiagnosticListener<JavaFileObject> noErrors = d -> {};
  64 
  65         for (int i = 0; i < CODE.length(); i++) {
  66             String code = CODE.substring(0, i + 1);
  67             StringWriter out = new StringWriter();
  68             try {
  69                 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
  70                     List.of("-XDdev", "--enable-preview", "-source", "13"), null,
  71                     Arrays.asList(new MyFileObject(code)));
  72                 ct.parse().iterator().next();
  73             } catch (Throwable t) {
  74                 System.err.println("Unexpected exception for code: " + code);
  75                 System.err.println("output: " + out);
  76                 throw t;
  77             }
  78             if (!out.toString().isEmpty()) {
  79                 System.err.println("Unexpected compiler for code: " + code);
  80                 System.err.println(out);
  81                 throw new AssertionError();
  82             }
  83         }
  84     }
  85 
  86     static class MyFileObject extends SimpleJavaFileObject {
  87         private String text;
  88 
  89         public MyFileObject(String text) {
  90             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
  91             this.text = text;
  92         }
  93 
  94         @Override
  95         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
  96             return text;
  97         }
  98     }
  99 }