public static byte[] instrument(
      byte[] originalBytes, String recorderClass, String recorderMethod, ClassLoader loader) {
    try {
      ClassReader cr = new ClassReader(originalBytes);
      // The verifier in JDK7+ requires accurate stackmaps, so we use
      // COMPUTE_FRAMES.
      ClassWriter cw = new StaticClassWriter(cr, ClassWriter.COMPUTE_FRAMES, loader);

      VerifyingClassAdapter vcw = new VerifyingClassAdapter(cw, originalBytes, cr.getClassName());
      ClassVisitor adapter = new AllocationClassAdapter(vcw, recorderClass, recorderMethod);

      cr.accept(adapter, ClassReader.SKIP_FRAMES);

      return vcw.toByteArray();
    } catch (RuntimeException e) {
      logger.log(Level.WARNING, "Failed to instrument class.", e);
      throw e;
    } catch (Error e) {
      logger.log(Level.WARNING, "Failed to instrument class.", e);
      throw e;
    }
  }
Given the bytes representing a class, go through all the bytecode in it and instrument any
occurrences of new/newarray/anewarray/multianewarray with pre- and post-allocation hooks. Even
more fun, intercept calls to the reflection API's Array.newInstance() and instrument those too.

@param originalBytes the original <code>byte[]</code> code.
@param recorderClass the <code>String</code> internal name of the class containing the recorder
    method to run.
@param recorderMethod the <code>String</code> name of the recorder method to run.
@param loader the <code>ClassLoader</code> for this class.
@return the instrumented <code>byte[]</code> code.
src/main/java/com/google/monitoring/runtime/instrumentation/AllocationInstrumenter.java