Search in sources :

Example 1 with InstrumentingClassLoader

use of com.scythe.instrumenter.instrumentation.InstrumentingClassLoader in project kanonizo by kanonizo.

the class Premain method premain.

/**
 * Premain that will be triggered when application runs with this
 * attached as a Java agent.
 *
 * @param arg   runtime properties to change
 * @param instr Instrumentation instance to attach a ClassFileTransformer
 */
public static void premain(String arg, Instrumentation instr) {
    ClassReplacementTransformer.addShouldInstrumentChecker((name) -> instrument);
    for (String s : forbiddenPackages) {
        ClassReplacementTransformer.addForbiddenPackage(s);
    }
    InstrumentingClassLoader loader = InstrumentingClassLoader.getInstance();
    InstrumentationProperties.INSTRUMENT_BRANCHES = false;
    InstrumentationProperties.WRITE_CLASS_IF_MODIFIED = true;
    instr.addTransformer((l, n, c, p, buf) -> {
        try {
            if (n != null) {
                return loader.modifyBytes(n, buf);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return buf;
    });
}
Also used : IOException(java.io.IOException) InstrumentingClassLoader(com.scythe.instrumenter.instrumentation.InstrumentingClassLoader)

Aggregations

InstrumentingClassLoader (com.scythe.instrumenter.instrumentation.InstrumentingClassLoader)1 IOException (java.io.IOException)1