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

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