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