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.

WeavingURLClassLoader.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Matthew Webster, Adrian Colyer,
  11. * Martin Lippert initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.weaver.loadtime;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18. import java.security.CodeSource;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.StringTokenizer;
  24. import org.aspectj.weaver.ExtensibleURLClassLoader;
  25. import org.aspectj.weaver.tools.WeavingAdaptor;
  26. import org.aspectj.weaver.tools.WeavingClassLoader;
  27. public class WeavingURLClassLoader extends ExtensibleURLClassLoader implements WeavingClassLoader {
  28. public static final String WEAVING_CLASS_PATH = "aj.class.path";
  29. public static final String WEAVING_ASPECT_PATH = "aj.aspect.path";
  30. private URL[] aspectURLs;
  31. private WeavingAdaptor adaptor;
  32. private boolean initializingAdaptor;
  33. private Map generatedClasses = new HashMap(); /* String -> byte[] */
  34. /*
  35. * This constructor is needed when using "-Djava.system.class.loader".
  36. */
  37. public WeavingURLClassLoader (ClassLoader parent) {
  38. this(getURLs(getClassPath()),getURLs(getAspectPath()),parent);
  39. // System.err.println("? WeavingURLClassLoader.<init>(" + m_parent + ")");
  40. }
  41. public WeavingURLClassLoader (URL[] urls, ClassLoader parent) {
  42. super(urls,parent);
  43. // System.out.println("WeavingURLClassLoader.WeavingURLClassLoader()");
  44. }
  45. public WeavingURLClassLoader (URL[] classURLs, URL[] aspectURLs, ClassLoader parent) {
  46. super(classURLs,parent);
  47. // System.err.println("? WeavingURLClassLoader.<init>() classURLs=" + classURLs.length + ", aspectURLs=" + aspectURLs.length);
  48. this.aspectURLs = aspectURLs;
  49. /* If either we nor our m_parent is using an ASPECT_PATH use a new-style
  50. * adaptor
  51. */
  52. if (this.aspectURLs.length > 0 || parent instanceof WeavingClassLoader) {
  53. adaptor = new WeavingAdaptor(this);
  54. }
  55. }
  56. private static String getAspectPath () {
  57. return System.getProperty(WEAVING_ASPECT_PATH,"");
  58. }
  59. private static String getClassPath () {
  60. return System.getProperty(WEAVING_CLASS_PATH,"");
  61. }
  62. private static URL[] getURLs (String path) {
  63. List urlList = new ArrayList();
  64. for (StringTokenizer t = new StringTokenizer(path,File.pathSeparator);
  65. t.hasMoreTokens();) {
  66. File f = new File(t.nextToken().trim());
  67. try {
  68. if (f.exists()) {
  69. URL url = f.toURL();
  70. if (url != null) urlList.add(url);
  71. }
  72. } catch (MalformedURLException e) {}
  73. }
  74. URL[] urls = new URL[urlList.size()];
  75. urlList.toArray(urls);
  76. return urls;
  77. }
  78. protected void addURL(URL url) {
  79. adaptor.addURL(url);
  80. super.addURL(url);
  81. }
  82. /**
  83. * Override to weave class using WeavingAdaptor
  84. */
  85. protected Class defineClass(String name, byte[] b, CodeSource cs) throws IOException {
  86. // System.err.println("? WeavingURLClassLoader.defineClass(" + name + ", [" + b.length + "])");
  87. /* Avoid recursion during adaptor initialization */
  88. if (!initializingAdaptor) {
  89. /* Need to defer creation because of possible recursion during constructor execution */
  90. if (adaptor == null && !initializingAdaptor) {
  91. DefaultWeavingContext weavingContext = new DefaultWeavingContext (this) {
  92. /* Ensures consistent LTW messages for testing */
  93. public String getClassLoaderName() {
  94. return loader.getClass().getName();
  95. }
  96. };
  97. ClassLoaderWeavingAdaptor clwAdaptor = new ClassLoaderWeavingAdaptor(this,weavingContext);
  98. initializingAdaptor = true;
  99. clwAdaptor.initialize(this,weavingContext);
  100. initializingAdaptor = false;
  101. adaptor = clwAdaptor;
  102. }
  103. b = adaptor.weaveClass(name,b);
  104. }
  105. return super.defineClass(name, b, cs);
  106. }
  107. /**
  108. * Override to find classes generated by WeavingAdaptor
  109. */
  110. protected byte[] getBytes (String name) throws IOException {
  111. byte[] bytes = super.getBytes(name);
  112. if (bytes == null) {
  113. // return adaptor.findClass(name);
  114. return (byte[])generatedClasses.remove(name);
  115. }
  116. return bytes;
  117. }
  118. /**
  119. * Implement method from WeavingClassLoader
  120. */
  121. public URL[] getAspectURLs() {
  122. return aspectURLs;
  123. }
  124. public void acceptClass (String name, byte[] bytes) {
  125. generatedClasses.put(name,bytes);
  126. }
  127. // private interface ClassPreProcessorAdaptor extends ClassPreProcessor {
  128. // public void addURL(URL url);
  129. // }
  130. //
  131. // private class WeavingAdaptorPreProcessor implements ClassPreProcessorAdaptor {
  132. //
  133. // private WeavingAdaptor adaptor;
  134. //
  135. // public WeavingAdaptorPreProcessor (WeavingClassLoader wcl) {
  136. // adaptor = new WeavingAdaptor(wcl);
  137. // }
  138. //
  139. // public void initialize() {
  140. // }
  141. //
  142. // public byte[] preProcess(String className, byte[] bytes, ClassLoader classLoader) {
  143. // return adaptor.weaveClass(className,bytes);
  144. // }
  145. //
  146. // public void addURL(URL url) {
  147. //
  148. // }
  149. // }
  150. }