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 rule cases can be parsed correctly for complex expressions.
  28  * @modules jdk.compiler
  29  */
  30 
  31 import java.io.StringWriter;
  32 import java.net.URI;
  33 import java.util.AbstractMap.SimpleEntry;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.List;
  37 import java.util.Locale;
  38 import java.util.Map.Entry;
  39 
  40 import javax.tools.*;
  41 
  42 import com.sun.source.tree.CaseTree;
  43 import com.sun.source.tree.CompilationUnitTree;
  44 import com.sun.source.util.JavacTask;
  45 import com.sun.source.util.TreePathScanner;
  46 import com.sun.source.util.Trees;
  47 
  48 public class RuleParsingTest {
  49 
  50     public static void main(String[] args) throws Exception {
  51         new RuleParsingTest().testParseComplexExpressions();
  52     }
  53 
  54     void testParseComplexExpressions() throws Exception {
  55         String[] expressions = {
  56             "(a)",
  57             "a",
  58             "a + a",
  59             "~a + a",
  60             "a = a",
  61             "a += a",
  62             "a + (a)",
  63             "a + (a) b",
  64             "true ? a : b",
  65             "m(() -> {})",
  66             "m(() -> 1)",
  67             "m(a -> 1)",
  68             "m((t a) -> 1)",
  69         };
  70         StringBuilder code = new StringBuilder();
  71         List<Entry<Long, Long>> spans = new ArrayList<>();
  72         code.append("class Test {\n" +
  73                     "    void t(int i) {\n");
  74         for (boolean switchExpr : new boolean[] {false, true}) {
  75             if (switchExpr) {
  76                 code.append("         int j = switch(i) {\n");
  77             } else {
  78                 code.append("         switch(i) {\n");
  79             }
  80             for (String expr : expressions) {
  81                 code.append("case ");
  82                 int start = code.length();
  83                 code.append(expr);
  84                 spans.add(new SimpleEntry<>((long) start, (long) code.length()));
  85                 code.append(" -> {}");
  86             }
  87             code.append("         };\n");
  88         }
  89         code.append("    }\n" +
  90                     "}\n");
  91         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  92         assert tool != null;
  93         DiagnosticListener<JavaFileObject> noErrors = d -> { throw new AssertionError(d.getMessage(null)); };
  94 
  95         StringWriter out = new StringWriter();
  96         JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
  97             List.of("--enable-preview", "-source", "12"), null,
  98             Arrays.asList(new MyFileObject(code.toString())));
  99         CompilationUnitTree cut = ct.parse().iterator().next();
 100         Trees trees = Trees.instance(ct);
 101         new TreePathScanner<Void, Void>() {
 102             @Override
 103             public Void visitCase(CaseTree node, Void p) {
 104                 long start = trees.getSourcePositions().getStartPosition(cut, node.getExpression());
 105                 long end = trees.getSourcePositions().getEndPosition(cut, node.getExpression());
 106                 if (!spans.remove(new SimpleEntry<>(start, end))) {
 107                     throw new AssertionError("Did not find an expression span in expected spans: " +
 108                                              start + "-" + end +
 109                                              " '" + node.getExpression().toString() + "'");
 110                 }
 111                 return super.visitCase(node, p);
 112             }
 113         }.scan(cut, null);
 114 
 115         if (!spans.isEmpty()) {
 116             throw new AssertionError("Remaning spans: " + spans);
 117         }
 118     }
 119 
 120     static class MyFileObject extends SimpleJavaFileObject {
 121         private String text;
 122 
 123         public MyFileObject(String text) {
 124             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
 125             this.text = text;
 126         }
 127 
 128         @Override
 129         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 130             return text;
 131         }
 132     }
 133 }