1 /*
   2  * Copyright (c) 2019, 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 8181171
  27  * @summary Test deleting static method pointing to by a jmethod
  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/native/othervm -javaagent:redefineagent.jar -Xlog:redefine+class*=trace RedefineDeleteJmethod
  35  */
  36 
  37 class B {
  38     private static int deleteMe() { System.out.println("deleteMe called"); return 5; }
  39     public static int callDeleteMe() { return deleteMe(); }
  40 }
  41 
  42 public class RedefineDeleteJmethod {
  43 
  44     public static String newB =
  45         "class B {" +
  46             "public static int callDeleteMe() { return 6; }" +
  47         "}";
  48 
  49     public static String newerB =
  50         "class B {" +
  51             "private static int deleteMe() { System.out.println(\"deleteMe (2) called\"); return 7; }" +
  52             "public static int callDeleteMe() { return deleteMe(); }" +
  53         "}";
  54 
  55 
  56     static {
  57         System.loadLibrary("RedefineDeleteJmethod");
  58     }
  59 
  60     static native int jniCallDeleteMe();
  61 
  62     static void test(int expected, boolean nsme_expected) throws Exception {
  63         // Call through static method
  64         int res = B.callDeleteMe();
  65         System.out.println("Result = " + res);
  66         if (res != expected) {
  67             throw new Error("returned " + res + " expected " + expected);
  68         }
  69 
  70         // Call through jmethodID, saved from first call.
  71         try {
  72             res = jniCallDeleteMe();
  73             if (nsme_expected) {
  74                 throw new RuntimeException("Failed, NoSuchMethodError expected");
  75             }
  76             if (res != expected) {
  77                 throw new Error("returned " + res + " expected " + expected);
  78             }
  79         } catch (NoSuchMethodError ex) {
  80             if (!nsme_expected) {
  81                 throw new RuntimeException("Failed, NoSuchMethodError not expected");
  82             }
  83             System.out.println("Passed, NoSuchMethodError expected");
  84         }
  85     }
  86 
  87     public static void main(String[] args) throws Exception {
  88         test(5, false);
  89         RedefineClassHelper.redefineClass(B.class, newB);
  90         test(6, true);
  91         RedefineClassHelper.redefineClass(B.class, newerB);
  92         test(7, true);
  93     }
  94 }