test/java/lang/reflect/Method/InterfaceStatic/StaticInterfaceMethodInWayOfDefault.java

Print this page


   1 /*
   2  * Copyright (c) 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 8009411
  27  * @summary Test that a static method on an interface doesn't hide a default
  28  *          method with the same name and signature in a separate compilation
  29  *          scenario.



  30  */
  31 
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.lang.reflect.InvocationTargetException;
  35 import java.lang.reflect.Method;
  36 import java.util.concurrent.Callable;
  37 
  38 import sun.misc.IOUtils;
  39 
  40 public class StaticInterfaceMethodInWayOfDefault {
  41     public interface A_v1 {
  42     }
  43 
  44     public interface A_v2 {
  45         default void m() {
  46             System.err.println("A.m() called");
  47         }
  48     }
  49 
  50     public interface B  extends A_v1 {
  51         static void m() {
  52             System.err.println("B.m() called");
  53         }
  54     }
  55 
  56     public interface C_v1 extends B {
  57         default void m() {
  58             System.err.println("C.m() called");


 127                     if (resolve) {
 128                         resolveClass(c);
 129                     }
 130                     return c;
 131                 }
 132             }
 133             else { // not our class
 134                 return super.loadClass(name, resolve);
 135             }
 136         }
 137 
 138         @Override
 139         protected Class<?> findClass(String name)
 140             throws ClassNotFoundException {
 141             // special class name -> replace it with alternative name
 142             if (name.endsWith("_v1")) {
 143                 String altName = name.substring(0, name.length() - 3) + "_v2";
 144                 String altPath = altName.replace('.', '/').concat(".class");
 145                 try (InputStream is = getResourceAsStream(altPath)) {
 146                     if (is != null) {
 147                         byte[] bytes = IOUtils.readFully(is, -1, true);
 148                         // patch class bytes to contain original name
 149                         for (int i = 0; i < bytes.length - 2; i++) {
 150                             if (bytes[i] == '_' &&
 151                                 bytes[i + 1] == 'v' &&
 152                                 bytes[i + 2] == '2') {
 153                                 bytes[i + 2] = '1';
 154                             }
 155                         }
 156                         return defineClass(name, bytes, 0, bytes.length);
 157                     }
 158                     else {
 159                         throw new ClassNotFoundException(name);
 160                     }
 161                 }
 162                 catch (IOException e) {
 163                     throw new ClassNotFoundException(name, e);
 164                 }
 165             }
 166             else { // not special class name -> just load the class
 167                 String path = name.replace('.', '/').concat(".class");
 168                 try (InputStream is = getResourceAsStream(path)) {
 169                     if (is != null) {
 170                         byte[] bytes = IOUtils.readFully(is, -1, true);
 171                         return defineClass(name, bytes, 0, bytes.length);
 172                     }
 173                     else {
 174                         throw new ClassNotFoundException(name);
 175                     }
 176                 }
 177                 catch (IOException e) {
 178                     throw new ClassNotFoundException(name, e);
 179                 }
 180             }
 181         }
 182     }
 183 }
   1 /*
   2  * Copyright (c) 2013, 2014, 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 8009411
  27  * @summary Test that a static method on an interface doesn't hide a default
  28  *          method with the same name and signature in a separate compilation
  29  *          scenario.
  30  * @library /lib/testlibrary
  31  * @build jdk.testlibrary.IOUtils
  32  * @run main StaticInterfaceMethodInWayOfDefault
  33  */
  34 
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.lang.reflect.InvocationTargetException;
  38 import java.lang.reflect.Method;
  39 import java.util.concurrent.Callable;
  40 
  41 import jdk.testlibrary.IOUtils;
  42 
  43 public class StaticInterfaceMethodInWayOfDefault {
  44     public interface A_v1 {
  45     }
  46 
  47     public interface A_v2 {
  48         default void m() {
  49             System.err.println("A.m() called");
  50         }
  51     }
  52 
  53     public interface B  extends A_v1 {
  54         static void m() {
  55             System.err.println("B.m() called");
  56         }
  57     }
  58 
  59     public interface C_v1 extends B {
  60         default void m() {
  61             System.err.println("C.m() called");


 130                     if (resolve) {
 131                         resolveClass(c);
 132                     }
 133                     return c;
 134                 }
 135             }
 136             else { // not our class
 137                 return super.loadClass(name, resolve);
 138             }
 139         }
 140 
 141         @Override
 142         protected Class<?> findClass(String name)
 143             throws ClassNotFoundException {
 144             // special class name -> replace it with alternative name
 145             if (name.endsWith("_v1")) {
 146                 String altName = name.substring(0, name.length() - 3) + "_v2";
 147                 String altPath = altName.replace('.', '/').concat(".class");
 148                 try (InputStream is = getResourceAsStream(altPath)) {
 149                     if (is != null) {
 150                         byte[] bytes = IOUtils.readFully(is);
 151                         // patch class bytes to contain original name
 152                         for (int i = 0; i < bytes.length - 2; i++) {
 153                             if (bytes[i] == '_' &&
 154                                 bytes[i + 1] == 'v' &&
 155                                 bytes[i + 2] == '2') {
 156                                 bytes[i + 2] = '1';
 157                             }
 158                         }
 159                         return defineClass(name, bytes, 0, bytes.length);
 160                     }
 161                     else {
 162                         throw new ClassNotFoundException(name);
 163                     }
 164                 }
 165                 catch (IOException e) {
 166                     throw new ClassNotFoundException(name, e);
 167                 }
 168             }
 169             else { // not special class name -> just load the class
 170                 String path = name.replace('.', '/').concat(".class");
 171                 try (InputStream is = getResourceAsStream(path)) {
 172                     if (is != null) {
 173                         byte[] bytes = IOUtils.readFully(is);
 174                         return defineClass(name, bytes, 0, bytes.length);
 175                     }
 176                     else {
 177                         throw new ClassNotFoundException(name);
 178                     }
 179                 }
 180                 catch (IOException e) {
 181                     throw new ClassNotFoundException(name, e);
 182                 }
 183             }
 184         }
 185     }
 186 }