简体   繁体   English

Java java.lang.IllegalArgumentException:参数数量错误

[英]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.只需将StringLoggerLoggerContext对象传递给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.

相关问题 反射错误:java.lang.IllegalArgumentException:参数数量错误 - Reflections error: java.lang.IllegalArgumentException: wrong number of arguments java.lang.IllegalArgumentException:参数个数错误调用构造函数时发生异常 - java.lang.IllegalArgumentException: wrong number of arguments Exception while invoking constructor 获取java.lang.IllegalArgumentException:以下代码段的参数数量错误 - Getting java.lang.IllegalArgumentException: wrong number of arguments error for below code snippet TestNG工厂,DataProvider和构造函数上的valargs导致“ java.lang.IllegalArgumentException:参数数量错误” 2 - TestNG Factory, DataProvider, and valargs on constructor is causing “java.lang.IllegalArgumentException: wrong number of arguments”2 Method.invoke抛出java.lang.IllegalArgumentException:参数数量错误 - Method.invoke throws java.lang.IllegalArgumentException: wrong number of arguments 线程“ main”中的异常java.lang.IllegalArgumentException:运行kafka生产程序时参数数量错误 - Exception in thread “main” java.lang.IllegalArgumentException: wrong number of arguments while running kafka producer 在线程“主”中使用method.invoke异常java.lang.IllegalArgumentException:参数数量错误 - Using method.invoke Exception in thread “main” java.lang.IllegalArgumentException: wrong number of arguments java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException 为什么我得到 Java.lang.IllegalArgumentException:使用反射创建带有私有构造函数的新实例时参数数量错误 - Why do I get Java.lang.IllegalArgumentException: wrong number of arguments while creating new instance with private constructor using reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM