Javassist ; Caused by: compile error: no such class: logger

Javassist ; Caused by: compile error: no such class: logger

我正在尝试检测我的 class 之一,如果我添加

它工作正常

System.out.println(somevariable)

但它抛出

Caused by: compile error: no such class: logger

添加时

logger.debug(somevariable);

这是带有记录器的代码:

public class SleepingClassTransformer implements ClassFileTransformer {
public Logger logger = Logger.getLogger(SleepingClassTransformer.class);

public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,
    ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

    byte[] byteCode = classfileBuffer;

    if (className.equals("com/acc/instrument/Sleeping")) {
        logger.debug("in class transformer");

        try {
            StringBuilder endBlock = new StringBuilder();
            ClassPool cp = ClassPool.getDefault();
            CtClass cc = cp.get("com.acc.instrument.Sleeping");
            CtMethod m = cc.getDeclaredMethod("randomSleep");
            m.addLocalVariable("elapsedTime", CtClass.longType);
            m.insertBefore("elapsedTime = System.currentTimeMillis();");
            endBlock.append("{elapsedTime = System.currentTimeMillis() - elapsedTime;"
                    + "System.out.println(\"Method Executed in ms: \" + elapsedTime);}");
            endBlock.append("{logger.debug(elapsedTime);}");
            m.insertAfter(endBlock.toString());
            byteCode = cc.toBytecode();
            cc.detach();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    return byteCode;
}
}

完全异常:

DEBUG | 2018-10-16 16:10:03 | [main] instrument.SleepingClassTransformer (SleepingClassTransformer.java:23) - in class transformer
javassist.CannotCompileException: [source error] no such class: logger
    at javassist.CtBehavior.insertAfter(CtBehavior.java:819)
    at javassist.CtBehavior.insertAfter(CtBehavior.java:743)
    at com.accenture.instrument.SleepingClassTransformer.transform(SleepingClassTransformer.java:35)
    at sun.instrument.TransformerManager.transform(TransformerManager.java:169)
    at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:365)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access[=12=]0(URLClassLoader.java:58)
    at java.net.URLClassLoader.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at com.accenture.instrument.Main.main(Main.java:11)
Caused by: compile error: no such class: logger
    at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:436)
    at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:412)
    at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:330)
    at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:671)
    at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:156)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
    at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:241)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:329)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:350)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
    at javassist.compiler.Javac.compileStmnt(Javac.java:568)
    at javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:833)
    at javassist.CtBehavior.insertAfter(CtBehavior.java:785)
    ... 17 more

记录器语句有什么问题?

也许您正在检测的 class 没有记录器对象。 试试这个

endBlock.append("{new java.util.logging.Logger.getGlobal().log(java.util.logging.Level.debug,elapsedTime);}")