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 BreakTree.getLabel returns reasonable values 28 * @modules jdk.compiler 29 */ 30 31 import java.io.StringWriter; 32 import java.net.URI; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.List; 36 37 import javax.lang.model.element.Name; 38 import javax.tools.*; 39 40 import com.sun.source.tree.BreakTree; 41 import com.sun.source.util.JavacTask; 42 import com.sun.source.util.TreePathScanner; 43 44 public class BreakTest { 45 46 private static final String CODE = 47 "public class C {" + 48 " void t1(Integer i) {" + 49 " LABEL: switch (i) {" + 50 " case null: i++; break LABEL;" + 51 " default: i++; break;" + 52 " }" + 53 " }" + 54 " int t2(Integer i) {" + 55 " return switch (i) {" + 56 " case null: break LABEL;" + 57 " default: yield 2;" + 58 " }" + 59 " }" + 60 "}"; 61 62 public static void main(String[] args) throws Exception { 63 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 64 String sourceVersion = Integer.toString(Runtime.version().feature()); 65 assert tool != null; 66 DiagnosticListener<JavaFileObject> noErrors = d -> {}; 67 68 StringWriter out = new StringWriter(); 69 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors, 70 List.of("-XDdev"), null, 71 Arrays.asList(new MyFileObject(CODE))); 72 List<String> labels = new ArrayList<>(); 73 new TreePathScanner<Void, Void>() { 74 @Override 75 public Void visitBreak(BreakTree node, Void p) { 76 Name label = node.getLabel(); 77 labels.add(label != null ? label.toString() : null); 78 return super.visitBreak(node, p); 79 } 80 }.scan(ct.parse(), null); 81 82 List<String> expected = Arrays.asList("LABEL", null, "LABEL"); 83 84 if (!expected.equals(labels)) { 85 throw new AssertionError("Unexpected labels found: " + labels); 86 } 87 } 88 89 static class MyFileObject extends SimpleJavaFileObject { 90 private String text; 91 92 public MyFileObject(String text) { 93 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 94 this.text = text; 95 } 96 97 @Override 98 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 99 return text; 100 } 101 } 102 }