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

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