Mon, 17 Oct 2011 12:57:36 +0100
7093325: Redundant entry in bytecode exception table
Summary: Inlining of finalizers does not update gaps list accordingly
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2010, 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 */
24 /*
25 * @test
26 * @bug 6337171 6996415
27 * @ignore fix has been disabled as a consequence of 6996415
28 * @summary javac should create bridge methods when type variable bounds restricted
29 * @run main OverrideBridge
30 */
32 import java.io.*;
33 import java.net.URI;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.HashMap;
39 import javax.tools.JavaCompiler;
40 import javax.tools.JavaFileObject;
41 import javax.tools.SimpleJavaFileObject;
42 import javax.tools.ToolProvider;
44 import com.sun.source.util.JavacTask;
45 import com.sun.tools.classfile.ClassFile;
46 import com.sun.tools.classfile.ConstantPoolException;
47 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
48 import com.sun.tools.classfile.Method;
50 public class OverrideBridge {
52 enum Implementation {
53 IMPLICIT(""),
54 EXPLICIT("@Override public abstract X m(X x);");
56 String impl;
58 Implementation(String impl) {
59 this.impl = impl;
60 }
61 }
63 static class JavaSource extends SimpleJavaFileObject {
65 final static String sourceStub =
66 "abstract class A<X> {\n" +
67 " public abstract X m(X x);\n" +
68 "}\n" +
69 "interface I<X> {\n" +
70 "X m(X x);\n" +
71 "}\n" +
72 "abstract class B<X extends B<X>> extends A<X> implements I<X> { #B }\n" +
73 "abstract class C<X extends C<X>> extends B<X> { #C }\n" +
74 "abstract class D<X extends D<X>> extends C<X> { #D }\n";
76 String source;
78 public JavaSource(Implementation implB, Implementation implC, Implementation implD) {
79 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
80 source = sourceStub.replace("#B", implB.impl).replace("#C", implC.impl).replace("#D", implD.impl);
81 }
83 @Override
84 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
85 return source;
86 }
87 }
89 public static void main(String... args) throws Exception {
90 Map<ClassFile, List<Method>> refMembers =
91 compile(Implementation.EXPLICIT, Implementation.EXPLICIT, Implementation.EXPLICIT, "ref");
92 int i = 0;
93 for (Implementation implB : Implementation.values()) {
94 for (Implementation implC : Implementation.values()) {
95 for (Implementation implD : Implementation.values()) {
96 Map<ClassFile, List<Method>> membersToCheck = compile(implB, implC, implD, "out_" + i++);
97 check(refMembers, membersToCheck);
98 }
99 }
100 }
101 }
103 static String workDir = System.getProperty("user.dir");
105 static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
106 File destDir = new File(workDir, destPath); destDir.mkdir();
107 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
108 JavaSource source = new JavaSource(implB, implC, implD);
109 JavacTask ct = (JavacTask)tool.getTask(null, null, null,
110 Arrays.asList("-d", destPath), null, Arrays.asList(source));
111 ct.generate();
112 Map<ClassFile, List<Method>> members = new HashMap<>();
113 addMembers(destDir, members);
114 return members;
115 }
117 static void addMembers(File destDir, Map<ClassFile, List<Method>> members) {
118 String[] names = { "B.class", "C.class", "D.class" };
119 try {
120 for (String name : names) {
121 File f = new File(destDir, name);
122 ClassFile cf = ClassFile.read(f);
123 members.put(cf, readMethod(cf, "m"));
124 }
125 } catch (Exception e) {
126 e.printStackTrace();
127 throw new Error("error reading classes");
128 }
129 }
131 static List<Method> readMethod(ClassFile cf, String name) throws ConstantPoolException {
132 List<Method> buf = new ArrayList<>();
133 for (Method m : cf.methods) {
134 if (m.getName(cf.constant_pool).equals(name)) {
135 buf.add(m);
136 }
137 }
138 return buf;
139 }
141 static void check(Map<ClassFile, List<Method>> refMembers, Map<ClassFile, List<Method>> membersToCheck) throws ConstantPoolException, InvalidDescriptor {
142 for (Map.Entry<ClassFile, List<Method>> ref : refMembers.entrySet()) {
143 ClassFile cRef = ref.getKey();
144 for (Method mRef : ref.getValue()) {
145 boolean ok = false;
146 for (Map.Entry<ClassFile, List<Method>> toCheck : membersToCheck.entrySet()) {
147 ClassFile cToCheck = toCheck.getKey();
148 for (Method mToCheck : toCheck.getValue()) {
149 if (cRef.getName().equals(cToCheck.getName()) &&
150 mRef.descriptor.getReturnType(cRef.constant_pool).equals(
151 mToCheck.descriptor.getReturnType(cToCheck.constant_pool)) &&
152 mRef.descriptor.getParameterTypes(cRef.constant_pool).equals(
153 mToCheck.descriptor.getParameterTypes(cToCheck.constant_pool))) {
154 ok = true;
155 }
156 }
157 }
158 if (!ok) {
159 throw new AssertionError("Matching method descriptor for " + mRef.descriptor.getParameterTypes(cRef.constant_pool) + "not found");
160 }
161 }
162 }
163 }
164 }