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.1KB

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