Wed, 22 Sep 2010 20:53:34 +0530
6587674: NoClassdefFound when anonymously extending a class.
Reviewed-by: jjg, mcimadamore
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
27 * @summary javac should create bridge methods when type variable bounds restricted
28 * @run main OverrideBridge
29 */
31 import java.io.*;
32 import java.net.URI;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.HashMap;
38 import javax.tools.JavaCompiler;
39 import javax.tools.JavaFileObject;
40 import javax.tools.SimpleJavaFileObject;
41 import javax.tools.ToolProvider;
43 import com.sun.source.util.JavacTask;
44 import com.sun.tools.classfile.ClassFile;
45 import com.sun.tools.classfile.ConstantPoolException;
46 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
47 import com.sun.tools.classfile.Method;
49 public class OverrideBridge {
51 enum Implementation {
52 IMPLICIT(""),
53 EXPLICIT("@Override public abstract X m(X x);");
55 String impl;
57 Implementation(String impl) {
58 this.impl = impl;
59 }
60 }
62 static class JavaSource extends SimpleJavaFileObject {
64 final static String sourceStub =
65 "abstract class A<X> {\n" +
66 " public abstract X m(X x);\n" +
67 "}\n" +
68 "interface I<X> {\n" +
69 "X m(X x);\n" +
70 "}\n" +
71 "abstract class B<X extends B<X>> extends A<X> implements I<X> { #B }\n" +
72 "abstract class C<X extends C<X>> extends B<X> { #C }\n" +
73 "abstract class D<X extends D<X>> extends C<X> { #D }\n";
75 String source;
77 public JavaSource(Implementation implB, Implementation implC, Implementation implD) {
78 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
79 source = sourceStub.replace("#B", implB.impl).replace("#C", implC.impl).replace("#D", implD.impl);
80 }
82 @Override
83 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
84 return source;
85 }
86 }
88 public static void main(String... args) throws Exception {
89 Map<ClassFile, List<Method>> refMembers =
90 compile(Implementation.EXPLICIT, Implementation.EXPLICIT, Implementation.EXPLICIT, "ref");
91 int i = 0;
92 for (Implementation implB : Implementation.values()) {
93 for (Implementation implC : Implementation.values()) {
94 for (Implementation implD : Implementation.values()) {
95 Map<ClassFile, List<Method>> membersToCheck = compile(implB, implC, implD, "out_" + i++);
96 check(refMembers, membersToCheck);
97 }
98 }
99 }
100 }
102 static String workDir = System.getProperty("user.dir");
104 static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
105 File destDir = new File(workDir, destPath); destDir.mkdir();
106 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
107 JavaSource source = new JavaSource(implB, implC, implD);
108 JavacTask ct = (JavacTask)tool.getTask(null, null, null,
109 Arrays.asList("-d", destPath), null, Arrays.asList(source));
110 ct.generate();
111 Map<ClassFile, List<Method>> members = new HashMap<>();
112 addMembers(destDir, members);
113 return members;
114 }
116 static void addMembers(File destDir, Map<ClassFile, List<Method>> members) {
117 String[] names = { "B.class", "C.class", "D.class" };
118 try {
119 for (String name : names) {
120 File f = new File(destDir, name);
121 ClassFile cf = ClassFile.read(f);
122 members.put(cf, readMethod(cf, "m"));
123 }
124 } catch (Exception e) {
125 e.printStackTrace();
126 throw new Error("error reading classes");
127 }
128 }
130 static List<Method> readMethod(ClassFile cf, String name) throws ConstantPoolException {
131 List<Method> buf = new ArrayList<>();
132 for (Method m : cf.methods) {
133 if (m.getName(cf.constant_pool).equals(name)) {
134 buf.add(m);
135 }
136 }
137 return buf;
138 }
140 static void check(Map<ClassFile, List<Method>> refMembers, Map<ClassFile, List<Method>> membersToCheck) throws ConstantPoolException, InvalidDescriptor {
141 for (Map.Entry<ClassFile, List<Method>> ref : refMembers.entrySet()) {
142 ClassFile cRef = ref.getKey();
143 for (Method mRef : ref.getValue()) {
144 boolean ok = false;
145 for (Map.Entry<ClassFile, List<Method>> toCheck : membersToCheck.entrySet()) {
146 ClassFile cToCheck = toCheck.getKey();
147 for (Method mToCheck : toCheck.getValue()) {
148 if (cRef.getName().equals(cToCheck.getName()) &&
149 mRef.descriptor.getReturnType(cRef.constant_pool).equals(
150 mToCheck.descriptor.getReturnType(cToCheck.constant_pool)) &&
151 mRef.descriptor.getParameterTypes(cRef.constant_pool).equals(
152 mToCheck.descriptor.getParameterTypes(cToCheck.constant_pool))) {
153 ok = true;
154 }
155 }
156 }
157 if (!ok) {
158 throw new AssertionError("Matching method descriptor for " + mRef.descriptor.getParameterTypes(cRef.constant_pool) + "not found");
159 }
160 }
161 }
162 }
163 }