• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999- 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  * or the Apache License Version 2.0.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  */
16 
17 package javassist;
18 
19 import java.io.InputStream;
20 import java.lang.ref.Reference;
21 import java.lang.ref.WeakReference;
22 import java.net.URL;
23 
24 /**
25  * A class search-path representing a class loader.
26  *
27  * <p>It is used for obtaining a class file from the given
28  * class loader by <code>getResourceAsStream()</code>.
29  * The <code>LoaderClassPath</code> refers to the class loader through
30  * <code>WeakReference</code>.  If the class loader is garbage collected,
31  * the other search pathes are examined.
32  *
33  * <p>The given class loader must have both <code>getResourceAsStream()</code>
34  * and <code>getResource()</code>.
35  *
36  * <p>Class files in a named module are private to that module.
37  * This method cannot obtain class files in named modules.
38  * </p>
39  *
40  * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
41  * @author Shigeru Chiba
42  *
43  * @see ClassPool#insertClassPath(ClassPath)
44  * @see ClassPool#appendClassPath(ClassPath)
45  * @see ClassClassPath
46  */
47 public class LoaderClassPath implements ClassPath {
48     private Reference<ClassLoader> clref;
49 
50     /**
51      * Creates a search path representing a class loader.
52      */
LoaderClassPath(ClassLoader cl)53     public LoaderClassPath(ClassLoader cl) {
54         clref = new WeakReference<ClassLoader>(cl);
55     }
56 
57     @Override
toString()58     public String toString() {
59         return clref.get() == null ? "<null>" : clref.get().toString();
60     }
61 
62     /**
63      * Obtains a class file from the class loader.
64      * This method calls <code>getResourceAsStream(String)</code>
65      * on the class loader.
66      */
67     @Override
openClassfile(String classname)68     public InputStream openClassfile(String classname) throws NotFoundException {
69         String cname = classname.replace('.', '/') + ".class";
70         ClassLoader cl = clref.get();
71         if (cl == null)
72             return null;        // not found
73         InputStream is = cl.getResourceAsStream(cname);
74         return is;
75     }
76 
77     /**
78      * Obtains the URL of the specified class file.
79      * This method calls <code>getResource(String)</code>
80      * on the class loader.
81      *
82      * @return null if the class file could not be found.
83      */
84     @Override
find(String classname)85     public URL find(String classname) {
86         String cname = classname.replace('.', '/') + ".class";
87         ClassLoader cl = clref.get();
88         if (cl == null)
89             return null;        // not found
90         URL url = cl.getResource(cname);
91         return url;
92     }
93 }
94