+/EE379K
programming paradigms
git clone https://git.julia.blue/+/EE379K
1653c54
master
1import java .io .*; 2import java .lang .instrument .ClassFileTransformer ; 3import java .lang .instrument .Instrumentation ; 4import java .nio .file .*; 5import java .security .ProtectionDomain ; 6import java .util .*; 7import java .util .stream .*; 8import org .objectweb .asm .ClassReader ; 9import org .objectweb .asm .ClassVisitor ; 10import org .objectweb .asm .ClassWriter ; 11import org .objectweb .asm .MethodVisitor ; 12import org .objectweb .asm .Opcodes ; 13 14public class Agent { 15private static Instrumentation inst ; 16 17public static void premain (String options , 18Instrumentation instrumentation ) { 19inst =instrumentation ; 20instrumentation .addTransformer (new Transformer ("class_whitelist" )); 21Runtime .getRuntime ().addShutdownHook (new Thread () { 22public void run () {Counter .report (); } 23 }); 24 } 25} 26 27class Transformer implements ClassFileTransformer { 28static Set <String >whitelist ; 29 30Transformer (String whitelistFile ) { 31super (); 32Path path =Paths .get (whitelistFile ); 33whitelist =new HashSet <>(); 34try (Stream <String >lines =Files .lines (path )) { 35lines .forEach (l ->whitelist .add (l )); 36 }catch (IOException ex ) {System .out .println (ex .toString ()); } 37 } 38 39public byte []transform (ClassLoader loader ,String className , 40Class <?>classBeingRedefined , 41ProtectionDomain protectionDomain , 42byte []classfileBuffer ) { 43if (whitelist .contains (className )) { 44// System.out.println("Loading " + className); 45ClassReader cr =new ClassReader (classfileBuffer ); 46ClassWriter cw =new ClassWriter (cr ,ClassWriter .COMPUTE_MAXS ); 47CoverageVisitor cv =new CoverageVisitor (cw ,className ); 48cr .accept (cv ,0 ); 49return cw .toByteArray (); 50 }else { 51return null ; 52 } 53 } 54}