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 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * ******************************************************************/
  13. package org.aspectj.weaver;
  14. import java.io.IOException;
  15. import java.net.URL;
  16. import java.net.URLClassLoader;
  17. import java.security.CodeSource;
  18. import org.aspectj.util.FileUtil;
  19. import org.aspectj.weaver.bcel.ClassPathManager;
  20. public abstract class ExtensibleURLClassLoader extends URLClassLoader {
  21. private ClassPathManager classPath;
  22. public ExtensibleURLClassLoader (URL[] urls, ClassLoader parent) {
  23. super(urls,parent);
  24. // System.err.println("? ExtensibleURLClassLoader.<init>() path=" + WeavingAdaptor.makeClasspath(urls));
  25. try {
  26. classPath = new ClassPathManager(FileUtil.makeClasspath(urls),null);
  27. }
  28. catch (ExceptionInInitializerError ex) {
  29. ex.printStackTrace(System.out);
  30. throw ex;
  31. }
  32. }
  33. protected void addURL(URL url) {
  34. super.addURL(url); // amc - this call was missing and is needed in
  35. // WeavingURLClassLoader chains
  36. classPath.addPath(url.getPath(),null);
  37. }
  38. protected Class findClass(String name) throws ClassNotFoundException {
  39. // System.err.println("? ExtensibleURLClassLoader.findClass(" + name + ")");
  40. try {
  41. byte[] bytes = getBytes(name);
  42. if (bytes != null) {
  43. return defineClass(name,bytes);
  44. }
  45. else {
  46. throw new ClassNotFoundException(name);
  47. }
  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. ClassPathManager.ClassFile classFile = classPath.find(UnresolvedType.forName(name));
  60. if (classFile != null) {
  61. b = FileUtil.readAsByteArray(classFile.getInputStream());
  62. }
  63. return b;
  64. }
  65. private Class defineClass(String name, byte[] bytes /*ClassPathManager.ClassFile classFile*/) throws IOException {
  66. String packageName = getPackageName(name);
  67. if (packageName != null) {
  68. Package pakkage = getPackage(packageName);
  69. if (pakkage == null) {
  70. definePackage(packageName,null,null,null,null,null,null,null);
  71. }
  72. }
  73. return defineClass(name, bytes, null);
  74. }
  75. private String getPackageName (String className) {
  76. int offset = className.lastIndexOf('.');
  77. return (offset == -1)? null : className.substring(0,offset);
  78. }
  79. }