Mon, 28 Feb 2011 13:42:24 -0800
7022711: compiler crash in try-with-resources
Reviewed-by: mcimadamore
mcimadamore@673 | 1 | /* |
mcimadamore@673 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@673 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@673 | 4 | * |
mcimadamore@673 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@673 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@673 | 7 | * published by the Free Software Foundation. |
mcimadamore@673 | 8 | * |
mcimadamore@673 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@673 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@673 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@673 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@673 | 13 | * accompanied this code). |
mcimadamore@673 | 14 | * |
mcimadamore@673 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@673 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@673 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@673 | 18 | * |
mcimadamore@673 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@673 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@673 | 21 | * questions. |
mcimadamore@673 | 22 | */ |
mcimadamore@673 | 23 | |
mcimadamore@673 | 24 | /* |
mcimadamore@673 | 25 | * @test |
mcimadamore@730 | 26 | * @bug 6337171 6996415 |
mcimadamore@730 | 27 | * @ignore fix has been disabled as a consequence of 6996415 |
mcimadamore@673 | 28 | * @summary javac should create bridge methods when type variable bounds restricted |
mcimadamore@673 | 29 | * @run main OverrideBridge |
mcimadamore@673 | 30 | */ |
mcimadamore@673 | 31 | |
mcimadamore@673 | 32 | import java.io.*; |
mcimadamore@673 | 33 | import java.net.URI; |
mcimadamore@673 | 34 | import java.util.ArrayList; |
mcimadamore@673 | 35 | import java.util.Arrays; |
mcimadamore@673 | 36 | import java.util.List; |
mcimadamore@673 | 37 | import java.util.Map; |
mcimadamore@673 | 38 | import java.util.HashMap; |
mcimadamore@673 | 39 | import javax.tools.JavaCompiler; |
mcimadamore@673 | 40 | import javax.tools.JavaFileObject; |
mcimadamore@673 | 41 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@673 | 42 | import javax.tools.ToolProvider; |
mcimadamore@673 | 43 | |
mcimadamore@673 | 44 | import com.sun.source.util.JavacTask; |
mcimadamore@673 | 45 | import com.sun.tools.classfile.ClassFile; |
mcimadamore@673 | 46 | import com.sun.tools.classfile.ConstantPoolException; |
mcimadamore@673 | 47 | import com.sun.tools.classfile.Descriptor.InvalidDescriptor; |
mcimadamore@673 | 48 | import com.sun.tools.classfile.Method; |
mcimadamore@673 | 49 | |
mcimadamore@673 | 50 | public class OverrideBridge { |
mcimadamore@673 | 51 | |
mcimadamore@673 | 52 | enum Implementation { |
mcimadamore@673 | 53 | IMPLICIT(""), |
mcimadamore@673 | 54 | EXPLICIT("@Override public abstract X m(X x);"); |
mcimadamore@673 | 55 | |
mcimadamore@673 | 56 | String impl; |
mcimadamore@673 | 57 | |
mcimadamore@673 | 58 | Implementation(String impl) { |
mcimadamore@673 | 59 | this.impl = impl; |
mcimadamore@673 | 60 | } |
mcimadamore@673 | 61 | } |
mcimadamore@673 | 62 | |
mcimadamore@673 | 63 | static class JavaSource extends SimpleJavaFileObject { |
mcimadamore@673 | 64 | |
mcimadamore@673 | 65 | final static String sourceStub = |
mcimadamore@673 | 66 | "abstract class A<X> {\n" + |
mcimadamore@673 | 67 | " public abstract X m(X x);\n" + |
mcimadamore@673 | 68 | "}\n" + |
mcimadamore@673 | 69 | "interface I<X> {\n" + |
mcimadamore@673 | 70 | "X m(X x);\n" + |
mcimadamore@673 | 71 | "}\n" + |
mcimadamore@673 | 72 | "abstract class B<X extends B<X>> extends A<X> implements I<X> { #B }\n" + |
mcimadamore@673 | 73 | "abstract class C<X extends C<X>> extends B<X> { #C }\n" + |
mcimadamore@673 | 74 | "abstract class D<X extends D<X>> extends C<X> { #D }\n"; |
mcimadamore@673 | 75 | |
mcimadamore@673 | 76 | String source; |
mcimadamore@673 | 77 | |
mcimadamore@673 | 78 | public JavaSource(Implementation implB, Implementation implC, Implementation implD) { |
mcimadamore@673 | 79 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@673 | 80 | source = sourceStub.replace("#B", implB.impl).replace("#C", implC.impl).replace("#D", implD.impl); |
mcimadamore@673 | 81 | } |
mcimadamore@673 | 82 | |
mcimadamore@673 | 83 | @Override |
mcimadamore@673 | 84 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@673 | 85 | return source; |
mcimadamore@673 | 86 | } |
mcimadamore@673 | 87 | } |
mcimadamore@673 | 88 | |
mcimadamore@673 | 89 | public static void main(String... args) throws Exception { |
mcimadamore@673 | 90 | Map<ClassFile, List<Method>> refMembers = |
mcimadamore@673 | 91 | compile(Implementation.EXPLICIT, Implementation.EXPLICIT, Implementation.EXPLICIT, "ref"); |
mcimadamore@673 | 92 | int i = 0; |
mcimadamore@673 | 93 | for (Implementation implB : Implementation.values()) { |
mcimadamore@673 | 94 | for (Implementation implC : Implementation.values()) { |
mcimadamore@673 | 95 | for (Implementation implD : Implementation.values()) { |
mcimadamore@673 | 96 | Map<ClassFile, List<Method>> membersToCheck = compile(implB, implC, implD, "out_" + i++); |
mcimadamore@673 | 97 | check(refMembers, membersToCheck); |
mcimadamore@673 | 98 | } |
mcimadamore@673 | 99 | } |
mcimadamore@673 | 100 | } |
mcimadamore@673 | 101 | } |
mcimadamore@673 | 102 | |
mcimadamore@673 | 103 | static String workDir = System.getProperty("user.dir"); |
mcimadamore@673 | 104 | |
mcimadamore@673 | 105 | static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception { |
mcimadamore@673 | 106 | File destDir = new File(workDir, destPath); destDir.mkdir(); |
mcimadamore@673 | 107 | final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@673 | 108 | JavaSource source = new JavaSource(implB, implC, implD); |
mcimadamore@673 | 109 | JavacTask ct = (JavacTask)tool.getTask(null, null, null, |
mcimadamore@673 | 110 | Arrays.asList("-d", destPath), null, Arrays.asList(source)); |
mcimadamore@673 | 111 | ct.generate(); |
mcimadamore@673 | 112 | Map<ClassFile, List<Method>> members = new HashMap<>(); |
mcimadamore@673 | 113 | addMembers(destDir, members); |
mcimadamore@673 | 114 | return members; |
mcimadamore@673 | 115 | } |
mcimadamore@673 | 116 | |
mcimadamore@673 | 117 | static void addMembers(File destDir, Map<ClassFile, List<Method>> members) { |
mcimadamore@673 | 118 | String[] names = { "B.class", "C.class", "D.class" }; |
mcimadamore@673 | 119 | try { |
mcimadamore@673 | 120 | for (String name : names) { |
mcimadamore@673 | 121 | File f = new File(destDir, name); |
mcimadamore@673 | 122 | ClassFile cf = ClassFile.read(f); |
mcimadamore@673 | 123 | members.put(cf, readMethod(cf, "m")); |
mcimadamore@673 | 124 | } |
mcimadamore@673 | 125 | } catch (Exception e) { |
mcimadamore@673 | 126 | e.printStackTrace(); |
mcimadamore@673 | 127 | throw new Error("error reading classes"); |
mcimadamore@673 | 128 | } |
mcimadamore@673 | 129 | } |
mcimadamore@673 | 130 | |
mcimadamore@673 | 131 | static List<Method> readMethod(ClassFile cf, String name) throws ConstantPoolException { |
mcimadamore@673 | 132 | List<Method> buf = new ArrayList<>(); |
mcimadamore@673 | 133 | for (Method m : cf.methods) { |
mcimadamore@673 | 134 | if (m.getName(cf.constant_pool).equals(name)) { |
mcimadamore@673 | 135 | buf.add(m); |
mcimadamore@673 | 136 | } |
mcimadamore@673 | 137 | } |
mcimadamore@673 | 138 | return buf; |
mcimadamore@673 | 139 | } |
mcimadamore@673 | 140 | |
mcimadamore@673 | 141 | static void check(Map<ClassFile, List<Method>> refMembers, Map<ClassFile, List<Method>> membersToCheck) throws ConstantPoolException, InvalidDescriptor { |
mcimadamore@673 | 142 | for (Map.Entry<ClassFile, List<Method>> ref : refMembers.entrySet()) { |
mcimadamore@673 | 143 | ClassFile cRef = ref.getKey(); |
mcimadamore@673 | 144 | for (Method mRef : ref.getValue()) { |
mcimadamore@673 | 145 | boolean ok = false; |
mcimadamore@673 | 146 | for (Map.Entry<ClassFile, List<Method>> toCheck : membersToCheck.entrySet()) { |
mcimadamore@673 | 147 | ClassFile cToCheck = toCheck.getKey(); |
mcimadamore@673 | 148 | for (Method mToCheck : toCheck.getValue()) { |
mcimadamore@673 | 149 | if (cRef.getName().equals(cToCheck.getName()) && |
mcimadamore@673 | 150 | mRef.descriptor.getReturnType(cRef.constant_pool).equals( |
mcimadamore@673 | 151 | mToCheck.descriptor.getReturnType(cToCheck.constant_pool)) && |
mcimadamore@673 | 152 | mRef.descriptor.getParameterTypes(cRef.constant_pool).equals( |
mcimadamore@673 | 153 | mToCheck.descriptor.getParameterTypes(cToCheck.constant_pool))) { |
mcimadamore@673 | 154 | ok = true; |
mcimadamore@673 | 155 | } |
mcimadamore@673 | 156 | } |
mcimadamore@673 | 157 | } |
mcimadamore@673 | 158 | if (!ok) { |
mcimadamore@673 | 159 | throw new AssertionError("Matching method descriptor for " + mRef.descriptor.getParameterTypes(cRef.constant_pool) + "not found"); |
mcimadamore@673 | 160 | } |
mcimadamore@673 | 161 | } |
mcimadamore@673 | 162 | } |
mcimadamore@673 | 163 | } |
mcimadamore@673 | 164 | } |