1 /* 2 * Copyright (c) 2018, 2019, 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: yield 0;" + 54 " case 0, 1: yield 1;" + 55 " default: yield 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 String sourceVersion = Integer.toString(Runtime.version().feature()); 65 66 for (int i = 0; i < CODE.length(); i++) { 67 String code = CODE.substring(0, i + 1); 68 StringWriter out = new StringWriter(); 69 try { 70 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors, 71 List.of("-XDdev"), null, 72 Arrays.asList(new MyFileObject(code))); 73 ct.parse().iterator().next(); 74 } catch (Throwable t) { 75 System.err.println("Unexpected exception for code: " + code); 76 System.err.println("output: " + out); 77 throw t; 78 } 79 if (!out.toString().isEmpty()) { 80 System.err.println("Unexpected compiler for code: " + code); 81 System.err.println(out); 82 throw new AssertionError(); 83 } 84 } 85 } 86 87 static class MyFileObject extends SimpleJavaFileObject { 88 private String text; 89 90 public MyFileObject(String text) { 91 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 92 this.text = text; 93 } 94 95 @Override 96 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 97 return text; 98 } 99 } 100 }