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