KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > hook > impl > WeavingClassLoader


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 JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLClassLoader JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
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 JavaDoc {
33     public WeavingClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
34         super(urls, parent);
35     }
36
37     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
38         String JavaDoc path = name.replace('.', '/').concat(".class");
39         Resource res = new URLClassPath(getURLs()).getResource(path, false);
40         if (res != null) {
41             //definePackage(name.substring(0, name.lastIndexOf(".")), null, null);
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 JavaDoc e) {
47                 throw new ClassNotFoundException JavaDoc(e.getMessage());
48             }
49         } else {
50             throw new ClassNotFoundException JavaDoc(name);
51         }
52     }
53
54     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
55         String JavaDoc path = System.getProperty("java.class.path");
56         ArrayList JavaDoc paths = new ArrayList JavaDoc();
57         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, File.pathSeparator);
58         while (st.hasMoreTokens()) {
59             paths.add((new File JavaDoc(st.nextToken())).getCanonicalFile().toURL());
60         }
61
62         //System.setProperty("aspectwerkz.transform.verbose", "yes");
63
//System.setProperty("aspectwerkz.transform.dump", "*");
64
//System.setProperty("aspectwerkz.definition.file", "...");
65
//@todo check child of extension classloader instead of boot classloader
66
ClassLoader JavaDoc cl = new WeavingClassLoader(
67                 (URL JavaDoc[]) paths.toArray(new URL JavaDoc[]{}), ClassLoader.getSystemClassLoader()
68                                                     .getParent()
69         );
70         Thread.currentThread().setContextClassLoader(cl);
71         String JavaDoc s = args[0];
72         String JavaDoc[] args1 = new String JavaDoc[args.length - 1];
73         if (args1.length > 0) {
74             System.arraycopy(args, 1, args1, 0, args.length - 1);
75         }
76         Class JavaDoc class1 = Class.forName(s, false, cl);
77         Method JavaDoc method = class1.getMethod(
78                 "main", new Class JavaDoc[]{
79                     String JavaDoc[].class
80                 }
81         );
82         method.invoke(
83                 null, new Object JavaDoc[]{
84                     args1
85                 }
86         );
87     }
88 }
Popular Tags