// Path: telemetry-instrumentation/src/main/java/com/yammer/telemetry/instrumentation/ClassInstrumentationHandler.java
// public interface ClassInstrumentationHandler {
//     boolean transformed(CtClass cc, ClassPool pool);
// }
// 
// Path: telemetry-instrumentation/src/main/java/com/yammer/telemetry/instrumentation/TelemetryTransformer.java
// public class TelemetryTransformer implements ClassFileTransformer {
//     private final Set<ClassInstrumentationHandler> handlers = new HashSet<>();
// 
//     public void addHandler(ClassInstrumentationHandler handler) {
//         handlers.add(handler);
//     }
// 
//     @Override
//     public byte[] transform(ClassLoader loader,
//                             String className,
//                             Class<?> classBeingRedefined,
//                             ProtectionDomain protectionDomain,
//                             byte[] classfileBuffer) throws IllegalClassFormatException {
//         return transform(loader, className, classfileBuffer, ClassPool.getDefault());
//     }
// 
//     /**
//      * Allows specifying the ClassPool, this allows tests to essentially 'reload' classes.
//      *
//      * @param loader
//      * @param className
//      * @param classfileBuffer
//      * @param cp
//      * @return
//      * @throws IllegalClassFormatException
//      */
//     public byte[] transform(ClassLoader loader,
//                             String className,
//                             byte[] classfileBuffer,
//                             ClassPool cp) throws IllegalClassFormatException {
//         try {
//             final String realClassName = className.replace('/', '.');
//             cp.insertClassPath(new LoaderClassPath(loader));
//             cp.insertClassPath(new ByteArrayClassPath(realClassName, classfileBuffer));
//             CtClass cc = cp.get(realClassName);
// 
//             boolean classUpdated = false;
//             for (ClassInstrumentationHandler handler : handlers) {
//                 if (classUpdated = handler.transformed(cc, cp)) {
//                     break;
//                 }
//             }
// 
//             if (classUpdated) {
//                 return cc.toBytecode();
//             } else {
//                 return null;
//             }
//         } catch (Exception e) {
//             e.printStackTrace();
//             return null;
//         } catch (Throwable t) {
//             t.printStackTrace();
//             throw t;
//         }
//     }
// }


import com.yammer.telemetry.instrumentation.ClassInstrumentationHandler;
import com.yammer.telemetry.instrumentation.TelemetryTransformer;
import org.junit.Before;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.fail;
            writer.println("Transformed tests failed:");
            for (Map.Entry<Method, Throwable> entry : testFailures.entrySet()) {
                writer.printf("%s:%n%s%n%n", entry.getKey(), entry.getValue());
                //noinspection ThrowableResultOfMethodCallIgnored
                entry.getValue().printStackTrace(writer);
                writer.println();
            }
            fail(builder.toString());
        }

        if (ran == 0) {
            fail("No tests were found within '" + clazz.getName() + "' that were annotated as '" + TransformedTest.class.getName() + "'");
        }
    }

    private static Throwable unwrap(Throwable e) {
        if (e instanceof InvocationTargetException) {
            return e.getCause();
        }
        return e;
    }

    public static void runTransformed(Class<?> clazz, String method, ClassInstrumentationHandler... handlers) throws Exception {
        Set<String> befores = new HashSet<>();
        for (Method beforeMethod : clazz.getDeclaredMethods()) {
            if (beforeMethod.isAnnotationPresent(Before.class)) {
                befores.add(beforeMethod.getName());
            }
        }

// Path: telemetry-instrumentation/src/main/java/com/yammer/telemetry/instrumentation/ClassInstrumentationHandler.java
// public interface ClassInstrumentationHandler {
//     boolean transformed(CtClass cc, ClassPool pool);
// }
// 
// Path: telemetry-instrumentation/src/main/java/com/yammer/telemetry/instrumentation/TelemetryTransformer.java
// public class TelemetryTransformer implements ClassFileTransformer {
//     private final Set<ClassInstrumentationHandler> handlers = new HashSet<>();
// 
//     public void addHandler(ClassInstrumentationHandler handler) {
//         handlers.add(handler);
//     }
// 
//     @Override
//     public byte[] transform(ClassLoader loader,
//                             String className,
//                             Class<?> classBeingRedefined,
//                             ProtectionDomain protectionDomain,
//                             byte[] classfileBuffer) throws IllegalClassFormatException {
//         return transform(loader, className, classfileBuffer, ClassPool.getDefault());
//     }
// 
//     /**
//      * Allows specifying the ClassPool, this allows tests to essentially 'reload' classes.
//      *
//      * @param loader
//      * @param className
//      * @param classfileBuffer
//      * @param cp
//      * @return
//      * @throws IllegalClassFormatException
//      */
//     public byte[] transform(ClassLoader loader,
//                             String className,
//                             byte[] classfileBuffer,
//                             ClassPool cp) throws IllegalClassFormatException {
//         try {
//             final String realClassName = className.replace('/', '.');
//             cp.insertClassPath(new LoaderClassPath(loader));
//             cp.insertClassPath(new ByteArrayClassPath(realClassName, classfileBuffer));
//             CtClass cc = cp.get(realClassName);
// 
//             boolean classUpdated = false;
//             for (ClassInstrumentationHandler handler : handlers) {
//                 if (classUpdated = handler.transformed(cc, cp)) {
//                     break;
//                 }
//             }
// 
//             if (classUpdated) {
//                 return cc.toBytecode();
//             } else {
//                 return null;
//             }
//         } catch (Exception e) {
//             e.printStackTrace();
//             return null;
//         } catch (Throwable t) {
//             t.printStackTrace();
//             throw t;
//         }
//     }
// }


// Path: telemetry-test-framework/src/main/java/com/yammer/telemetry/test/TelemetryTestHelpers.java
import com.yammer.telemetry.instrumentation.ClassInstrumentationHandler;
import com.yammer.telemetry.instrumentation.TelemetryTransformer;
import org.junit.Before;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.fail;

            writer.println("Transformed tests failed:");
            for (Map.Entry<Method, Throwable> entry : testFailures.entrySet()) {
                writer.printf("%s:%n%s%n%n", entry.getKey(), entry.getValue());
                //noinspection ThrowableResultOfMethodCallIgnored
                entry.getValue().printStackTrace(writer);
                writer.println();
            }
            fail(builder.toString());
        }

        if (ran == 0) {
            fail("No tests were found within '" + clazz.getName() + "' that were annotated as '" + TransformedTest.class.getName() + "'");
        }
    }

    private static Throwable unwrap(Throwable e) {
        if (e instanceof InvocationTargetException) {
            return e.getCause();
        }
        return e;
    }

    public static void runTransformed(Class<?> clazz, String method, ClassInstrumentationHandler... handlers) throws Exception {
        Set<String> befores = new HashSet<>();
        for (Method beforeMethod : clazz.getDeclaredMethods()) {
            if (beforeMethod.isAnnotationPresent(Before.class)) {
                befores.add(beforeMethod.getName());
            }
        }

        final TelemetryTransformer transformer = new TelemetryTransformer();