1 /* 2 * Copyright (c) 2016, 2018, 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 8081800 8010319 27 * @summary Redefine private and default interface methods 28 * @library /test/lib 29 * @modules java.base/jdk.internal.misc 30 * @modules java.compiler 31 * java.instrument 32 * jdk.jartool/sun.tools.jar 33 * @run main RedefineClassHelper 34 * @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class*=trace RedefineInterfaceMethods 35 */ 36 37 // package access top-level class to avoid problem with RedefineClassHelper 38 // and nested types. 39 40 interface RedefineInterfaceMethods_B { 41 int ORIGINAL_RETURN = 1; 42 int NEW_RETURN = 2; 43 private int privateMethod() { 44 return ORIGINAL_RETURN; 45 } 46 public default int defaultMethod() { 47 return privateMethod(); 48 } 49 } 50 51 public class RedefineInterfaceMethods { 52 53 static final int RET = -2; 54 55 public static String redefinedPrivateMethod = 56 "interface RedefineInterfaceMethods_B {" + 57 " int ORIGINAL_RETURN = 1;" + 58 " int NEW_RETURN = 2;" + 59 " private int privateMethod() {" + 60 " return NEW_RETURN;" + 61 " }" + 62 " public default int defaultMethod() {" + 63 " return privateMethod();" + 64 " }" + 65 "}"; 66 67 public static String redefinedDefaultMethod = 68 "interface RedefineInterfaceMethods_B {" + 69 " int ORIGINAL_RETURN = 1;" + 70 " int NEW_RETURN = 2;" + 71 " private int privateMethod() {" + 72 " return ORIGINAL_RETURN;" + 73 " }" + 74 " public default int defaultMethod() {" + 75 " return RedefineInterfaceMethods.RET;" + 76 " }" + 77 "}"; 78 79 static class Impl implements RedefineInterfaceMethods_B { 80 } 81 82 83 public static void main(String[] args) throws Exception { 84 85 Impl impl = new Impl(); 86 87 int res = impl.defaultMethod(); 88 if (res != RedefineInterfaceMethods_B.ORIGINAL_RETURN) 89 throw new Error("defaultMethod returned " + res + 90 " expected " + RedefineInterfaceMethods_B.ORIGINAL_RETURN); 91 92 RedefineClassHelper.redefineClass(RedefineInterfaceMethods_B.class, redefinedPrivateMethod); 93 94 res = impl.defaultMethod(); 95 if (res != RedefineInterfaceMethods_B.NEW_RETURN) 96 throw new Error("defaultMethod returned " + res + 97 " expected " + RedefineInterfaceMethods_B.NEW_RETURN); 98 99 System.gc(); 100 101 RedefineClassHelper.redefineClass(RedefineInterfaceMethods_B.class, redefinedDefaultMethod); 102 103 res = impl.defaultMethod(); 104 if (res != RET) 105 throw new Error("defaultMethod returned " + res + 106 " expected " + RET); 107 } 108 }