1 /*
 2  * Copyright (c) 2017, 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 8178870 8010319
27  * @summary Redefine class with CFLH twice to test deleting the cached_class_file
28  * @requires vm.jvmti
29  * @library /test/lib
30  * @modules java.base/jdk.internal.misc
31  * @modules java.compiler
32  *          java.instrument
33  *          jdk.jartool/sun.tools.jar
34  * @build jdk.test.lib.helpers.ClassFileInstaller jdk.test.lib.compiler.InMemoryJavaCompiler
35  * @run main RedefineClassHelper
36  * @run main/othervm/native -Xlog:redefine+class+load+exceptions -agentlib:RedefineDoubleDelete -javaagent:redefineagent.jar RedefineDoubleDelete
37  */
38 
39 // package access top-level class to avoid problem with RedefineClassHelper
40 // and nested types.
41 
42 // The ClassFileLoadHook for this class turns foo into faa and prints out faa.
43 class RedefineDoubleDelete_B {
44     int faa() { System.out.println("foo"); return 1; }
45 }
46 
47 public class RedefineDoubleDelete {
48 
49     // Class gets a redefinition error because it adds a data member
50     public static String newB =
51                 "class RedefineDoubleDelete_B {" +
52                 "   int count1 = 0;" +
53                 "}";
54 
55     public static String newerB =
56                 "class RedefineDoubleDelete_B { " +
57                 "   int faa() { System.out.println(\"baa\"); return 2; }" +
58                 "}";
59 
60     public static void main(String args[]) throws Exception {
61 
62         RedefineDoubleDelete_B b = new RedefineDoubleDelete_B();
63         int val = b.faa();
64         if (val != 1) {
65             throw new RuntimeException("return value wrong " + val);
66         }
67 
68         // Redefine B twice to get cached_class_file in both B scratch classes
69         try {
70             RedefineClassHelper.redefineClass(RedefineDoubleDelete_B.class, newB);
71         } catch (java.lang.UnsupportedOperationException e) {
72             // this is expected
73         }
74         try {
75             RedefineClassHelper.redefineClass(RedefineDoubleDelete_B.class, newB);
76         } catch (java.lang.UnsupportedOperationException e) {
77             // this is expected
78         }
79 
80         // Do a full GC.
81         System.gc();
82 
83         // Redefine with a compatible class
84         RedefineClassHelper.redefineClass(RedefineDoubleDelete_B.class, newerB);
85         val = b.faa();
86         if (val != 2) {
87             throw new RuntimeException("return value wrong " + val);
88         }
89 
90         // Do another full GC to clean things up.
91         System.gc();
92     }
93 }