|
| 1 | +package com.baeldung.instrumentation.agent; |
| 2 | + |
| 3 | +import javassist.CannotCompileException; |
| 4 | +import javassist.ClassPool; |
| 5 | +import javassist.CtClass; |
| 6 | +import javassist.CtMethod; |
| 7 | +import javassist.NotFoundException; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.lang.instrument.ClassFileTransformer; |
| 13 | +import java.lang.instrument.IllegalClassFormatException; |
| 14 | +import java.security.ProtectionDomain; |
| 15 | + |
| 16 | +public class AtmTransformer implements ClassFileTransformer { |
| 17 | + |
| 18 | + private static Logger LOGGER = LoggerFactory.getLogger(AtmTransformer.class); |
| 19 | + |
| 20 | + private static final String WITHDRAW_MONEY_METHOD = "withdrawMoney"; |
| 21 | + |
| 22 | + /** The internal form class name of the class to transform */ |
| 23 | + private String targetClassName; |
| 24 | + /** The class loader of the class we want to transform */ |
| 25 | + private ClassLoader targetClassLoader; |
| 26 | + |
| 27 | + public AtmTransformer(String targetClassName, ClassLoader targetClassLoader) { |
| 28 | + this.targetClassName = targetClassName; |
| 29 | + this.targetClassLoader = targetClassLoader; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, |
| 34 | + ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { |
| 35 | + byte[] byteCode = classfileBuffer; |
| 36 | + |
| 37 | + String finalTargetClassName = this.targetClassName.replaceAll("\\.", "/"); //replace . with / |
| 38 | + if (!className.equals(finalTargetClassName)) { |
| 39 | + return byteCode; |
| 40 | + } |
| 41 | + |
| 42 | + if (className.equals(finalTargetClassName) && loader.equals(targetClassLoader)) { |
| 43 | + LOGGER.info("[Agent] Transforming class MyAtm"); |
| 44 | + try { |
| 45 | + ClassPool cp = ClassPool.getDefault(); |
| 46 | + CtClass cc = cp.get(targetClassName); |
| 47 | + CtMethod m = cc.getDeclaredMethod(WITHDRAW_MONEY_METHOD); |
| 48 | + m.addLocalVariable("startTime", CtClass.longType); |
| 49 | + m.insertBefore("startTime = System.currentTimeMillis();"); |
| 50 | + |
| 51 | + StringBuilder endBlock = new StringBuilder(); |
| 52 | + |
| 53 | + m.addLocalVariable("endTime", CtClass.longType); |
| 54 | + m.addLocalVariable("opTime", CtClass.longType); |
| 55 | + endBlock.append("endTime = System.currentTimeMillis();"); |
| 56 | + endBlock.append("opTime = (endTime-startTime)/1000;"); |
| 57 | + |
| 58 | + endBlock.append("LOGGER.info(\"[Application] Withdrawal operation completed in:\" + opTime + \" seconds!\");"); |
| 59 | + |
| 60 | + m.insertAfter(endBlock.toString()); |
| 61 | + |
| 62 | + byteCode = cc.toBytecode(); |
| 63 | + cc.detach(); |
| 64 | + } catch (NotFoundException | CannotCompileException | IOException e) { |
| 65 | + LOGGER.error("Exception", e); |
| 66 | + } |
| 67 | + } |
| 68 | + return byteCode; |
| 69 | + } |
| 70 | +} |
0 commit comments