1 /*
   2  * Copyright (c) 2020, 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 class Parent {
  25     int get() {return 1;}
  26 }
  27 
  28 class Child extends Parent {
  29     int get() {return 2;}
  30     int dummy() {
  31         // When Child is linked during dynamic dump (when the VM is shutting
  32         // down), it will be verified, which will recursively cause Child2/Parent2
  33         // to be loaded and verified.
  34         // We want to make sure that this is done at a point in the VM lifecycle where
  35         // it's still safe to do so.
  36         Parent2 x = new Child2();
  37         return x.get();
  38     }
  39 }
  40 
  41 class Parent2 {
  42     int get() {return 3;}
  43 }
  44 
  45 class Child2 extends Parent2 {
  46     int get() {return 4;}
  47 }
  48 
  49 class MyShutdown extends Thread{
  50     public void run(){
  51         System.out.println("shut down hook invoked...");
  52     }
  53 }
  54 
  55 class LinkClassApp {
  56     public static void main(String args[]) {
  57         Runtime r=Runtime.getRuntime();
  58         r.addShutdownHook(new MyShutdown());
  59 
  60         if (args.length > 0 && args[0].equals("run")) {
  61             System.out.println("test() = " + test());
  62         } else {
  63             // Executed during dynamic dumping.
  64             System.out.println("Test.class is initialized.");
  65             System.out.println("Parent.class and Child.class are loaded when Test.class is verified,");
  66             System.out.println("but these two classes are not linked");
  67         }
  68 
  69         if (args.length > 0 && args[0].equals("callExit")) {
  70             System.exit(0);
  71         }
  72     }
  73 
  74     static int test() {
  75         // Verification of Test.test() would load Child and Parent, and create a verification constraint that
  76         // Child must be a subtype of Parent.
  77         //
  78         // Child and Parent are not linked until Test.test() is actually executed.
  79         Parent x = new Child();
  80         return x.get();
  81     }
  82 }