Ejemplo n.º 1
0
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: java com.sun.btrace.runtime.RunnableGenartor <class>");
      System.exit(1);
    }

    Class clazz = Class.forName(args[0]);
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
      int modifiers = method.getModifiers();
      int index = 0;
      RunnableGenerator gen = new RunnableGeneratorImpl();
      if (Modifier.isStatic(modifiers)
          || Modifier.isPublic(modifiers)
          || method.getParameterTypes().length == 0) {
        try {
          final String className = "Runnable$" + index;
          final byte[] bytes = gen.generate(method, className);
          ClassLoader loader =
              new ClassLoader() {
                public Class findClass(String name) throws ClassNotFoundException {
                  if (name.equals(className)) {
                    return defineClass(className, bytes, 0, bytes.length);
                  }
                  throw new ClassNotFoundException(name);
                }
              };
          Runnable r = (Runnable) loader.loadClass(className).newInstance();
          new Thread(r).start();
        } catch (Exception exp) {
          exp.printStackTrace();
        }
      }
      index++;
    }
  }