< prev index next >

test/langtools/tools/javac/switchexpr/BreakTest.java

Print this page




  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;


  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", "13"), 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;
< prev index next >