View Javadoc

    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              //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 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          //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 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  }