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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * <pre>
  28. * ClassPool cp = ClassPool.getDefault();
  29. * cp.insertClassPath(new ClassClassPath(this.getClass()));
  30. * </pre>
  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. * <p>Class files in a named module are private to that module.
  39. * This method cannot obtain class files in named modules.
  40. * </p>
  41. *
  42. * @see ClassPool#insertClassPath(ClassPath)
  43. * @see ClassPool#appendClassPath(ClassPath)
  44. * @see LoaderClassPath
  45. */
  46. public class ClassClassPath implements ClassPath {
  47. private Class<?> thisClass;
  48. /** Creates a search path.
  49. *
  50. * @param c the <code>Class</code> object used to obtain a class
  51. * file. <code>getResourceAsStream()</code> is called on
  52. * this object.
  53. */
  54. public ClassClassPath(Class<?> c) {
  55. thisClass = c;
  56. }
  57. ClassClassPath() {
  58. /* The value of thisClass was this.getClass() in early versions:
  59. *
  60. * thisClass = this.getClass();
  61. *
  62. * However, this made openClassfile() not search all the system
  63. * class paths if javassist.jar is put in jre/lib/ext/
  64. * (with JDK1.4).
  65. */
  66. this(java.lang.Object.class);
  67. }
  68. /**
  69. * Obtains a class file by <code>getResourceAsStream()</code>.
  70. */
  71. @Override
  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. @Override
  82. public URL find(String classname) {
  83. String filename = '/' + classname.replace('.', '/') + ".class";
  84. return thisClass.getResource(filename);
  85. }
  86. @Override
  87. public String toString() {
  88. return thisClass.getName() + ".class";
  89. }
  90. }