Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

FileClassLoader.java

00001 package edu.ksu.cis.bandera.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000  Roby Joehanes (robbyjo@cis.ksu.edu)           *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 
00036 // Adapted from magelang.com tutorial.
00037 
00038 import java.io.*;
00039 import java.net.*;
00040 import java.util.*;
00041 
00042 public class FileClassLoader extends ClassLoader {
00043 
00044     private String root;
00045     public static FileClassLoader v = new FileClassLoader();
00046 private FileClassLoader() {
00047      // root == null means that we're looking it through the java.class.path system
00048      // property
00049      root = null;
00050 }
00051 public FileClassLoader (String rootDir) {
00052      if (rootDir == null) throw new IllegalArgumentException ("Null root directory");
00053      root = rootDir;
00054 }
00055 public static Class load(String name) throws ClassNotFoundException
00056 {
00057      return v.loadClass(name);
00058 }
00059 protected Class loadClass (String name, boolean resolve) throws ClassNotFoundException {
00060 
00061      // Since all support classes of loaded class use same class loader
00062      // must check subclass cache of classes for things like Object
00063 
00064      // Class loaded yet?
00065      Class c = findLoadedClass (name);
00066      if (c == null) {
00067           try {
00068                c = findSystemClass (name);
00069           } catch (Exception e) {
00070                // Ignore these
00071           }
00072      }
00073 
00074      if (c == null) {
00075           // Convert class name argument to filename
00076           // Convert package names into subdirectories
00077           String filename = name.replace ('.', File.separatorChar) + ".class";
00078 
00079           try {
00080                // Load class data from file and save in byte array
00081                byte data[] = loadClassData(filename);
00082 
00083                // Convert byte array to Class
00084                c = defineClass (name, data, 0, data.length);
00085 
00086                // If failed, throw exception
00087                if (c == null) throw new ClassNotFoundException (name);
00088           } catch (IOException e) {
00089                throw new ClassNotFoundException ("Error reading file: " + filename);
00090           }
00091      }
00092 
00093      // Resolve class definition if approrpriate
00094      if (resolve) resolveClass (c);
00095 
00096      // Return class just created
00097      return c ;
00098 }
00099 private byte[] loadClassData (String filename) throws IOException {
00100      // Create a file object relative to directory provided
00101      File f = null;
00102 
00103      if (root == null)
00104      {
00105           // Fetch from the classpath
00106           String classPath = System.getProperty("java.class.path");
00107           StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
00108           boolean found = false;
00109 
00110           while (tok.hasMoreTokens())
00111           {
00112                f = new File(tok.nextToken(), filename);
00113                if (f.exists()) { found = true; break; }
00114           }
00115           if (!found) throw new IOException(filename+" cannot be found in class path: "+classPath);
00116      } else f = new File (root, filename);
00117 
00118      // Get size of class file
00119      int size = (int)f.length();
00120 
00121      // Reserve space to read
00122      byte buff[] = new byte[size];
00123 
00124      // Get stream to read from
00125      FileInputStream fis = new FileInputStream(f);
00126      DataInputStream dis = new DataInputStream (fis);
00127 
00128      // Read in data
00129      dis.readFully (buff);
00130 
00131      // close stream
00132      dis.close();
00133 
00134      // return data
00135      return buff;
00136 }
00137     public void setRootDirectory(String r)
00138     {
00139         if (r != null) root = r;
00140     }
00141 }

Generated at Thu Feb 7 06:45:43 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001