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.

ClassClassPath.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /**
  19. * A search-path for obtaining a class file
  20. * by <code>getResourceAsStream()</code> in <code>java.lang.Class</code>.
  21. *
  22. * <p>Try adding a <code>ClassClassPath</code> when a program is running
  23. * with a user-defined class loader and any class files are not found with
  24. * the default <code>ClassPool</code>. For example,
  25. *
  26. * <ul><pre>
  27. * ClassPool cp = ClassPool.getDefault();
  28. * cp.insertClassPath(new ClassClassPath(this.getClass()));
  29. * </pre></ul>
  30. *
  31. * This code snippet permanently adds a <code>ClassClassPath</code>
  32. * to the default <code>ClassPool</code>. Note that the default
  33. * <code>ClassPool</code> is a singleton. The added
  34. * <code>ClassClassPath</code> uses a class object representing
  35. * the class including the code snippet above.
  36. *
  37. * @see ClassPool#insertClassPath(ClassPath)
  38. * @see ClassPool#appendClassPath(ClassPath)
  39. * @see LoaderClassPath
  40. */
  41. public class ClassClassPath implements ClassPath {
  42. private Class thisClass;
  43. /** Creates a search path.
  44. *
  45. * @param c the <code>Class</code> object used to obtain a class
  46. * file. <code>getResourceAsStream()</code> is called on
  47. * this object.
  48. */
  49. public ClassClassPath(Class c) {
  50. thisClass = c;
  51. }
  52. ClassClassPath() {
  53. /* The value of thisClass was this.getClass() in early versions:
  54. *
  55. * thisClass = this.getClass();
  56. *
  57. * However, this made openClassfile() not search all the system
  58. * class paths if javassist.jar is put in jre/lib/ext/
  59. * (with JDK1.4).
  60. */
  61. this(java.lang.Object.class);
  62. }
  63. /**
  64. * Obtains a class file by <code>getResourceAsStream()</code>.
  65. */
  66. public InputStream openClassfile(String classname) {
  67. String jarname = "/" + classname.replace('.', '/') + ".class";
  68. return thisClass.getResourceAsStream(jarname);
  69. }
  70. /**
  71. * Obtains the URL of the specified class file.
  72. *
  73. * @return null if the class file could not be found.
  74. */
  75. public URL find(String classname) {
  76. String jarname = "/" + classname.replace('.', '/') + ".class";
  77. return thisClass.getResource(jarname);
  78. }
  79. /**
  80. * Does nothing.
  81. */
  82. public void close() {
  83. }
  84. public String toString() {
  85. return thisClass.getName() + ".class";
  86. }
  87. }