1 /*
   2  * Copyright (c) 2016, 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
  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 public class RedefineInterfaceMethods {
  37 
  38     static final int RET = -2;
  39 
  40     static interface 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 static String redefinedPrivateMethod =
  52         "interface RedefineInterfaceMethods$B {" +
  53         "    int ORIGINAL_RETURN = 1;" +
  54         "    int NEW_RETURN = 2;" +
  55         "    private int privateMethod() {" +
  56         "        return NEW_RETURN;" +
  57         "    }" +
  58         "    public default int defaultMethod() {" +
  59         "       return privateMethod();" +
  60         "    }" +
  61         "}";
  62 
  63     public static String redefinedDefaultMethod =
  64         "interface RedefineInterfaceMethods$B {" +
  65         "    int ORIGINAL_RETURN = 1;" +
  66         "    int NEW_RETURN = 2;" +
  67         "    private int privateMethod() {" +
  68         "        return ORIGINAL_RETURN;" +
  69         "    }" +
  70         "    public default int defaultMethod() {" +
  71         "       return RedefineInterfaceMethods.RET;" +
  72         "    }" +
  73         "}";
  74 
  75     static class Impl implements B {
  76     }
  77 
  78 
  79     public static void main(String[] args) throws Exception {
  80 
  81         Impl impl = new Impl();
  82 
  83         int res = impl.defaultMethod();
  84         if (res != B.ORIGINAL_RETURN)
  85             throw new Error("defaultMethod returned " + res +
  86                             " expected " + B.ORIGINAL_RETURN);
  87 
  88         RedefineClassHelper.redefineClass(B.class, redefinedPrivateMethod);
  89 
  90         res = impl.defaultMethod();
  91         if (res != B.NEW_RETURN)
  92             throw new Error("defaultMethod returned " + res +
  93                             " expected " + B.NEW_RETURN);
  94 
  95         System.gc();
  96 
  97         RedefineClassHelper.redefineClass(B.class, redefinedDefaultMethod);
  98 
  99         res = impl.defaultMethod();
 100         if (res != RET)
 101             throw new Error("defaultMethod returned " + res +
 102                             " expected " + RET);
 103     }
 104 }