You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoaderClassPath.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. package javassist;
  16. import java.io.InputStream;
  17. import java.net.URL;
  18. import java.lang.ref.WeakReference;
  19. /**
  20. * A class search-path representing a class loader.
  21. *
  22. * <p>It is used for obtaining a class file from the given
  23. * class loader by <code>getResourceAsStream()</code>.
  24. * The <code>LoaderClassPath</code> refers to the class loader through
  25. * <code>WeakReference</code>. If the class loader is garbage collected,
  26. * the other search pathes are examined.
  27. *
  28. * <p>The given class loader must have both <code>getResourceAsStream()</code>
  29. * and <code>getResource()</code>.
  30. *
  31. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  32. * @author Shigeru Chiba
  33. *
  34. * @see ClassPool#insertClassPath(ClassPath)
  35. * @see ClassPool#appendClassPath(ClassPath)
  36. * @see ClassClassPath
  37. */
  38. public class LoaderClassPath implements ClassPath {
  39. private WeakReference clref;
  40. /**
  41. * Creates a search path representing a class loader.
  42. */
  43. public LoaderClassPath(ClassLoader cl) {
  44. clref = new WeakReference(cl);
  45. }
  46. public String toString() {
  47. Object cl = null;
  48. if (clref != null)
  49. cl = clref.get();
  50. return cl == null ? "<null>" : cl.toString();
  51. }
  52. /**
  53. * Obtains a class file from the class loader.
  54. * This method calls <code>getResourceAsStream(String)</code>
  55. * on the class loader.
  56. */
  57. public InputStream openClassfile(String classname) {
  58. String cname = classname.replace('.', '/') + ".class";
  59. ClassLoader cl = (ClassLoader)clref.get();
  60. if (cl == null)
  61. return null; // not found
  62. else
  63. return cl.getResourceAsStream(cname);
  64. }
  65. /**
  66. * Obtains the URL of the specified class file.
  67. * This method calls <code>getResource(String)</code>
  68. * on the class loader.
  69. *
  70. * @return null if the class file could not be found.
  71. */
  72. public URL find(String classname) {
  73. String cname = classname.replace('.', '/') + ".class";
  74. ClassLoader cl = (ClassLoader)clref.get();
  75. if (cl == null)
  76. return null; // not found
  77. else
  78. return cl.getResource(cname);
  79. }
  80. /**
  81. * Closes this class path.
  82. */
  83. public void close() {
  84. clref = null;
  85. }
  86. }