1 /*
   2  * Copyright (c) 20018, 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: break 2;" +
  58             "        }" +
  59             "    }" +
  60             "}";
  61 
  62     public static void main(String[] args) throws Exception {
  63         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  64         assert tool != null;
  65         DiagnosticListener<JavaFileObject> noErrors = d -> {};
  66 
  67         StringWriter out = new StringWriter();
  68         JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
  69             List.of("-XDdev", "--enable-preview", "-source", "12"), null,
  70             Arrays.asList(new MyFileObject(CODE)));
  71         List<String> labels = new ArrayList<>();
  72         new TreePathScanner<Void, Void>() {
  73             @Override
  74             public Void visitBreak(BreakTree node, Void p) {
  75                 Name label = node.getLabel();
  76                 labels.add(label != null ? label.toString() : null);
  77                 return super.visitBreak(node, p);
  78             }
  79         }.scan(ct.parse(), null);
  80 
  81         List<String> expected = Arrays.asList("LABEL", null, "LABEL", null);
  82 
  83         if (!expected.equals(labels)) {
  84             throw new AssertionError("Unexpected labels found: " + labels);
  85         }
  86     }
  87 
  88     static class MyFileObject extends SimpleJavaFileObject {
  89         private String text;
  90 
  91         public MyFileObject(String text) {
  92             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
  93             this.text = text;
  94         }
  95 
  96         @Override
  97         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
  98             return text;
  99         }
 100     }
 101 }