diff options
author | aclement <aclement> | 2005-10-14 07:23:40 +0000 |
---|---|---|
committer | aclement <aclement> | 2005-10-14 07:23:40 +0000 |
commit | 8982544f13eb5cb082ca19db2bd2c5f77b1e5965 (patch) | |
tree | d7e4c96b17ce76331686fc3cab53326d0087f039 /loadtime/src | |
parent | 51a0fe4edd56a626ef5570e43bd2225be0dcb4dd (diff) | |
download | aspectj-8982544f13eb5cb082ca19db2bd2c5f77b1e5965.tar.gz aspectj-8982544f13eb5cb082ca19db2bd2c5f77b1e5965.zip |
Code for enhancement 107741: Updated WeavingURLClassLoader (thanks to Matthew Webster for the patch)
Diffstat (limited to 'loadtime/src')
-rw-r--r-- | loadtime/src/org/aspectj/weaver/loadtime/WeavingURLClassLoader.java | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/loadtime/src/org/aspectj/weaver/loadtime/WeavingURLClassLoader.java b/loadtime/src/org/aspectj/weaver/loadtime/WeavingURLClassLoader.java new file mode 100644 index 000000000..a80a4a918 --- /dev/null +++ b/loadtime/src/org/aspectj/weaver/loadtime/WeavingURLClassLoader.java @@ -0,0 +1,163 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Matthew Webster, Adrian Colyer, + * Martin Lippert initial implementation + * ******************************************************************/ + +package org.aspectj.weaver.loadtime; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.security.CodeSource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + +import org.aspectj.weaver.ExtensibleURLClassLoader; +import org.aspectj.weaver.tools.WeavingAdaptor; +import org.aspectj.weaver.tools.WeavingClassLoader; + +public class WeavingURLClassLoader extends ExtensibleURLClassLoader implements WeavingClassLoader { + + public static final String WEAVING_CLASS_PATH = "aj.class.path"; + public static final String WEAVING_ASPECT_PATH = "aj.aspect.path"; + + private URL[] aspectURLs; + private WeavingAdaptor adaptor; + private Map generatedClasses = new HashMap(); /* String -> byte[] */ + + /* + * This constructor is needed when using "-Djava.system.class.loader". + */ + public WeavingURLClassLoader (ClassLoader parent) { + this(getURLs(getClassPath()),getURLs(getAspectPath()),parent); +// System.err.println("? WeavingURLClassLoader.<init>(" + parent + ")"); + } + + public WeavingURLClassLoader (URL[] urls, ClassLoader parent) { + super(urls,parent); +// System.out.println("WeavingURLClassLoader.WeavingURLClassLoader()"); + } + + public WeavingURLClassLoader (URL[] classURLs, URL[] aspectURLs, ClassLoader parent) { + super(classURLs,parent); +// System.err.println("? WeavingURLClassLoader.<init>() classURLs=" + classURLs.length + ", aspectURLs=" + aspectURLs.length); + this.aspectURLs = aspectURLs; + + /* If either we nor our parent is using an ASPECT_PATH use a new-style + * adaptor + */ + if (this.aspectURLs.length > 0 || parent instanceof WeavingClassLoader) { + adaptor = new WeavingAdaptor(this); + } + } + + private static String getAspectPath () { + return System.getProperty(WEAVING_ASPECT_PATH,""); + } + + private static String getClassPath () { + return System.getProperty(WEAVING_CLASS_PATH,""); + } + + private static URL[] getURLs (String path) { + List urlList = new ArrayList(); + for (StringTokenizer t = new StringTokenizer(path,File.pathSeparator); + t.hasMoreTokens();) { + File f = new File(t.nextToken().trim()); + try { + if (f.exists()) { + URL url = f.toURL(); + if (url != null) urlList.add(url); + } + } catch (MalformedURLException e) {} + } + + URL[] urls = new URL[urlList.size()]; + urlList.toArray(urls); + return urls; + } + + protected void addURL(URL url) { + adaptor.addURL(url); + super.addURL(url); + } + + /** + * Override to weave class using WeavingAdaptor + */ + protected Class defineClass(String name, byte[] b, CodeSource cs) throws IOException { +// System.err.println("? WeavingURLClassLoader.defineClass(" + name + ", [" + b.length + "])"); + + /* Need to defer creation because of possible recursion during constructor execution */ + if (adaptor == null) { + ClassLoaderWeavingAdaptor clwAdaptor = new ClassLoaderWeavingAdaptor(this,null); + clwAdaptor.initialize(this,null); + adaptor = clwAdaptor; + } + + b = adaptor.weaveClass(name,b); + return super.defineClass(name, b, cs); + } + + /** + * Override to find classes generated by WeavingAdaptor + */ + protected byte[] getBytes (String name) throws IOException { + byte[] bytes = super.getBytes(name); + + if (bytes == null) { +// return adaptor.findClass(name); + return (byte[])generatedClasses.remove(name); + } + + return bytes; + } + + /** + * Implement method from WeavingClassLoader + */ + public URL[] getAspectURLs() { + return aspectURLs; + } + + public void acceptClass (String name, byte[] bytes) { + generatedClasses.put(name,bytes); + } + +// private interface ClassPreProcessorAdaptor extends ClassPreProcessor { +// public void addURL(URL url); +// } +// +// private class WeavingAdaptorPreProcessor implements ClassPreProcessorAdaptor { +// +// private WeavingAdaptor adaptor; +// +// public WeavingAdaptorPreProcessor (WeavingClassLoader wcl) { +// adaptor = new WeavingAdaptor(wcl); +// } +// +// public void initialize() { +// } +// +// public byte[] preProcess(String className, byte[] bytes, ClassLoader classLoader) { +// return adaptor.weaveClass(className,bytes); +// } +// +// public void addURL(URL url) { +// +// } +// } + +} |