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