Home | History | Annotate | Download | only in javassist
      1 /*
      2  * Javassist, a Java-bytecode translator toolkit.
      3  * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
      4  *
      5  * The contents of this file are subject to the Mozilla Public License Version
      6  * 1.1 (the "License"); you may not use this file except in compliance with
      7  * the License.  Alternatively, the contents of this file may be used under
      8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
      9  *
     10  * Software distributed under the License is distributed on an "AS IS" basis,
     11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     12  * for the specific language governing rights and limitations under the
     13  * License.
     14  */
     15 
     16 package javassist;
     17 
     18 import java.io.InputStream;
     19 import java.net.URL;
     20 import java.lang.ref.WeakReference;
     21 
     22 /**
     23  * A class search-path representing a class loader.
     24  *
     25  * <p>It is used for obtaining a class file from the given
     26  * class loader by <code>getResourceAsStream()</code>.
     27  * The <code>LoaderClassPath</code> refers to the class loader through
     28  * <code>WeakReference</code>.  If the class loader is garbage collected,
     29  * the other search pathes are examined.
     30  *
     31  * <p>The given class loader must have both <code>getResourceAsStream()</code>
     32  * and <code>getResource()</code>.
     33  *
     34  * @author <a href="mailto:bill (at) jboss.org">Bill Burke</a>
     35  * @author Shigeru Chiba
     36  *
     37  * @see ClassPool#insertClassPath(ClassPath)
     38  * @see ClassPool#appendClassPath(ClassPath)
     39  * @see ClassClassPath
     40  */
     41 public class LoaderClassPath implements ClassPath {
     42     private WeakReference clref;
     43 
     44     /**
     45      * Creates a search path representing a class loader.
     46      */
     47     public LoaderClassPath(ClassLoader cl) {
     48         clref = new WeakReference(cl);
     49     }
     50 
     51     public String toString() {
     52         Object cl = null;
     53         if (clref != null)
     54             cl = clref.get();
     55 
     56         return cl == null ? "<null>" : cl.toString();
     57     }
     58 
     59     /**
     60      * Obtains a class file from the class loader.
     61      * This method calls <code>getResourceAsStream(String)</code>
     62      * on the class loader.
     63      */
     64     public InputStream openClassfile(String classname) {
     65         String cname = classname.replace('.', '/') + ".class";
     66         ClassLoader cl = (ClassLoader)clref.get();
     67         if (cl == null)
     68             return null;        // not found
     69         else
     70             return cl.getResourceAsStream(cname);
     71     }
     72 
     73     /**
     74      * Obtains the URL of the specified class file.
     75      * This method calls <code>getResource(String)</code>
     76      * on the class loader.
     77      *
     78      * @return null if the class file could not be found.
     79      */
     80     public URL find(String classname) {
     81         String cname = classname.replace('.', '/') + ".class";
     82         ClassLoader cl = (ClassLoader)clref.get();
     83         if (cl == null)
     84             return null;        // not found
     85         else
     86             return cl.getResource(cname);
     87     }
     88 
     89     /**
     90      * Closes this class path.
     91      */
     92     public void close() {
     93         clref = null;
     94     }
     95 }
     96