Hi all, I need to declare and use an instance of a java.utils.logging.Logger cause int he project I have been working on I can't configure system out to print to console. How can I declare a Logger, and more in general instances of singletons and similar patterns with private constructors?
Currently I'm doing the following:
ClassPool classPool = ClassPool.getDefault();
classPool.importPackage("java.util.logging");
CtClass ctClass = classPool.makeClass(new ByteArrayInputStream(classfileBuffer));
CtClass logger = classPool.get("java.util.logging.Logger");
CtClass level = classPool.get("java.util.logging.Level");
CtField loggerInstance = new CtField(logger, "_stLogger", ctClass);
loggerInstance.setModifiers(Modifier.STATIC);
ctClass.addField(loggerInstance, CtField.Initializer.byNew(logger));
CtMethod[] methods = ctClass.getDeclaredMethods();
for (CtMethod method : methods) {
if (!Modifier.isNative(method.getModifiers()))
method.insertBefore("_stLogger.log(Level.SEVERE, \"Javassist test\");");
}
returnValue = ctClass.toBytecode();
ctClass.detach();
But I get
Exception in thread "main" java.lang.NoSuchMethodError: java.util.logging.Logger: method <init>()V not found
that results in a javassist.CannotCompileException: no method body
Kind Regards
Jacopo