1 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 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 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 ClassLoader cl = new WeavingClassLoader( 67 (URL []) paths.toArray(new URL []{}), ClassLoader.getSystemClassLoader() 68 .getParent() 69 ); 70 Thread.currentThread().setContextClassLoader(cl); 71 String s = args[0]; 72 String [] args1 = new String [args.length - 1]; 73 if (args1.length > 0) { 74 System.arraycopy(args, 1, args1, 0, args.length - 1); 75 } 76 Class class1 = Class.forName(s, false, cl); 77 Method method = class1.getMethod( 78 "main", new Class []{ 79 String [].class 80 } 81 ); 82 method.invoke( 83 null, new Object []{ 84 args1 85 } 86 ); 87 } 88 } | Popular Tags |