1 /*
   2  * Copyright (c) 2010, 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 import java.io.File;
  26 import java.io.ByteArrayInputStream;
  27 import java.io.FileInputStream;
  28 
  29 public class LE2_Loader extends ClassLoader {
  30 
  31     private static String classPath;
  32     static {
  33         classPath = System.getProperty("test.classes");
  34         if (classPath == null) {
  35             classPath = "";
  36         } else {
  37             classPath = classPath + File.separator;
  38         }
  39     }
  40 
  41     // Print routine to generate indented output that
  42     // reflects recursive actions of the classloader.
  43     int _recur = 0;
  44     public void print(String msg) {
  45         for (int i = 0; i < _recur; i++) {
  46             System.out.print("  ");
  47         }
  48         System.out.println(">>LE2_Loader>> " + msg);
  49     }
  50 
  51     protected Class findClass2(String name) throws ClassNotFoundException {
  52         print("Fetching the implementation of " + name);
  53         int old = _recur;
  54         String filename = classPath + name.replace(".", "/") + ".impl2";
  55         try {
  56 
  57             FileInputStream fi = new FileInputStream(filename);
  58             byte result[] = new byte[fi.available()];
  59             fi.read(result);
  60 
  61             print("call defineClass() on " + name);
  62             _recur++;
  63             // This will throw the expected LinkageError when loading the
  64             // second version of class B.
  65             Class clazz = defineClass(name, result, 0, result.length);
  66             _recur = old;
  67             print("Returning newly loaded class.");
  68             return clazz;
  69         } catch (Exception e) {
  70             _recur = old;
  71             print("Not found on disk: " + filename);
  72             // If we caught an exception, either the class was not found or
  73             // it was unreadable by our process.
  74             return null;
  75         }
  76     }
  77 
  78     protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException  {
  79         // Attempt a disk load first.
  80         Class c = findClass2(name);
  81         if (c == null) {
  82             // Check if the class has already been loaded.
  83             print("Checking for prior loaded class " + name);
  84             c = findLoadedClass(name);
  85             print("Letting super-loader load " + name);
  86             int old = _recur;
  87             _recur++;
  88             c = super.loadClass(name, false);
  89             _recur = old;
  90         }
  91         if (resolve) { print("Resolving class " + name); resolveClass(c); }
  92         print("Returning class " + c.getClassLoader() + ":" + name);
  93         return c;
  94     }
  95 }