1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/7079713/TestCircularClassfile.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,168 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7079713 1.30 + * @summary javac hangs when compiling a class that references a cyclically inherited class 1.31 + * @run main TestCircularClassfile 1.32 + */ 1.33 + 1.34 +import java.io.*; 1.35 +import java.net.URI; 1.36 +import java.util.Arrays; 1.37 +import javax.tools.JavaCompiler; 1.38 +import javax.tools.JavaFileObject; 1.39 +import javax.tools.SimpleJavaFileObject; 1.40 +import javax.tools.StandardJavaFileManager; 1.41 +import javax.tools.StandardLocation; 1.42 +import javax.tools.ToolProvider; 1.43 + 1.44 +import com.sun.source.util.JavacTask; 1.45 + 1.46 +public class TestCircularClassfile { 1.47 + 1.48 + enum SourceKind { 1.49 + A_EXTENDS_B("class B {} class A extends B { void m() {} }"), 1.50 + B_EXTENDS_A("class A { void m() {} } class B extends A {}"); 1.51 + 1.52 + String sourceStr; 1.53 + 1.54 + private SourceKind(String sourceStr) { 1.55 + this.sourceStr = sourceStr; 1.56 + } 1.57 + 1.58 + SimpleJavaFileObject getSource() { 1.59 + return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) { 1.60 + @Override 1.61 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 1.62 + return sourceStr; 1.63 + } 1.64 + }; 1.65 + } 1.66 + } 1.67 + 1.68 + enum TestKind { 1.69 + REPLACE_A("A.class"), 1.70 + REPLACE_B("B.class"); 1.71 + 1.72 + String targetClass; 1.73 + 1.74 + private TestKind(String targetClass) { 1.75 + this.targetClass = targetClass; 1.76 + } 1.77 + } 1.78 + 1.79 + enum ClientKind { 1.80 + METHOD_CALL1("A a = null; a.m();"), 1.81 + METHOD_CALL2("B b = null; b.m();"), 1.82 + CONSTR_CALL1("new A();"), 1.83 + CONSTR_CALL2("new B();"), 1.84 + ASSIGN1("A a = null; B b = a;"), 1.85 + ASSIGN2("B b = null; A a = b;"); 1.86 + 1.87 + String mainMethod; 1.88 + 1.89 + private ClientKind(String mainMethod) { 1.90 + this.mainMethod = mainMethod; 1.91 + } 1.92 + 1.93 + SimpleJavaFileObject getSource() { 1.94 + return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) { 1.95 + @Override 1.96 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 1.97 + return "class Test { public static void main(String[] args) { #M } }" 1.98 + .replace("#M", mainMethod); 1.99 + } 1.100 + }; 1.101 + } 1.102 + } 1.103 + 1.104 + public static void main(String... args) throws Exception { 1.105 + JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 1.106 + StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); 1.107 + int count = 0; 1.108 + for (SourceKind sk1 : SourceKind.values()) { 1.109 + for (SourceKind sk2 : SourceKind.values()) { 1.110 + for (TestKind tk : TestKind.values()) { 1.111 + for (ClientKind ck : ClientKind.values()) { 1.112 + new TestCircularClassfile("sub_"+count++, sk1, sk2, tk, ck).check(comp, fm); 1.113 + } 1.114 + } 1.115 + } 1.116 + } 1.117 + } 1.118 + 1.119 + static String workDir = System.getProperty("user.dir"); 1.120 + 1.121 + String destPath; 1.122 + SourceKind sk1; 1.123 + SourceKind sk2; 1.124 + TestKind tk; 1.125 + ClientKind ck; 1.126 + 1.127 + TestCircularClassfile(String destPath, SourceKind sk1, SourceKind sk2, TestKind tk, ClientKind ck) { 1.128 + this.destPath = destPath; 1.129 + this.sk1 = sk1; 1.130 + this.sk2 = sk2; 1.131 + this.tk = tk; 1.132 + this.ck = ck; 1.133 + } 1.134 + 1.135 + void check(JavaCompiler comp, StandardJavaFileManager fm) throws Exception { 1.136 + //step 1: compile first source code in the test subfolder 1.137 + File destDir = new File(workDir, destPath); destDir.mkdir(); 1.138 + //output dir must be set explicitly as we are sharing the fm (see bug 7026941) 1.139 + fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir)); 1.140 + JavacTask ct = (JavacTask)comp.getTask(null, fm, null, 1.141 + null, null, Arrays.asList(sk1.getSource())); 1.142 + ct.generate(); 1.143 + 1.144 + //step 2: compile second source code in a temp folder 1.145 + File tmpDir = new File(destDir, "tmp"); tmpDir.mkdir(); 1.146 + //output dir must be set explicitly as we are sharing the fm (see bug 7026941) 1.147 + fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir)); 1.148 + ct = (JavacTask)comp.getTask(null, fm, null, 1.149 + null, null, Arrays.asList(sk2.getSource())); 1.150 + ct.generate(); 1.151 + 1.152 + //step 3: move a classfile from the temp folder to the test subfolder 1.153 + File fileToMove = new File(tmpDir, tk.targetClass); 1.154 + File target = new File(destDir, tk.targetClass); 1.155 + target.delete(); 1.156 + boolean success = fileToMove.renameTo(target); 1.157 + 1.158 + if (!success) { 1.159 + throw new AssertionError("error when moving file " + tk.targetClass); 1.160 + } 1.161 + 1.162 + //step 4: compile the client class against the classes in the test subfolder 1.163 + //input/output dir must be set explicitly as we are sharing the fm (see bug 7026941) 1.164 + fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir)); 1.165 + fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(destDir)); 1.166 + ct = (JavacTask)comp.getTask(null, fm, null, 1.167 + null, null, Arrays.asList(ck.getSource())); 1.168 + 1.169 + ct.generate(); 1.170 + } 1.171 +}