< prev index next >

test/jdk/jdk/jfr/event/io/TestInstrumentation.java

Print this page
rev 52456 : imported patch asm.additional


 312         if (!instrClassesTarget.contains(className)) {
 313             return null;
 314         }
 315 
 316         boolean isRedefinition = classBeingRedefined != null;
 317         log("instrument class(" + className + ") " + (isRedefinition ? "redef" : "load"));
 318 
 319         ClassReader reader = new ClassReader(bytes);
 320         ClassWriter writer = new ClassWriter(
 321                 reader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
 322         CallbackClassVisitor classVisitor = new CallbackClassVisitor(writer);
 323         reader.accept(classVisitor, 0);
 324         instrClassesDone.add(className);
 325         return writer.toByteArray();
 326     }
 327 
 328     private static class CallbackClassVisitor extends ClassVisitor {
 329         private String className;
 330 
 331         public CallbackClassVisitor(ClassVisitor cv) {
 332             super(Opcodes.ASM5, cv);
 333         }
 334 
 335         @Override
 336         public void visit(
 337                 int version, int access, String name, String signature,
 338                 String superName, String[] interfaces) {
 339             cv.visit(version, access, name, signature, superName, interfaces);
 340             className = name;
 341         }
 342 
 343         @Override
 344         public MethodVisitor visitMethod(
 345                 int access, String methodName, String desc, String signature, String[] exceptions) {
 346             String methodKey = getInstrMethodKey(className, methodName, desc);
 347             boolean isInstrumentedMethod = Arrays.asList(instrMethodKeys).contains(methodKey);
 348             MethodVisitor mv = cv.visitMethod(access, methodName, desc, signature, exceptions);
 349             if (isInstrumentedMethod && mv != null) {
 350                 mv = new CallbackMethodVisitor(mv, methodKey);
 351                 log("instrumented " + methodKey);
 352             }
 353             return mv;
 354         }
 355     }
 356 
 357     public static class CallbackMethodVisitor extends MethodVisitor {
 358         private String logMessage;
 359 
 360         public CallbackMethodVisitor(MethodVisitor mv, String logMessage) {
 361             super(Opcodes.ASM5, mv);
 362             this.logMessage = logMessage;
 363         }
 364 
 365         @Override
 366         public void visitCode() {
 367             mv.visitCode();
 368             String methodDescr = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class));
 369             String className = InstrumentationCallback.class.getName().replace('.', '/');
 370             mv.visitLdcInsn(logMessage);
 371             mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, "callback", methodDescr);
 372         }
 373     }
 374 
 375 }


 312         if (!instrClassesTarget.contains(className)) {
 313             return null;
 314         }
 315 
 316         boolean isRedefinition = classBeingRedefined != null;
 317         log("instrument class(" + className + ") " + (isRedefinition ? "redef" : "load"));
 318 
 319         ClassReader reader = new ClassReader(bytes);
 320         ClassWriter writer = new ClassWriter(
 321                 reader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
 322         CallbackClassVisitor classVisitor = new CallbackClassVisitor(writer);
 323         reader.accept(classVisitor, 0);
 324         instrClassesDone.add(className);
 325         return writer.toByteArray();
 326     }
 327 
 328     private static class CallbackClassVisitor extends ClassVisitor {
 329         private String className;
 330 
 331         public CallbackClassVisitor(ClassVisitor cv) {
 332             super(Opcodes.ASM7, cv);
 333         }
 334 
 335         @Override
 336         public void visit(
 337                 int version, int access, String name, String signature,
 338                 String superName, String[] interfaces) {
 339             cv.visit(version, access, name, signature, superName, interfaces);
 340             className = name;
 341         }
 342 
 343         @Override
 344         public MethodVisitor visitMethod(
 345                 int access, String methodName, String desc, String signature, String[] exceptions) {
 346             String methodKey = getInstrMethodKey(className, methodName, desc);
 347             boolean isInstrumentedMethod = Arrays.asList(instrMethodKeys).contains(methodKey);
 348             MethodVisitor mv = cv.visitMethod(access, methodName, desc, signature, exceptions);
 349             if (isInstrumentedMethod && mv != null) {
 350                 mv = new CallbackMethodVisitor(mv, methodKey);
 351                 log("instrumented " + methodKey);
 352             }
 353             return mv;
 354         }
 355     }
 356 
 357     public static class CallbackMethodVisitor extends MethodVisitor {
 358         private String logMessage;
 359 
 360         public CallbackMethodVisitor(MethodVisitor mv, String logMessage) {
 361             super(Opcodes.ASM7, mv);
 362             this.logMessage = logMessage;
 363         }
 364 
 365         @Override
 366         public void visitCode() {
 367             mv.visitCode();
 368             String methodDescr = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class));
 369             String className = InstrumentationCallback.class.getName().replace('.', '/');
 370             mv.visitLdcInsn(logMessage);
 371             mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, "callback", methodDescr);
 372         }
 373     }
 374 
 375 }
< prev index next >