method.invoke - java.lang.RuntimeException:java.lang.IllegalArgumentException:object不是声明类的实例

时间:2012-09-24 10:33:58

标签: java reflection

有什么想法吗?

我在尝试调用对象时遇到错误。

R java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class

 Caused by: 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(Compiled Code))
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at com.test.corp.name.workflow.changetracking.changeimpl.FieldValueChange.apply(FieldValueChange.java:171)
 public void apply(DomainContext context) {
    try {
        final Class entityClazz = Class.forName(entityClass);
        Object entity = context.getEntity( entityClazz, primaryKey);
        Object entityChange = context.getChangeEntity(entityClazz, primaryKey);
        Method setter = getSetterMethod(entityClazz,fieldName);

        if(entity!=null)
            setter.invoke(entity, new Object[]{getValue()}); //this line 171
        if(entityChange!=null)
            setter.invoke(entityChange, new Object[]{getValue()});

    }catch(Exception e) {
        throw new RuntimeException(e);
    }
}

我认为这已经足够了,但仍有错误,有人吗?

1 个答案:

答案 0 :(得分:1)

它说的是entity不属于班级entityClazz。当您通过反射setter.invoke进行调用时,它当然会期望该类的对象。

尝试输出entity.getClass()entityClazz,看看它们是否相同,或者至少entityClazzentity.getClass()的父级。