Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LoaderClassPath.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. */
  15. package javassist;
  16. import java.io.InputStream;
  17. import java.lang.ref.WeakReference;
  18. /**
  19. * A class search-path representing a class loader.
  20. *
  21. * <p>It is used for obtaining a class file from the given
  22. * class loader by <code>getResourceAsStream()</code>.
  23. * The <code>LoaderClassPath</code> refers to the class loader through
  24. * <code>WeakReference</code>. If the class loader is garbage collected,
  25. * the other search pathes are examined.
  26. *
  27. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  28. * @author Shigeru Chiba
  29. *
  30. * @see javassist.ClassPath
  31. * @see ClassPool#insertClassPath(ClassPath)
  32. * @see ClassPool#appendClassPath(ClassPath)
  33. */
  34. public class LoaderClassPath implements ClassPath {
  35. private WeakReference clref;
  36. /**
  37. * Creates a search path representing a class loader.
  38. */
  39. public LoaderClassPath(ClassLoader cl) {
  40. clref = new WeakReference(cl);
  41. }
  42. public String toString() {
  43. Object cl = null;
  44. if (clref != null)
  45. cl = clref.get();
  46. return cl == null ? "<null>" : cl.toString();
  47. }
  48. /**
  49. * Obtains a class file from the class loader.
  50. */
  51. public InputStream openClassfile(String classname) {
  52. String cname = classname.replace('.', '/') + ".class";
  53. ClassLoader cl = (ClassLoader)clref.get();
  54. if (cl == null)
  55. return null; // not found
  56. else
  57. return cl.getResourceAsStream(cname);
  58. }
  59. /**
  60. * Closes this class path.
  61. */
  62. public void close() {
  63. clref = null;
  64. }
  65. }