异常 java.lang.IllegalArgumentException:对象不是 sun.reflect.NativeMethodAccessorImpl.invoke0 处声明类的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11992184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:12:03  来源:igfitidea点击:

exception java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0

javaillegalargumentexception

提问by billy_blanks

Hi I am receiving the above error when I run my code. I am having trouble understanding what is causing the error. I have seen the solution on a similar threadbut I am not sure if this applies in my case. Can someone please help me make some sense of this? Any help is greatly appreciated.

嗨,我在运行代码时收到上述错误。我无法理解导致错误的原因。我在类似的线程上看到了解决方案,但我不确定这是否适用于我的情况。有人可以帮我理解一下吗?任何帮助是极大的赞赏。

ERROR

错误

[org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/window].[jsp]]
Servlet.service() for servlet jsp threw exception
java.lang.IllegalArgumentException: object is not an instance of
declaring class     at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)     at
com.container.taglib.util.MirrorMaker.invokeMethod(MirrorMaker.java:54)
    at
com.container.taglib.util.MirrorMaker.invokeMethod(MirrorMaker.java:48)
    at
com.container.taglib.list.TableSorter.invokeMethod(TableSorter.java:1092)
    at
com.container.taglib.list.TableSorter.createList(TableSorter.java:503)
    at
com.container.taglib.list.TableSorter.doAfterBody(TableSorter.java:151)

Code:

代码:

public static Object invokeMethod(Object obj, Method method, Object[] params)throws IllegalAccessException, InvocationTargetException{
    Object ret = null;
    String str = "";
    try{
        ret = method.invoke(obj, params);
        if(ret instanceof String){
            str = (String)ret;
            //logger.info("ret str: "+str);                        
        }else if(ret instanceof Integer){
            str = ((Integer)ret).toString();
            //logger.info("ret int: "+str);
        }else if(ret instanceof java.util.Date){
            str = new SimpleDateFormat("yyyy-MM-dd").format(ret);
            logger.info("ret date: "+str);
        }else if(ret instanceof Double) {
            str = ((Double)ret).toString();
        }else if(ret instanceof ArrayList){
            return ret;
        }else{
            return ret;
        }

    }catch(IllegalAccessException ex){
        logger.info("illegal access");
        throw ex;
    }catch(InvocationTargetException ex){
        logger.error("invocation target ex");
        throw ex;
    }           
    return str;
}

回答by Luke Woodward

There is no reason for this exception being thrown other than by trying to use the reflection API to invoke a method using an object that is not an instance of the class that declares the method.

除了尝试使用反射 API 来调用方法,该方法使用的对象不是声明该方法的类的实例,否则没有理由抛出此异常。

The error message you get back from the reflection API doesn't help you a great deal figuring out exactly what went wrong. Adding a check such as the following to your invokeMethodfunction could provide more useful information:

您从反射 API 返回的错误消息并不能帮助您确定究竟出了什么问题。在您的invokeMethod函数中添加如下检查可以提供更多有用的信息:

if (!Modifier.isStatic(method.getModifiers())) {
    if (obj == null) {
        throw new NullPointerException("obj is null");
    } else if (!method.getDeclaringClass().isAssignableFrom(obj.getClass())) {
        throw new IllegalArgumentException(
            "Cannot call method '" + method + "' of class '" + method.getDeclaringClass().getName()
            + "' using object '" + obj + "' of class '" + obj.getClass().getName() + "' because"
            + " object '" + obj + "' is not an instance of '" + method.getDeclaringClass().getName() + "'");
    }
}

This code will check for an object being an instance of the class that declares the method, and if not, throw an exception with more details about the object and the method's declaring class.

此代码将检查一个对象是否是声明该方法的类的实例,如果不是,则抛出一个异常,其中包含有关该对象和该方法的声明类的更多详细信息。

It seems your code is part of a web application. I can speculate that the class of objand the declaring class of Methodmight be two different copies of the same class loaded in two separate classloaders. (In Tomcat, each web application uses its own classloader.) However, without being able to see your code and how you are running it, this is nothing more than a complete stab in the dark.

您的代码似乎是 Web 应用程序的一部分。我可以推测,的类obj和声明的类Method可能是在两个单独的类加载器中加载的同一类的两个不同副本。(在 Tomcat 中,每个 Web 应用程序都使用自己的类加载器。)然而,由于无法看到您的代码以及您如何运行它,这只不过是在黑暗中的完全刺杀。