1 /* 2 * Copyright (c) 2016, 2017, 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 import sun.hotspot.WhiteBox; 26 27 public class RedefineBasic { 28 29 public static String newB = 30 " class RedefineBasic$B { " + 31 " public static void okToCallBeforeRedefine() { " + 32 " throw new RuntimeException(\"newB: okToCallBeforeRedefine is " + 33 " called after redefinition, test failed\"); }" + 34 " public static void okToCallAfterRedefine() { " + 35 " System.out.println(\"newB: okToCallAfterRedefine\"); } " + 36 " } "; 37 38 39 static class B { 40 public static void okToCallBeforeRedefine() { 41 System.out.println("okToCallBeforeRedefine"); 42 } 43 public static void okToCallAfterRedefine() { 44 throw new RuntimeException( 45 "okToCallAfterRedefine is called before redefinition, test failed"); 46 } 47 } 48 49 static class SubclassOfB extends B { 50 public static void testAfterRedefine() { 51 B.okToCallAfterRedefine(); 52 } 53 } 54 55 class Subclass2OfB extends B { 56 public void testAfterRedefine() { 57 super.okToCallAfterRedefine(); 58 } 59 } 60 61 // verify that a given class is shared, report error if necessary 62 public static void 63 verifyClassIsShared(WhiteBox wb, Class c) throws Exception { 64 if (!wb.isSharedClass(c)) { 65 throw new RuntimeException( 66 "This class should be shared but isn't: " + c.getName()); 67 } else { 68 System.out.println("The class is shared as expected: " + 69 c.getName()); 70 } 71 } 72 73 public static void main(String[] args) throws Exception { 74 WhiteBox wb = WhiteBox.getWhiteBox(); 75 76 verifyClassIsShared(wb, RedefineBasic.class); 77 verifyClassIsShared(wb, B.class); 78 verifyClassIsShared(wb, SubclassOfB.class); 79 verifyClassIsShared(wb, Subclass2OfB.class); 80 81 // (1) Test case: verify that original B works as expected 82 // and that redefined B is shared and works as expected, 83 // with new behavior 84 B.okToCallBeforeRedefine(); 85 RedefineClassHelper.redefineClass(B.class, newB); 86 verifyClassIsShared(wb, B.class); 87 B.okToCallAfterRedefine(); 88 89 // Static subclass of the super: 90 // 1. Make sure it is still shared 91 // 2. and it calls the correct super (the redefined one) 92 verifyClassIsShared(wb, SubclassOfB.class); 93 SubclassOfB.testAfterRedefine(); 94 95 // Same as above, but for non-static class 96 verifyClassIsShared(wb, Subclass2OfB.class); 97 RedefineBasic thisTest = new RedefineBasic(); 98 thisTest.testSubclass2OfB(); 99 } 100 101 public void testSubclass2OfB() { 102 Subclass2OfB sub = new Subclass2OfB(); 103 sub.testAfterRedefine(); 104 } 105 }