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.ByteArrayInputStream;
  26 import java.io.FileInputStream;
  27 public class Loader2 extends ClassLoader {
  28   int _recur;
  29   public void print( String msg ) {
  30     for( int i=0; i<_recur; i++ )
  31       System.out.print("  ");
  32     System.out.println(">>Loader2>> "+msg);
  33   }
  34 
  35   protected Class findClass2(String name) throws ClassNotFoundException {
  36     print("Fetching the implementation of "+name);
  37     int old = _recur;
  38     try {
  39       FileInputStream fi = new FileInputStream(name+".class");
  40       byte result[] = new byte[fi.available()];
  41       fi.read(result);
  42 
  43       print("DefineClass1 on "+name);
  44       _recur++;
  45       Class clazz = defineClass(name, result, 0, result.length);
  46       _recur = old;
  47       print("Returning newly loaded class.");
  48       return clazz;
  49     } catch (Exception e) {
  50       _recur = old;
  51       print("Not found on disk.");
  52       // If we caught an exception, either the class was not found or
  53       // it was unreadable by our process.
  54       return null;
  55       //throw new ClassNotFoundException(e.toString());
  56     }
  57   }
  58 
  59   protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException  {
  60     // Attempt a disk load first
  61     Class c = findClass2(name);
  62     if( c == null ) {
  63       // check if the class has already been loaded
  64       print("Checking for prior loaded class "+name);
  65       c = findLoadedClass(name);
  66       print("Letting super-loader load "+name);
  67       int old = _recur;
  68       _recur++;
  69       c = super.loadClass(name, false);
  70       _recur=old;
  71     }
  72     if (resolve) { print("Resolving class "+name); resolveClass(c); }
  73     print("Returning clazz "+c.getClassLoader()+":"+name);
  74     return c;
  75   }
  76 }