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