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 3.0KB

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