Fri, 24 Apr 2020 03:58:51 +0100
Merge
mcimadamore@1072 | 1 | /* |
mcimadamore@1072 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1072 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1072 | 4 | * |
mcimadamore@1072 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1072 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1072 | 7 | * published by the Free Software Foundation. |
mcimadamore@1072 | 8 | * |
mcimadamore@1072 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1072 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1072 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1072 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1072 | 13 | * accompanied this code). |
mcimadamore@1072 | 14 | * |
mcimadamore@1072 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1072 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1072 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1072 | 18 | * |
mcimadamore@1072 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1072 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1072 | 21 | * questions. |
mcimadamore@1072 | 22 | */ |
mcimadamore@1072 | 23 | |
mcimadamore@1072 | 24 | /* |
mcimadamore@1072 | 25 | * @test |
mcimadamore@1072 | 26 | * @bug 7079713 |
mcimadamore@1072 | 27 | * @summary javac hangs when compiling a class that references a cyclically inherited class |
mcimadamore@1072 | 28 | * @run main TestCircularClassfile |
mcimadamore@1072 | 29 | */ |
mcimadamore@1072 | 30 | |
mcimadamore@1072 | 31 | import java.io.*; |
mcimadamore@1072 | 32 | import java.net.URI; |
mcimadamore@1072 | 33 | import java.util.Arrays; |
mcimadamore@1072 | 34 | import javax.tools.JavaCompiler; |
mcimadamore@1072 | 35 | import javax.tools.JavaFileObject; |
mcimadamore@1072 | 36 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1072 | 37 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1072 | 38 | import javax.tools.StandardLocation; |
mcimadamore@1072 | 39 | import javax.tools.ToolProvider; |
mcimadamore@1072 | 40 | |
mcimadamore@1072 | 41 | import com.sun.source.util.JavacTask; |
mcimadamore@1072 | 42 | |
mcimadamore@1072 | 43 | public class TestCircularClassfile { |
mcimadamore@1072 | 44 | |
mcimadamore@1072 | 45 | enum SourceKind { |
mcimadamore@1072 | 46 | A_EXTENDS_B("class B {} class A extends B { void m() {} }"), |
mcimadamore@1072 | 47 | B_EXTENDS_A("class A { void m() {} } class B extends A {}"); |
mcimadamore@1072 | 48 | |
mcimadamore@1072 | 49 | String sourceStr; |
mcimadamore@1072 | 50 | |
mcimadamore@1072 | 51 | private SourceKind(String sourceStr) { |
mcimadamore@1072 | 52 | this.sourceStr = sourceStr; |
mcimadamore@1072 | 53 | } |
mcimadamore@1072 | 54 | |
mcimadamore@1072 | 55 | SimpleJavaFileObject getSource() { |
mcimadamore@1072 | 56 | return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) { |
mcimadamore@1072 | 57 | @Override |
mcimadamore@1072 | 58 | public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
mcimadamore@1072 | 59 | return sourceStr; |
mcimadamore@1072 | 60 | } |
mcimadamore@1072 | 61 | }; |
mcimadamore@1072 | 62 | } |
mcimadamore@1072 | 63 | } |
mcimadamore@1072 | 64 | |
mcimadamore@1072 | 65 | enum TestKind { |
mcimadamore@1072 | 66 | REPLACE_A("A.class"), |
mcimadamore@1072 | 67 | REPLACE_B("B.class"); |
mcimadamore@1072 | 68 | |
mcimadamore@1072 | 69 | String targetClass; |
mcimadamore@1072 | 70 | |
mcimadamore@1072 | 71 | private TestKind(String targetClass) { |
mcimadamore@1072 | 72 | this.targetClass = targetClass; |
mcimadamore@1072 | 73 | } |
mcimadamore@1072 | 74 | } |
mcimadamore@1072 | 75 | |
mcimadamore@1072 | 76 | enum ClientKind { |
mcimadamore@1072 | 77 | METHOD_CALL1("A a = null; a.m();"), |
mcimadamore@1072 | 78 | METHOD_CALL2("B b = null; b.m();"), |
mcimadamore@1072 | 79 | CONSTR_CALL1("new A();"), |
mcimadamore@1072 | 80 | CONSTR_CALL2("new B();"), |
mcimadamore@1072 | 81 | ASSIGN1("A a = null; B b = a;"), |
mcimadamore@1072 | 82 | ASSIGN2("B b = null; A a = b;"); |
mcimadamore@1072 | 83 | |
mcimadamore@1072 | 84 | String mainMethod; |
mcimadamore@1072 | 85 | |
mcimadamore@1072 | 86 | private ClientKind(String mainMethod) { |
mcimadamore@1072 | 87 | this.mainMethod = mainMethod; |
mcimadamore@1072 | 88 | } |
mcimadamore@1072 | 89 | |
mcimadamore@1072 | 90 | SimpleJavaFileObject getSource() { |
mcimadamore@1072 | 91 | return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) { |
mcimadamore@1072 | 92 | @Override |
mcimadamore@1072 | 93 | public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
mcimadamore@1072 | 94 | return "class Test { public static void main(String[] args) { #M } }" |
mcimadamore@1072 | 95 | .replace("#M", mainMethod); |
mcimadamore@1072 | 96 | } |
mcimadamore@1072 | 97 | }; |
mcimadamore@1072 | 98 | } |
mcimadamore@1072 | 99 | } |
mcimadamore@1072 | 100 | |
mcimadamore@1072 | 101 | public static void main(String... args) throws Exception { |
mcimadamore@1072 | 102 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1072 | 103 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1072 | 104 | int count = 0; |
mcimadamore@1072 | 105 | for (SourceKind sk1 : SourceKind.values()) { |
mcimadamore@1072 | 106 | for (SourceKind sk2 : SourceKind.values()) { |
mcimadamore@1072 | 107 | for (TestKind tk : TestKind.values()) { |
mcimadamore@1072 | 108 | for (ClientKind ck : ClientKind.values()) { |
mcimadamore@1072 | 109 | new TestCircularClassfile("sub_"+count++, sk1, sk2, tk, ck).check(comp, fm); |
mcimadamore@1072 | 110 | } |
mcimadamore@1072 | 111 | } |
mcimadamore@1072 | 112 | } |
mcimadamore@1072 | 113 | } |
mcimadamore@1072 | 114 | } |
mcimadamore@1072 | 115 | |
mcimadamore@1072 | 116 | static String workDir = System.getProperty("user.dir"); |
mcimadamore@1072 | 117 | |
mcimadamore@1072 | 118 | String destPath; |
mcimadamore@1072 | 119 | SourceKind sk1; |
mcimadamore@1072 | 120 | SourceKind sk2; |
mcimadamore@1072 | 121 | TestKind tk; |
mcimadamore@1072 | 122 | ClientKind ck; |
mcimadamore@1072 | 123 | |
mcimadamore@1072 | 124 | TestCircularClassfile(String destPath, SourceKind sk1, SourceKind sk2, TestKind tk, ClientKind ck) { |
mcimadamore@1072 | 125 | this.destPath = destPath; |
mcimadamore@1072 | 126 | this.sk1 = sk1; |
mcimadamore@1072 | 127 | this.sk2 = sk2; |
mcimadamore@1072 | 128 | this.tk = tk; |
mcimadamore@1072 | 129 | this.ck = ck; |
mcimadamore@1072 | 130 | } |
mcimadamore@1072 | 131 | |
mcimadamore@1072 | 132 | void check(JavaCompiler comp, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1072 | 133 | //step 1: compile first source code in the test subfolder |
mcimadamore@1072 | 134 | File destDir = new File(workDir, destPath); destDir.mkdir(); |
mcimadamore@1072 | 135 | //output dir must be set explicitly as we are sharing the fm (see bug 7026941) |
mcimadamore@1072 | 136 | fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir)); |
mcimadamore@1072 | 137 | JavacTask ct = (JavacTask)comp.getTask(null, fm, null, |
mcimadamore@1072 | 138 | null, null, Arrays.asList(sk1.getSource())); |
mcimadamore@1072 | 139 | ct.generate(); |
mcimadamore@1072 | 140 | |
mcimadamore@1072 | 141 | //step 2: compile second source code in a temp folder |
mcimadamore@1072 | 142 | File tmpDir = new File(destDir, "tmp"); tmpDir.mkdir(); |
mcimadamore@1072 | 143 | //output dir must be set explicitly as we are sharing the fm (see bug 7026941) |
mcimadamore@1072 | 144 | fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir)); |
mcimadamore@1072 | 145 | ct = (JavacTask)comp.getTask(null, fm, null, |
mcimadamore@1072 | 146 | null, null, Arrays.asList(sk2.getSource())); |
mcimadamore@1072 | 147 | ct.generate(); |
mcimadamore@1072 | 148 | |
mcimadamore@1072 | 149 | //step 3: move a classfile from the temp folder to the test subfolder |
mcimadamore@1072 | 150 | File fileToMove = new File(tmpDir, tk.targetClass); |
mcimadamore@1072 | 151 | File target = new File(destDir, tk.targetClass); |
jjh@1075 | 152 | target.delete(); |
mcimadamore@1072 | 153 | boolean success = fileToMove.renameTo(target); |
mcimadamore@1072 | 154 | |
mcimadamore@1072 | 155 | if (!success) { |
mcimadamore@1072 | 156 | throw new AssertionError("error when moving file " + tk.targetClass); |
mcimadamore@1072 | 157 | } |
mcimadamore@1072 | 158 | |
mcimadamore@1072 | 159 | //step 4: compile the client class against the classes in the test subfolder |
mcimadamore@1072 | 160 | //input/output dir must be set explicitly as we are sharing the fm (see bug 7026941) |
mcimadamore@1072 | 161 | fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir)); |
mcimadamore@1072 | 162 | fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(destDir)); |
mcimadamore@1072 | 163 | ct = (JavacTask)comp.getTask(null, fm, null, |
mcimadamore@1072 | 164 | null, null, Arrays.asList(ck.getSource())); |
mcimadamore@1072 | 165 | |
mcimadamore@1072 | 166 | ct.generate(); |
mcimadamore@1072 | 167 | } |
mcimadamore@1072 | 168 | } |