< prev index next >

test/jdk/java/lang/instrument/asmlib/Instrumentor.java

Print this page
rev 52456 : imported patch asm.additional


  85         }
  86     }
  87 
  88     public static Instrumentor instrFor(byte[] classData) {
  89         return new Instrumentor(classData);
  90     }
  91 
  92 
  93     private final ClassReader cr;
  94     private final ClassWriter output;
  95     private ClassVisitor instrumentingVisitor = null;
  96     private final AtomicInteger matches = new AtomicInteger(0);
  97 
  98     private Instrumentor(byte[] classData) {
  99         cr = new ClassReader(classData);
 100         output = new ClassWriter(ClassWriter.COMPUTE_MAXS);
 101         instrumentingVisitor = output;
 102     }
 103 
 104     public synchronized Instrumentor addMethodEntryInjection(String methodName, Consumer<InstrHelper> injector) {
 105         instrumentingVisitor = new ClassVisitor(Opcodes.ASM5, instrumentingVisitor) {
 106             @Override
 107             public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
 108                 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
 109 
 110                 if (name.equals(methodName)) {
 111                     matches.getAndIncrement();
 112 
 113                     mv = new MethodVisitor(Opcodes.ASM5, mv) {
 114                         @Override
 115                         public void visitCode() {
 116                             injector.accept(new InstrHelper(mv, name));
 117                         }
 118                     };
 119                 }
 120                 return mv;
 121             }
 122         };
 123         return this;
 124     }
 125 
 126     public synchronized Instrumentor addNativeMethodTrackingInjection(String prefix, Consumer<InstrHelper> injector) {
 127         instrumentingVisitor = new ClassVisitor(Opcodes.ASM5, instrumentingVisitor) {
 128             private final Set<Consumer<ClassVisitor>> wmGenerators = new HashSet<>();
 129             private String className;
 130 
 131             @Override
 132             public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
 133                 this.className = name;
 134                 super.visit(version, access, name, signature, superName, interfaces);
 135             }
 136 
 137 
 138             @Override
 139             public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
 140                 if ((access & Opcodes.ACC_NATIVE) != 0) {
 141                     matches.getAndIncrement();
 142 
 143                     String newName = prefix + name;
 144                     wmGenerators.add((v)->{
 145                         MethodVisitor mv = v.visitMethod(access & ~Opcodes.ACC_NATIVE, name, desc, signature, exceptions);
 146                         mv.visitCode();
 147                         injector.accept(new InstrHelper(mv, name));




  85         }
  86     }
  87 
  88     public static Instrumentor instrFor(byte[] classData) {
  89         return new Instrumentor(classData);
  90     }
  91 
  92 
  93     private final ClassReader cr;
  94     private final ClassWriter output;
  95     private ClassVisitor instrumentingVisitor = null;
  96     private final AtomicInteger matches = new AtomicInteger(0);
  97 
  98     private Instrumentor(byte[] classData) {
  99         cr = new ClassReader(classData);
 100         output = new ClassWriter(ClassWriter.COMPUTE_MAXS);
 101         instrumentingVisitor = output;
 102     }
 103 
 104     public synchronized Instrumentor addMethodEntryInjection(String methodName, Consumer<InstrHelper> injector) {
 105         instrumentingVisitor = new ClassVisitor(Opcodes.ASM7, instrumentingVisitor) {
 106             @Override
 107             public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
 108                 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
 109 
 110                 if (name.equals(methodName)) {
 111                     matches.getAndIncrement();
 112 
 113                     mv = new MethodVisitor(Opcodes.ASM7, mv) {
 114                         @Override
 115                         public void visitCode() {
 116                             injector.accept(new InstrHelper(mv, name));
 117                         }
 118                     };
 119                 }
 120                 return mv;
 121             }
 122         };
 123         return this;
 124     }
 125 
 126     public synchronized Instrumentor addNativeMethodTrackingInjection(String prefix, Consumer<InstrHelper> injector) {
 127         instrumentingVisitor = new ClassVisitor(Opcodes.ASM7, instrumentingVisitor) {
 128             private final Set<Consumer<ClassVisitor>> wmGenerators = new HashSet<>();
 129             private String className;
 130 
 131             @Override
 132             public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
 133                 this.className = name;
 134                 super.visit(version, access, name, signature, superName, interfaces);
 135             }
 136 
 137 
 138             @Override
 139             public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
 140                 if ((access & Opcodes.ACC_NATIVE) != 0) {
 141                     matches.getAndIncrement();
 142 
 143                     String newName = prefix + name;
 144                     wmGenerators.add((v)->{
 145                         MethodVisitor mv = v.visitMethod(access & ~Opcodes.ACC_NATIVE, name, desc, signature, exceptions);
 146                         mv.visitCode();
 147                         injector.accept(new InstrHelper(mv, name));


< prev index next >