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 
     21 /**
     22  * A search-path for obtaining a class file
     23  * by <code>getResourceAsStream()</code> in <code>java.lang.Class</code>.
     24  *
     25  * <p>Try adding a <code>ClassClassPath</code> when a program is running
     26  * with a user-defined class loader and any class files are not found with
     27  * the default <code>ClassPool</code>.  For example,
     28  *
     29  * <ul><pre>
     30  * ClassPool cp = ClassPool.getDefault();
     31  * cp.insertClassPath(new ClassClassPath(this.getClass()));
     32  * </pre></ul>
     33  *
     34  * This code snippet permanently adds a <code>ClassClassPath</code>
     35  * to the default <code>ClassPool</code>.  Note that the default
     36  * <code>ClassPool</code> is a singleton.  The added
     37  * <code>ClassClassPath</code> uses a class object representing
     38  * the class including the code snippet above.
     39  *
     40  * @see ClassPool#insertClassPath(ClassPath)
     41  * @see ClassPool#appendClassPath(ClassPath)
     42  * @see LoaderClassPath
     43  */
     44 public class ClassClassPath implements ClassPath {
     45     private Class thisClass;
     46 
     47     /** Creates a search path.
     48      *
     49      * @param c     the <code>Class</code> object used to obtain a class
     50      *              file.  <code>getResourceAsStream()</code> is called on
     51      *              this object.
     52      */
     53     public ClassClassPath(Class c) {
     54         thisClass = c;
     55     }
     56 
     57     ClassClassPath() {
     58         /* The value of thisClass was this.getClass() in early versions:
     59          *
     60          *     thisClass = this.getClass();
     61          *
     62          * However, this made openClassfile() not search all the system
     63          * class paths if javassist.jar is put in jre/lib/ext/
     64          * (with JDK1.4).
     65          */
     66         this(java.lang.Object.class);
     67     }
     68 
     69     /**
     70      * Obtains a class file by <code>getResourceAsStream()</code>.
     71      */
     72     public InputStream openClassfile(String classname) {
     73         String jarname = "/" + classname.replace('.', '/') + ".class";
     74         return thisClass.getResourceAsStream(jarname);
     75     }
     76 
     77     /**
     78      * Obtains the URL of the specified class file.
     79      *
     80      * @return null if the class file could not be found.
     81      */
     82     public URL find(String classname) {
     83         String jarname = "/" + classname.replace('.', '/') + ".class";
     84         return thisClass.getResource(jarname);
     85     }
     86 
     87     /**
     88      * Does nothing.
     89      */
     90     public void close() {
     91     }
     92 
     93     public String toString() {
     94         return thisClass.getName() + ".class";
     95     }
     96 }
     97