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.

UtilClassLoader.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.util;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.net.URL;
  19. import java.net.URLClassLoader;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import java.util.List;
  24. /**
  25. * Load classes as File from File[] dirs or URL[] jars.
  26. */
  27. public class UtilClassLoader extends URLClassLoader {
  28. /** seek classes in dirs first */
  29. List<File> dirs;
  30. /** save URL[] only for toString */
  31. private URL[] urlsForDebugString;
  32. public UtilClassLoader(URL[] urls, File[] dirs) {
  33. super(urls);
  34. LangUtil.throwIaxIfNotAssignable(dirs, File.class, "dirs");
  35. this.urlsForDebugString = urls;
  36. List<File> dcopy = new ArrayList<>();
  37. if (!LangUtil.isEmpty(dirs)) {
  38. dcopy.addAll(Arrays.asList(dirs));
  39. }
  40. this.dirs = Collections.unmodifiableList(dcopy);
  41. }
  42. public URL getResource(String name) {
  43. return ClassLoader.getSystemResource(name);
  44. }
  45. public InputStream getResourceAsStream(String name) {
  46. return ClassLoader.getSystemResourceAsStream(name);
  47. }
  48. public synchronized Class<?> loadClass(String name, boolean resolve)
  49. throws ClassNotFoundException {
  50. // search the cache, our dirs (if maybe test),
  51. // the system, the superclass (URL[]),
  52. // and our dirs again (if not maybe test)
  53. ClassNotFoundException thrown = null;
  54. Class<?> result = findLoadedClass(name);
  55. if (null != result) {
  56. resolve = false;
  57. } else {
  58. try {
  59. result = findSystemClass(name);
  60. } catch (ClassNotFoundException e) {
  61. thrown = e;
  62. }
  63. }
  64. if (null == result) {
  65. try {
  66. result = super.loadClass(name, resolve);
  67. } catch (ClassNotFoundException e) {
  68. thrown = e;
  69. }
  70. if (null != result) { // resolved by superclass
  71. return result;
  72. }
  73. }
  74. if (null == result) {
  75. byte[] data = readClass(name);
  76. if (data != null) {
  77. result = defineClass(name, data, 0, data.length);
  78. } // handle ClassFormatError?
  79. }
  80. if (null == result) {
  81. throw (null != thrown ? thrown : new ClassNotFoundException(name));
  82. }
  83. if (resolve) {
  84. resolveClass(result);
  85. }
  86. return result;
  87. }
  88. /** @return null if class not found or byte[] of class otherwise */
  89. private byte[] readClass(String className) throws ClassNotFoundException {
  90. final String fileName = className.replace('.', '/')+".class";
  91. for (File dir : dirs) {
  92. File file = new File(dir, fileName);
  93. if (file.canRead()) {
  94. return getClassData(file);
  95. }
  96. }
  97. return null;
  98. }
  99. private byte[] getClassData(File f) {
  100. try {
  101. FileInputStream stream= new FileInputStream(f);
  102. ByteArrayOutputStream out= new ByteArrayOutputStream(1000);
  103. byte[] b= new byte[4096];
  104. int n;
  105. while ((n= stream.read(b)) != -1) {
  106. out.write(b, 0, n);
  107. }
  108. stream.close();
  109. out.close();
  110. return out.toByteArray();
  111. } catch (IOException e) {
  112. }
  113. return null;
  114. }
  115. /** @return String with debug info: urls and classes used */
  116. public String toString() {
  117. return "UtilClassLoader(urls="
  118. + Arrays.asList(urlsForDebugString)
  119. + ", dirs="
  120. + dirs
  121. + ")";
  122. }
  123. }