繁体   English   中英

exception java.lang.IllegalArgumentException:object不是在sun.reflect.NativeMethodAccessorImpl.invoke0声明类的实例

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

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

错误

[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)

码:

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;
}

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

您从反射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() + "'");
    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM