1 /* 2 * Copyright (c) 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 * @test 26 * @bug 8178870 27 * @summary Redefine class with CFLH twice to test deleting the cached_class_file 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/native -Xlog:redefine+class+load+exceptions -agentlib:RedefineDoubleDelete -javaagent:redefineagent.jar RedefineDoubleDelete 35 */ 36 37 public class RedefineDoubleDelete { 38 39 // Class gets a redefinition error because it adds a data member 40 public static String newB = 41 "class RedefineDoubleDelete$B {" + 42 " int count1 = 0;" + 43 "}"; 44 45 public static String newerB = 46 "class RedefineDoubleDelete$B { " + 47 " int faa() { System.out.println(\"baa\"); return 2; }" + 48 "}"; 49 50 // The ClassFileLoadHook for this class turns foo into faa and prints out faa. 51 static class B { 52 int faa() { System.out.println("foo"); return 1; } 53 } 54 55 public static void main(String args[]) throws Exception { 56 57 B b = new B(); 58 int val = b.faa(); 59 if (val != 1) { 60 throw new RuntimeException("return value wrong " + val); 61 } 62 63 // Redefine B twice to get cached_class_file in both B scratch classes 64 try { 65 RedefineClassHelper.redefineClass(B.class, newB); 66 } catch (java.lang.UnsupportedOperationException e) { 67 // this is expected 68 } 69 try { 70 RedefineClassHelper.redefineClass(B.class, newB); 71 } catch (java.lang.UnsupportedOperationException e) { 72 // this is expected 73 } 74 75 // Do a full GC. 76 System.gc(); 77 78 // Redefine with a compatible class 79 RedefineClassHelper.redefineClass(B.class, newerB); 80 val = b.faa(); 81 if (val != 2) { 82 throw new RuntimeException("return value wrong " + val); 83 } 84 85 // Do another full GC to clean things up. 86 System.gc(); 87 } 88 }