langtools/test/tools/javac/lambda/T8031967.java
changeset 24649 1760d92bc6d2
child 30730 d3ce7619db2c
equal deleted inserted replaced
24648:3b639cd0c8bd 24649:1760d92bc6d2
       
     1 /*
       
     2  * Copyright (c) 2014, 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 8031967
       
    27  * @summary Ensure javac can handle very deeply nested chain of method invocations occurring as
       
    28  *          a parameter to other method invocations.
       
    29  * @run main T8031967
       
    30  */
       
    31 
       
    32 import java.io.IOException;
       
    33 import java.net.URI;
       
    34 import java.util.Arrays;
       
    35 import java.util.List;
       
    36 
       
    37 import javax.tools.DiagnosticListener;
       
    38 import javax.tools.JavaCompiler;
       
    39 import javax.tools.JavaFileObject;
       
    40 import javax.tools.SimpleJavaFileObject;
       
    41 import javax.tools.ToolProvider;
       
    42 
       
    43 import com.sun.source.util.JavacTask;
       
    44 
       
    45 public class T8031967 {
       
    46 
       
    47     public static void main(String... args) throws IOException {
       
    48         new T8031967().run();
       
    49     }
       
    50 
       
    51     final int depth = 50;
       
    52 
       
    53     private void run() throws IOException {
       
    54         runTestCase(true);
       
    55         runTestCase(false);
       
    56     }
       
    57 
       
    58     private void runTestCase(boolean withErrors) throws IOException {
       
    59         StringBuilder code = new StringBuilder();
       
    60 
       
    61         code.append("public class Test {\n" +
       
    62                     "    private void test() {\n" +
       
    63                     "        GroupLayout l = new GroupLayout();\n" +
       
    64                     "        l.setHorizontalGroup(\n");
       
    65 
       
    66         gen(code, depth);
       
    67         code.append("        );\n" +
       
    68                     "    }\n");
       
    69         if (!withErrors) {
       
    70             code.append("    class GroupLayout {\n" +
       
    71                         "        ParallelGroup createParallelGroup() {return null;}\n" +
       
    72                         "        ParallelGroup createParallelGroup(int i) {return null;}\n" +
       
    73                         "        ParallelGroup createParallelGroup(int i, int j) {return null;}\n" +
       
    74                         "        void setHorizontalGroup(Group g) { }\n" +
       
    75                         "    }\n" +
       
    76                         "    \n" +
       
    77                         "    class Group {\n" +
       
    78                         "        Group addGroup(Group g) { return this; }\n" +
       
    79                         "        Group addGroup(int i, Group g) { return this; }\n" +
       
    80                         "        Group addGap(int i) { return this; }\n" +
       
    81                         "        Group addGap(long l) { return this; }\n" +
       
    82                         "        Group addGap(int i, int j) { return this; }\n" +
       
    83                         "        Group addComponent(Object c) { return this; }\n" +
       
    84                         "        Group addComponent(int i, Object c) { return this; }\n" +
       
    85                         "    }\n" +
       
    86                         "    class ParallelGroup extends Group {\n" +
       
    87                         "        Group addGroup(Group g) { return this; }\n" +
       
    88                         "        Group addGroup(int i, Group g) { return this; }\n" +
       
    89                         "        Group addGap(int i) { return this; }\n" +
       
    90                         "        Group addGap(int i, int j) { return this; }\n" +
       
    91                         "        Group addComponent(Object c) { return this; }\n" +
       
    92                         "        Group addComponent(int i, Object c) { return this; }\n" +
       
    93                         "    }\n");
       
    94         }
       
    95 
       
    96         code.append("}\n");
       
    97 
       
    98         JavaSource source = new JavaSource(code.toString());
       
    99         List<JavaSource> sourceList = Arrays.asList(source);
       
   100         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
   101         DiagnosticListener<JavaFileObject> noErrors = (diagnostic) -> {
       
   102             throw new IllegalStateException("Should not produce errors: " + diagnostic);
       
   103         };
       
   104         JavacTask task = (JavacTask) compiler.getTask(null, null, withErrors ? null : noErrors,
       
   105                 null, null, sourceList);
       
   106 
       
   107         task.analyze();
       
   108     }
       
   109 
       
   110     private void gen(StringBuilder code, int depth) {
       
   111         code.append("l.createParallelGroup()\n");
       
   112         if (depth > 0) {
       
   113             code.append(".addGroup(\n");
       
   114             gen(code, depth - 1);
       
   115             code.append(")");
       
   116         }
       
   117 
       
   118         code.append(".addGap(1)\n" +
       
   119                     ".addComponent(new Object())\n" +
       
   120                     ".addGap(1)\n" +
       
   121                     ".addComponent(new Object())");
       
   122     }
       
   123 
       
   124     class JavaSource extends SimpleJavaFileObject {
       
   125 
       
   126         final String code;
       
   127         public JavaSource(String code) {
       
   128             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
   129             this.code = code;
       
   130         }
       
   131 
       
   132         @Override
       
   133         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
   134             return code;
       
   135         }
       
   136     }
       
   137 }