1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| class NativeWrappingClassFileTransformer implements ClassFileTransformer {
static final Type BLOCK_HOUND_RUNTIME_TYPE = Type.getType("Lreactor/blockhound/BlockHoundRuntime;"); static final String PREFIX = "$$BlockHound$$_";
NativeWrappingClassFileTransformer() { }
@Override public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer ) {
ClassReader cr = new ClassReader(classfileBuffer); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
try { cr.accept(new NativeWrappingClassVisitor(cw, className), 0);
classfileBuffer = cw.toByteArray(); } catch (Throwable e) { e.printStackTrace(); throw e; }
return classfileBuffer; }
static class NativeWrappingClassVisitor extends ClassVisitor {
private final String className;
NativeWrappingClassVisitor(ClassVisitor cw, String internalClassName) { super(ASM7, cw); this.className = internalClassName; }
@Override public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { if ((access & ACC_NATIVE) == 0) { return super.visitMethod(access, name, descriptor, signature, exceptions); }
super.visitMethod( ACC_NATIVE | ACC_PRIVATE | ACC_FINAL | (access & ACC_STATIC), PREFIX + name, descriptor, signature, exceptions );
MethodVisitor delegatingMethodVisitor = super.visitMethod( access & ~ACC_NATIVE, name, descriptor, signature, exceptions); delegatingMethodVisitor.visitCode();
return new MethodVisitor(ASM7, delegatingMethodVisitor) {
@Override public void visitEnd() {
visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); visitLdcInsn("Method called: " + className + "." + name); visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
Type returnType = Type.getReturnType(descriptor); Type[] argumentTypes = Type.getArgumentTypes(descriptor); boolean isStatic = (access & ACC_STATIC)!= 0; if (!isStatic) { visitVarInsn(ALOAD, 0); } int index = isStatic? 0 : 1; for (Type argumentType : argumentTypes) { visitVarInsn(argumentType.getOpcode(ILOAD), index); index += argumentType.getSize(); }
visitMethodInsn( isStatic? INVOKESTATIC : INVOKESPECIAL, className, PREFIX + name, descriptor, false );
visitInsn(returnType.getOpcode(IRETURN)); visitMaxs(0, 0); super.visitEnd(); } }; }
} }
|