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.Tree; 42 import com.sun.source.util.JavacTask; 43 import com.sun.source.util.SimpleTreeVisitor; 44 import com.sun.source.util.TreePathScanner; 45 46 public class SwitchExpressionSimpleVisitorTest { 47 48 public static void main(String[] args) throws Exception { 49 new SwitchExpressionSimpleVisitorTest().run(); 50 } 51 52 void run() throws Exception { 53 String code = "class Test {\n" + 54 " int t(int i) {\n" + 55 " return switch(i) {\n" + 56 " default: break -1;\n" + 57 " }\n" + 58 " }\n" + 59 "}\n"; 60 int[] callCount = new int[1]; 61 int[] switchExprNodeCount = new int[1]; 62 new TreePathScanner<Void, Void>() { 63 @Override 64 public Void visitSwitchExpression(SwitchExpressionTree node, Void p) { 65 node.accept(new SimpleTreeVisitor<Void, Void>() { 66 @Override 67 protected Void defaultAction(Tree defaultActionNode, Void p) { 68 callCount[0]++; 69 if (node == defaultActionNode) { 70 switchExprNodeCount[0]++; 71 } 72 return null; 73 } 74 }, null); 75 return super.visitSwitchExpression(node, p); 76 } 77 }.scan(parse(code), null); 78 79 if (callCount[0] != 1 || switchExprNodeCount[0] != 1) { 80 throw new AssertionError("Unexpected counts; callCount=" + callCount[0] + 81 ", switchExprNodeCount=" + switchExprNodeCount[0]); 82 } 83 } 84 85 private CompilationUnitTree parse(String code) throws IOException { 86 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 87 assert tool != null; 88 DiagnosticListener<JavaFileObject> noErrors = d -> {}; 89 90 StringWriter out = new StringWriter(); 91 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors, 92 List.of("--enable-preview", "-source", Integer.toString(Runtime.version().feature())), null, 93 Arrays.asList(new MyFileObject(code))); 94 return ct.parse().iterator().next(); 95 } 96 97 static class MyFileObject extends SimpleJavaFileObject { 98 private String text; 99 100 public MyFileObject(String text) { 101 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 102 this.text = text; 103 } 104 105 @Override 106 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 107 return text; 108 } 109 } 110 }