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.

ExtensibleURLClassLoader.java 3.3KB

14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Matthew Webster, Adrian Colyer,
  11. * Martin Lippert initial implementation
  12. * Andy Clement
  13. * Roy Varghese - Bug 473555
  14. * ******************************************************************/
  15. package org.aspectj.weaver.bcel;
  16. import java.io.IOException;
  17. import java.net.URL;
  18. import java.net.URLClassLoader;
  19. import java.security.CodeSource;
  20. import org.aspectj.util.FileUtil;
  21. import org.aspectj.weaver.BCException;
  22. import org.aspectj.weaver.UnresolvedType;
  23. public abstract class ExtensibleURLClassLoader extends URLClassLoader {
  24. private ClassPathManager classPath;
  25. public ExtensibleURLClassLoader(URL[] urls, ClassLoader parent) {
  26. super(urls, parent);
  27. // System.err.println("? ExtensibleURLClassLoader.<init>() path=" + WeavingAdaptor.makeClasspath(urls));
  28. try {
  29. classPath = new ClassPathManager(FileUtil.makeClasspath(urls), null);
  30. } catch (ExceptionInInitializerError ex) {
  31. ex.printStackTrace(System.out);
  32. throw ex;
  33. }
  34. }
  35. protected void addURL(URL url) {
  36. super.addURL(url); // amc - this call was missing and is needed in
  37. // WeavingURLClassLoader chains
  38. classPath.addPath(url.getPath(), null);
  39. }
  40. protected Class findClass(String name) throws ClassNotFoundException {
  41. // System.err.println("? ExtensibleURLClassLoader.findClass(" + name + ")");
  42. try {
  43. byte[] bytes = getBytes(name);
  44. if (bytes != null) {
  45. return defineClass(name, bytes);
  46. } else {
  47. throw new ClassNotFoundException(name);
  48. }
  49. } catch (IOException ex) {
  50. throw new ClassNotFoundException(name);
  51. }
  52. }
  53. protected Class defineClass(String name, byte[] b, CodeSource cs) throws IOException {
  54. // System.err.println("? ExtensibleURLClassLoader.defineClass(" + name + ",[" + b.length + "])");
  55. return defineClass(name, b, 0, b.length, cs);
  56. }
  57. protected byte[] getBytes(String name) throws IOException {
  58. byte[] b = null;
  59. UnresolvedType unresolvedType = null;
  60. try {
  61. unresolvedType = UnresolvedType.forName(name);
  62. } catch (BCException bce) {
  63. if (!bce.getMessage().contains("nameToSignature")) {
  64. bce.printStackTrace(System.err);
  65. }
  66. return null;
  67. }
  68. ClassPathManager.ClassFile classFile = classPath.find(unresolvedType);
  69. if (classFile != null) {
  70. try {
  71. b = FileUtil.readAsByteArray(classFile.getInputStream());
  72. } finally {
  73. classFile.close();
  74. }
  75. }
  76. return b;
  77. }
  78. private Class defineClass(String name, byte[] bytes /* ClassPathManager.ClassFile classFile */) throws IOException {
  79. String packageName = getPackageName(name);
  80. if (packageName != null) {
  81. Package pakkage = getPackage(packageName);
  82. if (pakkage == null) {
  83. definePackage(packageName, null, null, null, null, null, null, null);
  84. }
  85. }
  86. return defineClass(name, bytes, null);
  87. }
  88. private String getPackageName(String className) {
  89. int offset = className.lastIndexOf('.');
  90. return (offset == -1) ? null : className.substring(0, offset);
  91. }
  92. @Override
  93. public void close() throws IOException {
  94. super.close();
  95. classPath.closeArchives();
  96. }
  97. }