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.

ModuleClassPath.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.lang.reflect.Layer;
  19. import java.lang.reflect.Module;
  20. import java.net.URL;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.Set;
  24. /**
  25. * A search-path for obtaining a class file
  26. * by <code>getResourceAsStream()</code> in <code>java.lang.reflect.Module</code>.
  27. *
  28. * @see ClassPool#insertClassPath(ClassPath)
  29. * @see ClassPool#appendClassPath(ClassPath)
  30. * @see LoaderClassPath
  31. * @see ClassClassPath
  32. * @see 3.21
  33. */
  34. public class ModuleClassPath implements ClassPath {
  35. private HashMap packages = new HashMap();
  36. ModuleClassPath() { this(Layer.boot()); }
  37. /**
  38. * Constructs a search path.
  39. *
  40. * @param layer the layer used to obtain a class file.
  41. */
  42. public ModuleClassPath(Layer layer) {
  43. while (layer != null) {
  44. Set modules = layer.modules();
  45. Iterator it = modules.iterator();
  46. while (it.hasNext())
  47. addPackages((Module)it.next());
  48. layer = layer.parent().orElse(null);
  49. }
  50. }
  51. /**
  52. * Constructs a search path.
  53. *
  54. * @param m the module used to obtain a class file.
  55. */
  56. public ModuleClassPath(Module m) {
  57. addPackages(m);
  58. }
  59. private void addPackages(Module m) {
  60. String[] names = m.getPackages();
  61. for (int i = 0; i < names.length; i++)
  62. packages.put(names[i], m);
  63. }
  64. /**
  65. * Obtains a class file by <code>getResourceAsStream()</code>.
  66. */
  67. public InputStream openClassfile(String classname) throws NotFoundException {
  68. String filename = classname.replace('.', '/') + ".class";
  69. Module m = (Module)packages.get(getPackageName(classname));
  70. if (m == null)
  71. return null;
  72. else
  73. try {
  74. return m.getResourceAsStream(filename);
  75. }
  76. catch (java.io.IOException e) {
  77. throw new NotFoundException(classname, e);
  78. }
  79. }
  80. /**
  81. * Obtains the URL of the specified class file.
  82. *
  83. * @return null if the class file could not be found.
  84. */
  85. public URL find(String classname) {
  86. String filename = classname.replace('.', '/') + ".class";
  87. Module m = (Module)packages.get(getPackageName(classname));
  88. if (m == null)
  89. return null;
  90. else
  91. try {
  92. InputStream is = m.getResourceAsStream(filename);
  93. if (is == null)
  94. return null;
  95. else {
  96. is.close();
  97. return new URL("jar:file:unknown.jar!/" + filename);
  98. }
  99. }
  100. catch (java.io.IOException e) {
  101. return null;
  102. }
  103. }
  104. private static String getPackageName(String name) {
  105. int i = name.lastIndexOf('.');
  106. if (i > 0)
  107. return name.substring(0, i);
  108. else
  109. return "";
  110. }
  111. /**
  112. * Does nothing.
  113. */
  114. public void close() {}
  115. }