672N/A/*
672N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
672N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
672N/A *
672N/A * This code is free software; you can redistribute it and/or modify it
672N/A * under the terms of the GNU General Public License version 2 only, as
672N/A * published by the Free Software Foundation.
672N/A *
672N/A * This code is distributed in the hope that it will be useful, but WITHOUT
672N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
672N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
672N/A * version 2 for more details (a copy is included in the LICENSE file that
672N/A * accompanied this code).
672N/A *
672N/A * You should have received a copy of the GNU General Public License version
672N/A * 2 along with this work; if not, write to the Free Software Foundation,
672N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
672N/A *
672N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
672N/A * or visit www.oracle.com if you need additional information or have any
672N/A * questions.
672N/A */
672N/A
672N/A/*
672N/A * @test
729N/A * @bug 6337171 6996415
729N/A * @ignore fix has been disabled as a consequence of 6996415
672N/A * @summary javac should create bridge methods when type variable bounds restricted
672N/A * @run main OverrideBridge
672N/A */
672N/A
672N/Aimport java.io.*;
672N/Aimport java.net.URI;
672N/Aimport java.util.ArrayList;
672N/Aimport java.util.Arrays;
672N/Aimport java.util.List;
672N/Aimport java.util.Map;
672N/Aimport java.util.HashMap;
672N/Aimport javax.tools.JavaCompiler;
672N/Aimport javax.tools.JavaFileObject;
672N/Aimport javax.tools.SimpleJavaFileObject;
672N/Aimport javax.tools.ToolProvider;
672N/A
672N/Aimport com.sun.source.util.JavacTask;
672N/Aimport com.sun.tools.classfile.ClassFile;
672N/Aimport com.sun.tools.classfile.ConstantPoolException;
672N/Aimport com.sun.tools.classfile.Descriptor.InvalidDescriptor;
672N/Aimport com.sun.tools.classfile.Method;
672N/A
672N/Apublic class OverrideBridge {
672N/A
672N/A enum Implementation {
672N/A IMPLICIT(""),
672N/A EXPLICIT("@Override public abstract X m(X x);");
672N/A
672N/A String impl;
672N/A
672N/A Implementation(String impl) {
672N/A this.impl = impl;
672N/A }
672N/A }
672N/A
672N/A static class JavaSource extends SimpleJavaFileObject {
672N/A
672N/A final static String sourceStub =
672N/A "abstract class A<X> {\n" +
672N/A " public abstract X m(X x);\n" +
672N/A "}\n" +
672N/A "interface I<X> {\n" +
672N/A "X m(X x);\n" +
672N/A "}\n" +
672N/A "abstract class B<X extends B<X>> extends A<X> implements I<X> { #B }\n" +
672N/A "abstract class C<X extends C<X>> extends B<X> { #C }\n" +
672N/A "abstract class D<X extends D<X>> extends C<X> { #D }\n";
672N/A
672N/A String source;
672N/A
672N/A public JavaSource(Implementation implB, Implementation implC, Implementation implD) {
672N/A super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
672N/A source = sourceStub.replace("#B", implB.impl).replace("#C", implC.impl).replace("#D", implD.impl);
672N/A }
672N/A
672N/A @Override
672N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
672N/A return source;
672N/A }
672N/A }
672N/A
672N/A public static void main(String... args) throws Exception {
672N/A Map<ClassFile, List<Method>> refMembers =
672N/A compile(Implementation.EXPLICIT, Implementation.EXPLICIT, Implementation.EXPLICIT, "ref");
672N/A int i = 0;
672N/A for (Implementation implB : Implementation.values()) {
672N/A for (Implementation implC : Implementation.values()) {
672N/A for (Implementation implD : Implementation.values()) {
672N/A Map<ClassFile, List<Method>> membersToCheck = compile(implB, implC, implD, "out_" + i++);
672N/A check(refMembers, membersToCheck);
672N/A }
672N/A }
672N/A }
672N/A }
672N/A
672N/A static String workDir = System.getProperty("user.dir");
672N/A
672N/A static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
672N/A File destDir = new File(workDir, destPath); destDir.mkdir();
672N/A final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
672N/A JavaSource source = new JavaSource(implB, implC, implD);
672N/A JavacTask ct = (JavacTask)tool.getTask(null, null, null,
672N/A Arrays.asList("-d", destPath), null, Arrays.asList(source));
672N/A ct.generate();
672N/A Map<ClassFile, List<Method>> members = new HashMap<>();
672N/A addMembers(destDir, members);
672N/A return members;
672N/A }
672N/A
672N/A static void addMembers(File destDir, Map<ClassFile, List<Method>> members) {
672N/A String[] names = { "B.class", "C.class", "D.class" };
672N/A try {
672N/A for (String name : names) {
672N/A File f = new File(destDir, name);
672N/A ClassFile cf = ClassFile.read(f);
672N/A members.put(cf, readMethod(cf, "m"));
672N/A }
672N/A } catch (Exception e) {
672N/A e.printStackTrace();
672N/A throw new Error("error reading classes");
672N/A }
672N/A }
672N/A
672N/A static List<Method> readMethod(ClassFile cf, String name) throws ConstantPoolException {
672N/A List<Method> buf = new ArrayList<>();
672N/A for (Method m : cf.methods) {
672N/A if (m.getName(cf.constant_pool).equals(name)) {
672N/A buf.add(m);
672N/A }
672N/A }
672N/A return buf;
672N/A }
672N/A
672N/A static void check(Map<ClassFile, List<Method>> refMembers, Map<ClassFile, List<Method>> membersToCheck) throws ConstantPoolException, InvalidDescriptor {
672N/A for (Map.Entry<ClassFile, List<Method>> ref : refMembers.entrySet()) {
672N/A ClassFile cRef = ref.getKey();
672N/A for (Method mRef : ref.getValue()) {
672N/A boolean ok = false;
672N/A for (Map.Entry<ClassFile, List<Method>> toCheck : membersToCheck.entrySet()) {
672N/A ClassFile cToCheck = toCheck.getKey();
672N/A for (Method mToCheck : toCheck.getValue()) {
672N/A if (cRef.getName().equals(cToCheck.getName()) &&
672N/A mRef.descriptor.getReturnType(cRef.constant_pool).equals(
672N/A mToCheck.descriptor.getReturnType(cToCheck.constant_pool)) &&
672N/A mRef.descriptor.getParameterTypes(cRef.constant_pool).equals(
672N/A mToCheck.descriptor.getParameterTypes(cToCheck.constant_pool))) {
672N/A ok = true;
672N/A }
672N/A }
672N/A }
672N/A if (!ok) {
672N/A throw new AssertionError("Matching method descriptor for " + mRef.descriptor.getParameterTypes(cRef.constant_pool) + "not found");
672N/A }
672N/A }
672N/A }
672N/A }
672N/A}