Hibernate version: 3.0
Occasionally I have problems invoking hibernate entities through reflection. Sometimes the underlying JDK throws
java.lang.IllegalArgumentException: object is not an instance of declaring class
I have written a class called PropertyMapping which invokes my entity's property setter method. In the following example I am trying to set a simple String property called "name" on an entity of class TextView, and though it almost always works, it does fail occasionally, indicated by the exception above.
What happens is this:
1. I look up my entity's setName method like this:
writeMethod =
myTextView.getClass().getMethod(writeMethodName, propertyCarrierClass);
2. I invoke the write method like this
writeMethod.invoke(myTextView, object);
3. Sometimes, not always, Java throws
java.lang.IllegalArgumentException:
object is not an instance of declaring class
as if to indicate that the entity is not the entity from which I looked up the method.
I have sourrounded my method invokation with the following code:
try {...}
catch(IllegalArgumentException exception) {
throw new SystemFailure(
"\nUnable to write a value property \"" +
property.getName() + "\" to an entity property:\n" +
"Entity class: \"" + (myTextView == null ? null : myTextView.getClass().getName()) + "\"\n" +
"Entity method name: \"" + (writeMethod == null ? null : writeMethod.getName()) + "\"\n" +
"Method belongs to class: \"" + writeMethod.getDeclaringClass().getName() + "\"\n" +
"Method parameter type: \"" + writeMethod.getParameterTypes()[0].getName() + "\"\n" +
"Object to be written: \"" + property.getObject() + "\"\n" + "Object type: \"" + (property.getObject() == null ? null : property.getObject().getClass().getName()) + "\"\n",
exception
);
}
And this code outputs the following
Unable to write a value property "name" to an entity property:
Entity class: "dk.rockit.puls.server.entity.website.content.text.TextView"
Entity method name: "setName"
Method belongs to class: "dk.rockit.puls.server.entity.website.content.text.Text
View$$EnhancerByCGLIB$$46db9118"
Method parameter type: "java.lang.String"
Object to be written: "H°jre kolonnes visning"
Object type: "java.lang.String"
Here is the exception, which is thrown occasionally:
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:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at dk.rockit.puls.server.value.manager.propertyMapping.PrimitiveMapping.
write(PrimitiveMapping.java:118)
at dk.rockit.puls.server.value.manager.propertyMapping.PrimitiveMapping.
differentiate(PrimitiveMapping.java:150)
at dk.rockit.puls.server.value.manager.AbstractValueManager.differentiat
e(AbstractValueManager.java:403)
...
So, as you can see, I look up a method on an entity which is being proxied by CGLIB and then somehow this makes the reflection fail. In the example above I am looking up the method "setName" on a class called "TextView", which apparantly is being proxied by CGLIB as
dk.rockit.puls.server.entity.website.content.text.TextView$$EnhancerByCGLIB$$46db9118
and then the reflection mechanism below is unable to operate correctly.
Has anyone got a clue to what could be wrong here and / or why this only fails occasionally and not all the time?
Any hints would be highly appreciated.
Randahl
|