[英]Java java.lang.IllegalArgumentException: wrong number of arguments
I try access info(String var) method in a final class that has a non visible contstructor Class(String var0, Logger var1, LoggerContext var2)我尝试在具有不可见构造函数 Class(String var0, Logger var1, LoggerContext var2) 的最终类中访问 info(String var) 方法
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)
You can only use var0.newInstance()
with the args of it.您只能将var0.newInstance()
与它的参数一起使用。
Just pass objects of the String
, Logger
and LoggerContext
to the newInstance()
method.只需将String
、 Logger
和LoggerContext
对象传递给newInstance()
方法。
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.然后你应该从记录器管理器/工厂获取记录器的实例,比如 LoggerFactory.getLogger("myLogger").info("message") 其中记录器的名称通常是类的名称,但它不需要是。 - @GotoFinal - @GotoFinal
EDIT: Sorry I don't know how to mark the right answer, quite new here.编辑:对不起,我不知道如何标记正确的答案,这里很新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.