Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/TestAddDeleteMethods.java
41153 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 819293626* @summary RI does not follow the JVMTI RedefineClasses spec; need to disallow adding and deleting methods27* @requires vm.jvmti28* @library /test/lib29* @modules java.base/jdk.internal.misc30* @modules java.compiler31* java.instrument32* jdk.jartool/sun.tools.jar33* @run main RedefineClassHelper34* @run main/othervm -javaagent:redefineagent.jar TestAddDeleteMethods AllowAddDelete=no35* @run main/othervm -javaagent:redefineagent.jar -XX:+AllowRedefinitionToAddDeleteMethods TestAddDeleteMethods AllowAddDelete=yes36*/3738import static jdk.test.lib.Asserts.assertEquals;39import java.lang.Runnable;4041// package access top-level class to avoid problem with RedefineClassHelper42// and nested types.43class A implements Runnable {44private void foo() { System.out.println(" OLD foo called"); }45public void publicFoo() { System.out.println(" OLD publicFoo called"); }46private final void finalFoo() { System.out.println(" OLD finalFoo called"); }47private static void staticFoo() { System.out.println(" OLD staticFoo called"); }48public void run() { foo(); publicFoo(); finalFoo(); staticFoo(); }49}5051class B implements Runnable {52public void run() { }53}5455public class TestAddDeleteMethods {56static private boolean allowAddDeleteMethods = false;5758static private A a;59static private B b;6061// This redefinition is expected to always succeed.62public static String newA =63"class A implements Runnable {" +64"private void foo() { System.out.println(\" NEW foo called\"); }" +65"public void publicFoo() { System.out.println(\" NEW publicFoo called\"); }" +66"private final void finalFoo() { System.out.println(\" NEW finalFoo called\"); }" +67"private static void staticFoo() { System.out.println(\" NEW staticFoo called\"); }" +68"public void run() { foo(); publicFoo(); finalFoo(); staticFoo(); }" +69"}";7071// This redefinition is expected to always fail.72public static String ADeleteFoo =73"class A implements Runnable {" +74"public void publicFoo() { System.out.println(\" NEW publicFoo called\"); }" +75"private final void finalFoo() { System.out.println(\" NEW finalFoo called\"); }" +76"private static void staticFoo() { System.out.println(\" NEW staticFoo called\"); }" +77"public void run() { publicFoo(); finalFoo(); staticFoo(); }" +78"}";7980// This redefinition is expected to always fail.81public static String ADeletePublicFoo =82"class A implements Runnable {" +83"private void foo() { System.out.println(\" NEW foo called\"); }" +84"private final void finalFoo() { System.out.println(\" NEW finalFoo called\"); }" +85"private static void staticFoo() { System.out.println(\" NEW staticFoo called\"); }" +86"public void run() { foo(); finalFoo(); staticFoo(); }" +87"}";8889// This redefinition is expected to succeed with option -XX:+AllowRedefinitionToAddDeleteMethods.90public static String ADeleteFinalFoo =91"class A implements Runnable {" +92"private void foo() { System.out.println(\" NEW foo called\"); }" +93"public void publicFoo() { System.out.println(\" NEW publicFoo called\"); }" +94"private static void staticFoo() { System.out.println(\" NEW staticFoo called\"); }" +95"public void run() { foo(); publicFoo(); staticFoo(); }" +96"}";9798// This redefinition is expected to succeed with option -XX:+AllowRedefinitionToAddDeleteMethods.99// With compatibility option redefinition ADeleteFinalFoo already deleted finalFoo method.100// So, this redefinition will add it back which is expected to work.101public static String ADeleteStaticFoo =102"class A implements Runnable {" +103"private void foo() { System.out.println(\" NEW foo called\"); }" +104"public void publicFoo() { System.out.println(\" NEW publicFoo called\"); }" +105"private final void finalFoo() { System.out.println(\" NEW finalFoo called\"); }" +106"public void run() { foo(); publicFoo(); finalFoo(); }" +107"}";108109// This redefinition is expected to always fail.110public static String BAddBar =111"class B implements Runnable {" +112"private void bar() { System.out.println(\" bar called\"); }" +113"public void run() { bar(); }" +114"}";115116// This redefinition is expected to always fail.117public static String BAddPublicBar =118"class B implements Runnable {" +119"public void publicBar() { System.out.println(\" publicBar called\"); }" +120"public void run() { publicBar(); }" +121"}";122123// This redefinition is expected to succeed with option -XX:+AllowRedefinitionToAddDeleteMethods.124public static String BAddFinalBar =125"class B implements Runnable {" +126"private final void finalBar() { System.out.println(\" finalBar called\"); }" +127"public void run() { finalBar(); }" +128"}";129130// This redefinition is expected to succeed with option -XX:+AllowRedefinitionToAddDeleteMethods.131// With compatibility option redefinition BAddFinalBar added finalBar method.132// So, this redefinition will deleate it back which is expected to work.133public static String BAddStaticBar =134"class B implements Runnable {" +135"private static void staticBar() { System.out.println(\" staticBar called\"); }" +136"public void run() { staticBar(); }" +137"}";138139static private final String ExpMsgPrefix = "attempted to ";140static private final String ExpMsgPostfix = " a method";141142static private void log(String msg) { System.out.println(msg); }143144public static void test(Runnable obj, String newBytes, String expSuffix, String methodName,145boolean expectedRedefToPass) throws Exception {146String expectedMessage = ExpMsgPrefix + expSuffix + ExpMsgPostfix;147Class klass = obj.getClass();148String className = klass.getName();149String expResult = expectedRedefToPass ? "PASS" : "FAIL";150151log("");152log("## Test " + expSuffix + " method \'" + methodName + "\' in class " + className +153"; redefinition expected to " + expResult);154155try {156RedefineClassHelper.redefineClass(klass, newBytes);157158if (expectedRedefToPass) {159log(" Did not get UOE at redefinition as expected");160} else {161throw new RuntimeException("Failed, expected UOE");162}163obj.run();164log("");165} catch (UnsupportedOperationException uoe) {166String message = uoe.getMessage();167168if (expectedRedefToPass) {169throw new RuntimeException("Failed, unexpected UOE: " + message);170} else {171log(" Got expected UOE: " + message);172if (!message.endsWith(expectedMessage)) {173throw new RuntimeException("Expected UOE error message to end with: " + expectedMessage);174}175}176}177}178179static {180a = new A();181b = new B();182}183184public static void main(String[] args) throws Exception {185if (args.length > 0 && args[0].equals("AllowAddDelete=yes")) {186allowAddDeleteMethods = true;187}188189log("## Test original class A");190a.run();191log("");192193log("## Test with modified method bodies in class A; redefinition expected to pass: true");194RedefineClassHelper.redefineClass(A.class, newA);195a.run();196197test(a, ADeleteFoo, "delete", "foo", false);198test(a, ADeletePublicFoo, "delete", "publicFoo", false);199test(a, ADeleteFinalFoo, "delete", "finalFoo", allowAddDeleteMethods);200test(a, ADeleteStaticFoo, "delete", "staticFoo", allowAddDeleteMethods);201202test(b, BAddBar, "add", "bar", false);203test(b, BAddPublicBar, "add", "publicBar", false);204test(b, BAddFinalBar, "add", "finalBar", allowAddDeleteMethods);205test(b, BAddStaticBar, "add", "staticBar", allowAddDeleteMethods);206}207}208209210