test/tools/javac/generics/OverrideBridge.java

Fri, 03 May 2013 09:56:56 -0700

author
jjg
date
Fri, 03 May 2013 09:56:56 -0700
changeset 1721
abd153854f16
parent 730
20659c8c917d
permissions
-rw-r--r--

8012728: Normalize @ignore comments on langtools tests
Reviewed-by: vromero, mcimadamore

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

mercurial