0

I try access info(String var) method in a final class that has a non visible contstructor Class(String var0, Logger var1, LoggerContext var2)

try {
Constructor<? extends Logger> var0 = logger.getClass().getDeclaredConstructor(String.class, Logger.class, LoggerContext.class);

var0.setAccessible(true);

logger.getClass().getDeclaredMethod("info", String.class).invoke(var0.newInstance(), new String(message));

} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
            e.printStackTrace();
}

and this is the error

java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
4
  • Note: new String(message) - this is completely not needed Commented Jan 24, 2020 at 16:31
  • 1
    The question is: what you are trying to actually do, this looks like you are trying to do something else, that's not how you are supposed to use any kind of logger. Commented Jan 24, 2020 at 16:33
  • I am trying to access a method from logback, specificly Logger#info(String) Commented Jan 24, 2020 at 16:34
  • 1
    Then you should get instance of logger from logger manager/factory, like LoggerFactory.getLogger("myLogger").info("message") where name of logger is often name of class but it does not need to be. Commented Jan 24, 2020 at 16:38

2 Answers 2

1

You can only use var0.newInstance() with the args of it.

Just pass objects of the String, Logger and LoggerContext to the newInstance() method.

Sign up to request clarification or add additional context in comments.

3 Comments

Then I get java.lang.IllegalArgumentException: object is not an instance of declaring class logger.getClass().getDeclaredMethod("info", String.class).invoke(var0, new String(message));
var0.newInstance(String.class, Logger.class, LoggerContext.class) @dan1st java.lang.IllegalArgumentException: argument type mismatch
You have to call it with objects of the type of these classes.
0

Solved! Then you should get instance of logger from logger manager/factory, like LoggerFactory.getLogger("myLogger").info("message") where name of logger is often name of class but it does not need to be. - @GotoFinal

EDIT: Sorry I don't know how to mark the right answer, quite new here.

2 Comments

and for future: xyproblem.info, please always include what you are trying to do, not just how.
Yeah sorry for that, will always include it in future questions ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.