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 SimpleTreeVisitor.visitSwitchExpression behaves as it should
  28  * @modules jdk.compiler
  29  */
  30 
  31 import java.io.IOException;
  32 import java.io.StringWriter;
  33 import java.net.URI;
  34 import java.util.Arrays;
  35 import java.util.List;
  36 
  37 import javax.tools.*;
  38 
  39 import com.sun.source.tree.CompilationUnitTree;
  40 import com.sun.source.tree.SwitchExpressionTree;
  41 import com.sun.source.tree.YieldTree;
  42 import com.sun.source.tree.Tree;
  43 import com.sun.source.util.JavacTask;
  44 import com.sun.source.util.SimpleTreeVisitor;
  45 import com.sun.source.util.TreePathScanner;
  46 
  47 public class SwitchExpressionSimpleVisitorTest {
  48 
  49     public static void main(String[] args) throws Exception {
  50         new SwitchExpressionSimpleVisitorTest().run();
  51     }
  52 
  53     void run() throws Exception {
  54         String code = "class Test {\n" +
  55                       "    int t(int i) {\n" +
  56                       "         return switch(i) {\n" +
  57                       "              default: yield -1;\n" +
  58                       "         }\n" +
  59                       "    }\n" +
  60                       "}\n";
  61         int[] callCount = new int[1];
  62         int[] switchExprNodeCount = new int[1];
  63         int[] yieldNodeCount = new int[1];
  64         new TreePathScanner<Void, Void>() {
  65             @Override
  66             public Void visitSwitchExpression(SwitchExpressionTree node, Void p) {
  67                 node.accept(new SimpleTreeVisitor<Void, Void>() {
  68                     @Override
  69                     protected Void defaultAction(Tree defaultActionNode, Void p) {
  70                         callCount[0]++;
  71                         if (node == defaultActionNode) {
  72                             switchExprNodeCount[0]++;
  73                         }
  74                         return null;
  75                     }
  76                 }, null);
  77                 return super.visitSwitchExpression(node, p);
  78             }
  79             @Override
  80             public Void visitYield(YieldTree node, Void p) {
  81                 node.accept(new SimpleTreeVisitor<Void, Void>() {
  82                     @Override
  83                     protected Void defaultAction(Tree defaultActionNode, Void p) {
  84                         callCount[0]++;
  85                         if (node == defaultActionNode) {
  86                             yieldNodeCount[0]++;
  87                         }
  88                         return null;
  89                     }
  90                 }, null);
  91                 return super.visitYield(node, p);
  92             }
  93         }.scan(parse(code), null);
  94 
  95         if (callCount[0] != 2 || switchExprNodeCount[0] != 1 ||
  96             yieldNodeCount[0] != 1) {
  97             throw new AssertionError("Unexpected counts; callCount=" + callCount[0] +
  98                                      ", switchExprNodeCount=" + switchExprNodeCount[0] +
  99                                      ", yieldNodeCount=" + yieldNodeCount[0]);
 100         }
 101     }
 102 
 103     private CompilationUnitTree parse(String code) throws IOException {
 104         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
 105         assert tool != null;
 106         DiagnosticListener<JavaFileObject> noErrors = d -> {};
 107 
 108         StringWriter out = new StringWriter();
 109         JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
 110             List.of("--enable-preview", "-source", Integer.toString(Runtime.version().feature())), null,
 111             Arrays.asList(new MyFileObject(code)));
 112         return ct.parse().iterator().next();
 113     }
 114 
 115     static class MyFileObject extends SimpleJavaFileObject {
 116         private String text;
 117 
 118         public MyFileObject(String text) {
 119             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
 120             this.text = text;
 121         }
 122 
 123         @Override
 124         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 125             return text;
 126         }
 127     }
 128 }