1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz.hook.impl;
9
10 import sun.misc.Resource;
11 import sun.misc.URLClassPath;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.lang.reflect.Method;
16 import java.net.URL;
17 import java.net.URLClassLoader;
18 import java.util.ArrayList;
19 import java.util.StringTokenizer;
20
21 /***
22 * Very basic classloader that do online weaving. <p/>This classloader can be used thru several means
23 * <ul>
24 * <li>as a URLClassLoader in a custom development</li>
25 * <li>as a <i>MainClass </i> to allow on the fly weaving (without support for classloader hierarchy)</li>
26 * </ul>
27 * It can also be used for debugging step by step in any IDE
28 *
29 * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
30 * @todo rewrite based on SUN src (definePackage missing)
31 */
32 public class WeavingClassLoader extends URLClassLoader {
33 public WeavingClassLoader(URL[] urls, ClassLoader parent) {
34 super(urls, parent);
35 }
36
37 protected Class findClass(String name) throws ClassNotFoundException {
38 String path = name.replace('.', '/').concat(".class");
39 Resource res = new URLClassPath(getURLs()).getResource(path, false);
40 if (res != null) {
41
42 try {
43 byte[] b = res.getBytes();
44 byte[] transformed = ClassPreProcessorHelper.defineClass0Pre(this, name, b, 0, b.length, null);
45 return defineClass(name, transformed, 0, transformed.length);
46 } catch (IOException e) {
47 throw new ClassNotFoundException(e.getMessage());
48 }
49 } else {
50 throw new ClassNotFoundException(name);
51 }
52 }
53
54 public static void main(String[] args) throws Exception {
55 String path = System.getProperty("java.class.path");
56 ArrayList paths = new ArrayList();
57 StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
58 while (st.hasMoreTokens()) {
59 paths.add((new File(st.nextToken())).getCanonicalFile().toURL());
60 }
61
62
63
64
65
66 ClassLoader cl = new WeavingClassLoader((URL[]) paths.toArray(new URL[] {}), ClassLoader.getSystemClassLoader()
67 .getParent());
68 Thread.currentThread().setContextClassLoader(cl);
69 String s = args[0];
70 String[] args1 = new String[args.length - 1];
71 if (args1.length > 0) {
72 System.arraycopy(args, 1, args1, 0, args.length - 1);
73 }
74 Class class1 = cl.loadClass(s);
75 Method method = class1.getMethod("main", new Class[] {
76 String[].class
77 });
78 method.invoke(null, new Object[] {
79 args1
80 });
81 }
82 }