0 Replies Latest reply on Aug 20, 2015 4:38 AM by jbiscella

    Types with private constructor


      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();
              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);
              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();


      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