1 4 package com.tc.aspectwerkz.hook.impl; 5 6 import sun.misc.Resource; 7 import sun.misc.URLClassPath; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.lang.reflect.Method ; 12 import java.net.URL ; 13 import java.net.URLClassLoader ; 14 import java.util.ArrayList ; 15 import java.util.StringTokenizer ; 16 17 28 public class WeavingClassLoader extends URLClassLoader { 29 public WeavingClassLoader(URL [] urls, ClassLoader parent) { 30 super(urls, parent); 31 } 32 33 protected Class findClass(String name) throws ClassNotFoundException { 34 String path = name.replace('.', '/').concat(".class"); 35 Resource res = new URLClassPath(getURLs()).getResource(path, false); 36 if (res != null) { 37 try { 39 byte[] b = res.getBytes(); 40 byte[] transformed = ClassPreProcessorHelper.defineClass0Pre(this, name, b, 0, b.length, null); 41 return defineClass(name, transformed, 0, transformed.length); 42 } catch (IOException e) { 43 throw new ClassNotFoundException (e.getMessage()); 44 } 45 } else { 46 throw new ClassNotFoundException (name); 47 } 48 } 49 50 public static void main(String [] args) throws Exception { 51 String path = System.getProperty("java.class.path"); 52 ArrayList paths = new ArrayList (); 53 StringTokenizer st = new StringTokenizer (path, File.pathSeparator); 54 while (st.hasMoreTokens()) { 55 paths.add((new File (st.nextToken())).getCanonicalFile().toURL()); 56 } 57 58 ClassLoader cl = new WeavingClassLoader( 63 (URL []) paths.toArray(new URL []{}), ClassLoader.getSystemClassLoader() 64 .getParent() 65 ); 66 Thread.currentThread().setContextClassLoader(cl); 67 String s = args[0]; 68 String [] args1 = new String [args.length - 1]; 69 if (args1.length > 0) { 70 System.arraycopy(args, 1, args1, 0, args.length - 1); 71 } 72 Class class1 = Class.forName(s, false, cl); 73 Method method = class1.getMethod( 74 "main", new Class []{ 75 String [].class 76 } 77 ); 78 method.invoke( 79 null, new Object []{ 80 args1 81 } 82 ); 83 } 84 } | Popular Tags |