1 /*
   2  * Copyright (c) 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 8223305
  27  * @summary Verify Tree.toString() related to switch expressions
  28  * @modules jdk.compiler
  29  */
  30 
  31 import java.net.URI;
  32 import java.util.Arrays;
  33 import java.util.List;
  34 
  35 import javax.tools.*;
  36 
  37 import com.sun.source.util.JavacTask;
  38 
  39 public class ExpressionSwitchToString {
  40 
  41     private static final String CODE =
  42             "public class C {" +
  43             "    void t1(Integer i) {" +
  44             "        switch (i) {" +
  45             "            case null: i++; break;" +
  46             "            case 0, 1: i++; break;" +
  47             "            default: i++; break;" +
  48             "        }" +
  49             "    }" +
  50             "    int t2(Integer i) {" +
  51             "        return switch (i) {" +
  52             "            case null: yield 0;" +
  53             "            case 0, 1: yield 1;" +
  54             "            default: yield 2;" +
  55             "        }" +
  56             "    }" +
  57             "}";
  58 
  59     private static final String EXPECTED =
  60             "\n" +
  61             "public class C {\n" +
  62             "    \n" +
  63             "    void t1(Integer i) {\n" +
  64             "        switch (i) {\n" +
  65             "        case null:\n" +
  66             "            i++;\n" +
  67             "            break;\n" +
  68             "        \n" +
  69             "        case 0, 1:\n" +
  70             "            i++;\n" +
  71             "            break;\n" +
  72             "        \n" +
  73             "        default:\n" +
  74             "            i++;\n" +
  75             "            break;\n" +
  76             "        \n" +
  77             "        }\n" +
  78             "    }\n" +
  79             "    \n" +
  80             "    int t2(Integer i) {\n" +
  81             "        return switch (i) {\n" +
  82             "        case null:\n" +
  83             "            yield 0;\n" +
  84             "        \n" +
  85             "        case 0, 1:\n" +
  86             "            yield 1;\n" +
  87             "        \n" +
  88             "        default:\n" +
  89             "            yield 2;\n" +
  90             "        \n" +
  91             "        };\n" +
  92             "    }\n" +
  93             "}";
  94 
  95     public static void main(String[] args) throws Exception {
  96         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  97         assert tool != null;
  98         DiagnosticListener<JavaFileObject> noErrors = d -> {};
  99         String sourceVersion = Integer.toString(Runtime.version().feature());
 100 
 101         JavacTask ct = (JavacTask) tool.getTask(null, null, noErrors,
 102             List.of("-XDdev"), null,
 103             Arrays.asList(new MyFileObject(CODE)));
 104         String actualCode = ct.parse().iterator().next().toString();
 105         actualCode = actualCode.replace(System.getProperty("line.separator"), "\n");
 106         if (!EXPECTED.equals(actualCode)) {
 107             throw new AssertionError("Unexpected toString outcome: " +
 108                                      actualCode);
 109         }
 110     }
 111 
 112     static class MyFileObject extends SimpleJavaFileObject {
 113         private String text;
 114 
 115         public MyFileObject(String text) {
 116             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
 117             this.text = text;
 118         }
 119 
 120         @Override
 121         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 122             return text;
 123         }
 124     }
 125 }