1 /* 2 * Copyright (c) 2018, 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 CaseTree methods return expected values 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.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 37 import java.util.stream.Collectors; 38 39 import javax.tools.*; 40 41 import com.sun.source.tree.CaseTree; 42 import com.sun.source.tree.CompilationUnitTree; 43 import com.sun.source.tree.Tree; 44 import com.sun.source.util.JavacTask; 45 import com.sun.source.util.TreePathScanner; 46 47 public class CaseTest { 48 49 public static void main(String[] args) throws Exception { 50 new CaseTest().testLabels(); 51 new CaseTest().testStatement(); 52 new CaseTest().testRule(); 53 } 54 55 void testLabels() throws Exception { 56 String code = "class Test {\n" + 57 " void t(int i) {\n" + 58 " switch(i) {\n" + 59 " case 0: break;\n" + 60 " case 1, 2: breal;\n" + 61 " default: breal;\n" + 62 " }\n" + 63 " }\n" + 64 "}\n"; 65 List<String> labels = new ArrayList<>(); 66 new TreePathScanner<Void, Void>() { 67 @Override 68 public Void visitCase(CaseTree node, Void p) { 69 labels.add(String.valueOf(node.getExpression())); 70 labels.add(node.getExpressions().stream() 71 .map(String::valueOf) 72 .collect(Collectors.joining(",", "[", "]"))); 73 return super.visitCase(node, p); 74 } 75 }.scan(parse(code), null); 76 77 List<String> expected = Arrays.asList("0", "[0]", "1", "[1,2]", "null", "[]"); 78 79 if (!expected.equals(labels)) { 80 throw new AssertionError("Unexpected labels found: " + labels); 81 } 82 } 83 84 void testStatement() throws Exception { 85 String code = "class Test {\n" + 86 " void t(int i) {\n" + 87 " switch(i) {\n" + 88 " case 0:" + 89 " System.err.println();\n" + 90 " break;\n" + 91 " }\n" + 92 " }\n" + 93 "}\n"; 94 new TreePathScanner<Void, Void>() { 95 @Override 96 public Void visitCase(CaseTree node, Void p) { 97 if (node.getStatements().size() != 2) { 98 throw new AssertionError("Unexpected statements: " + node.getStatements()); 99 } 100 if (node.getBody() != null) { 101 throw new AssertionError("Unexpected body: " + node.getBody()); 102 } 103 return super.visitCase(node, p); 104 } 105 }.scan(parse(code), null); 106 } 107 108 void testRule() throws Exception { 109 String code = "class Test {\n" + 110 " void t(int i) {\n" + 111 " switch(i) {\n" + 112 " case 0 -> {" + 113 " System.err.println();\n" + 114 " };\n" + 115 " }\n" + 116 " }\n" + 117 "}\n"; 118 new TreePathScanner<Void, Void>() { 119 @Override 120 public Void visitCase(CaseTree node, Void p) { 121 if (node.getStatements() != null) { 122 throw new AssertionError("Unexpected statements: " + node.getStatements()); 123 } 124 if (node.getBody().getKind() != Tree.Kind.BLOCK) { 125 throw new AssertionError("Unexpected body: " + node.getBody()); 126 } 127 return super.visitCase(node, p); 128 } 129 }.scan(parse(code), null); 130 } 131 132 private CompilationUnitTree parse(String code) throws IOException { 133 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 134 assert tool != null; 135 DiagnosticListener<JavaFileObject> noErrors = d -> {}; 136 137 StringWriter out = new StringWriter(); 138 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors, 139 List.of("-XDdev", "--enable-preview", "-source", "12"), null, 140 Arrays.asList(new MyFileObject(code))); 141 return ct.parse().iterator().next(); 142 } 143 144 static class MyFileObject extends SimpleJavaFileObject { 145 private String text; 146 147 public MyFileObject(String text) { 148 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 149 this.text = text; 150 } 151 152 @Override 153 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 154 return text; 155 } 156 } 157 }