简体   繁体   中英

method.invoke - java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class

Any idea?

I got an error, when trying to invoke an object.

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

I think this is enough, but still errors, anyone?

What it says is that entity is not of the class entityClazz . When you call via reflection setter.invoke it expect an object of that class, of course.

Try to output entity.getClass() and entityClazz and see if they are the same or at least entityClazz is parent of entity.getClass() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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