+/EE379K
programming paradigms
git clone https://git.julia.blue/+/EE379K
1653c54
master
1import java .io .File ; 2import java .nio .file .Files ; 3import java .util .*; 4import java .util .Map .Entry ; 5import org .objectweb .asm .*; 6 7class Counter { 8static Map <String ,Integer >invocations =new HashMap <>(); 9 10static void count (String name ) { 11invocations .put (name ,invocations .get (name ) +1 ); 12 } 13 14static void report () { 15System .out .println ("\n\nCOVERAGE DATA\n" ); 16TreeMap <String ,Integer >sorted =new TreeMap <>(invocations ); 17String current ="" ; 18for (Entry <String ,Integer >mapping :sorted .entrySet ()) { 19String className =mapping .getKey ().split ("\\." )[0 ]; 20String methodName =mapping .getKey ().split ("\\." )[1 ]; 21if (!className .equals (current )) { 22current =className ; 23System .out .println ("\nClass " +className ); 24 } 25System .out .println (methodName +": " +mapping .getValue ()); 26 } 27 } 28} 29 30class MVisitor extends MethodVisitor { 31private String name ; 32 33public MVisitor (MethodVisitor mv ,String name ) { 34super (Opcodes .ASM5 ,mv ); 35this .name =name ; 36Counter .invocations .put (name ,0 ); 37 } 38 39@ Override 40public void visitCode () { 41mv .visitLdcInsn (name ); 42mv .visitMethodInsn (Opcodes .INVOKESTATIC , 43Type .getType (Counter .class ).getInternalName (), 44"count" , 45Type .getMethodDescriptor (Type .VOID_TYPE , 46Type .getType (String .class )), 47false ); 48super .visitCode (); 49 } 50} 51 52public class CoverageVisitor extends ClassVisitor { 53String className ; 54 55public CoverageVisitor (ClassWriter cw ,String className ) { 56super (Opcodes .ASM5 ,cw ); 57this .className =className ; 58 } 59 60@ Override 61public MethodVisitor visitMethod (int access ,String name ,String desc , 62String signature ,String []exceptions ) { 63MethodVisitor mv = 64super .visitMethod (access ,name ,desc ,signature ,exceptions ); 65// System.out.println("Visiting " + name); 66if (!name .equals ("<init>" )) { 67mv =new MVisitor (mv ,className +"." +name ); 68 } 69return mv ; 70 } 71}