コード例 #1
0
  public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: java com.sun.btrace.runtime.ErrorReturnInstrumentor <class>");
      System.exit(1);
    }

    args[0] = args[0].replace('.', '/');
    FileInputStream fis = new FileInputStream(args[0] + ".class");
    ClassReader reader = new ClassReader(new BufferedInputStream(fis));
    FileOutputStream fos = new FileOutputStream(args[0] + ".class");
    ClassWriter writer = InstrumentUtils.newClassWriter();
    InstrumentUtils.accept(
        reader,
        new ClassVisitor(Opcodes.ASM4, writer) {
          public MethodVisitor visitMethod(
              int access, String name, String desc, String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            return new ErrorReturnInstrumentor(mv, null, args[0], args[0], access, name, desc);
          }
        });
    fos.write(writer.toByteArray());
  }
コード例 #2
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++;
    }
  }