123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /*
- * Javassist, a Java-bytecode translator toolkit.
- * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- */
- package javassist;
-
- import java.io.InputStream;
- import java.lang.ref.WeakReference;
-
- /**
- * A class search-path representing a class loader.
- *
- * <p>It is used for obtaining a class file from the given
- * class loader by <code>getResourceAsStream()</code>.
- * The <code>LoaderClassPath</code> refers to the class loader through
- * <code>WeakReference</code>. If the class loader is garbage collected,
- * the other search pathes are examined.
- *
- * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
- * @author Shigeru Chiba
- *
- * @see javassist.ClassPath
- * @see ClassPool#insertClassPath(ClassPath)
- * @see ClassPool#appendClassPath(ClassPath)
- */
- public class LoaderClassPath implements ClassPath {
- private WeakReference clref;
-
- /**
- * Creates a search path representing a class loader.
- */
- public LoaderClassPath(ClassLoader cl) {
- clref = new WeakReference(cl);
- }
-
- public String toString() {
- Object cl = null;
- if (clref != null)
- cl = clref.get();
-
- return cl == null ? "<null>" : cl.toString();
- }
-
- /**
- * Obtains a class file from the class loader.
- */
- public InputStream openClassfile(String classname) {
- String cname = classname.replace('.', '/') + ".class";
- ClassLoader cl = (ClassLoader)clref.get();
- if (cl == null)
- return null; // not found
- else
- return cl.getResourceAsStream(cname);
- }
-
- /**
- * Closes this class path.
- */
- public void close() {
- clref = null;
- }
- }
|