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