我正在尝试检测我的一门课,如果我添加
System.out.println(somevariable)
但是会抛出
由以下原因引起:编译错误:没有此类:记录器
添加时
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$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.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
logger语句有什么问题?
答案 0 :(得分:0)
也许您正在检测的类没有logger对象。 试试这个
endBlock.append("{new java.util.logging.Logger.getGlobal().log(java.util.logging.Level.debug,elapsedTime);}")