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 8231827
27  * @summary Ensure SimpleTreeVisitor.visitBindingPattern and visitInstanceOf 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.BindingPatternTree;
40 import com.sun.source.tree.CompilationUnitTree;
41 import com.sun.source.tree.InstanceOfTree;
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 PatternsSimpleVisitorTest {
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         new PatternsSimpleVisitorTest().run();
51     }
52 
run()53     void run() throws Exception {
54         String code = "class Test {\n" +
55                       "    boolean t(Object o) {\n" +
56                       "         return o instanceof String s ? s.isEmpty() : false;\n" +
57                       "    }\n" +
58                       "}\n";
59         int[] callCount = new int[1];
60         int[] instanceOfNodeCount = new int[1];
61         int[] bindingPatternNodeCount = new int[1];
62         new TreePathScanner<Void, Void>() {
63             @Override
64             public Void visitInstanceOf(InstanceOfTree 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                             instanceOfNodeCount[0]++;
71                         }
72                         return null;
73                     }
74                 }, null);
75                 return super.visitInstanceOf(node, p);
76             }
77             @Override
78             public Void visitBindingPattern(BindingPatternTree node, Void p) {
79                 node.accept(new SimpleTreeVisitor<Void, Void>() {
80                     @Override
81                     protected Void defaultAction(Tree defaultActionNode, Void p) {
82                         callCount[0]++;
83                         if (node == defaultActionNode) {
84                             bindingPatternNodeCount[0]++;
85                         }
86                         return null;
87                     }
88                 }, null);
89                 return super.visitBindingPattern(node, p);
90             }
91         }.scan(parse(code), null);
92 
93         if (callCount[0] != 2 || instanceOfNodeCount[0] != 1 ||
94             bindingPatternNodeCount[0] != 1) {
95             throw new AssertionError("Unexpected counts; callCount=" + callCount[0] +
96                                      ", switchExprNodeCount=" + instanceOfNodeCount[0] +
97                                      ", yieldNodeCount=" + bindingPatternNodeCount[0]);
98         }
99     }
100 
parse(String code)101     private CompilationUnitTree parse(String code) throws IOException {
102         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
103         assert tool != null;
104         DiagnosticListener<JavaFileObject> noErrors = d -> {};
105 
106         StringWriter out = new StringWriter();
107         JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
108             null, null,
109             Arrays.asList(new MyFileObject(code)));
110         return ct.parse().iterator().next();
111     }
112 
113     static class MyFileObject extends SimpleJavaFileObject {
114         private String text;
115 
MyFileObject(String text)116         public MyFileObject(String text) {
117             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
118             this.text = text;
119         }
120 
121         @Override
getCharContent(boolean ignoreEncodingErrors)122         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
123             return text;
124         }
125     }
126 }
127