kostaskougios / cloning

deep clone java objects
Other
591 stars 112 forks source link

Fix for #105 #106

Closed tweimer closed 3 years ago

tweimer commented 3 years ago

Okay, I tested that with the specified fis and test case, as in the commit linked above.

Before the fix, the test failed with:

testStaticTransientMembers(com.rits.tests.cloning.TestCloner) Time elapsed: 0.002 sec <<< ERROR! java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.util.ArrayList.serialVersionUID accessible: module java.base does not "opens java.util" to unnamed module @23ceabc1 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177) at java.base/java.lang.reflect.Field.setAccessible(Field.java:171) at com.rits.cloning.Cloner$CloneObjectCloner.(Cloner.java:582) at com.rits.cloning.Cloner.findDeepCloner(Cloner.java:477) at com.rits.cloning.Cloner.cloneInternal(Cloner.java:441) at com.rits.cloning.Cloner.deepClone(Cloner.java:340) at com.rits.tests.cloning.TestCloner.testStaticTransientMembers(TestCloner.java:915)

After the fix, that Exception is gone, but tjhere is another one:

testStaticTransientMembers(com.rits.tests.cloning.TestCloner) Time elapsed: 0.002 sec <<< ERROR! java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not "opens java.util" to unnamed module @23ceabc1 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177) at java.base/java.lang.reflect.Field.setAccessible(Field.java:171) at com.rits.cloning.Cloner$CloneObjectCloner.(Cloner.java:589) at com.rits.cloning.Cloner.findDeepCloner(Cloner.java:477) at com.rits.cloning.Cloner.cloneInternal(Cloner.java:441) at com.rits.cloning.Cloner.deepClone(Cloner.java:340) at com.rits.tests.cloning.TestCloner.testStaticTransientMembers(TestCloner.java:915)

That doesn't make it much better, but at least the fix doesn't hurt.

Fixes #105

kostaskougios commented 3 years ago

Thanks Tobias, I was looking and trying to understand the issue as well. Btw I am also trying to move cloner to java 11 (running the tests with jdk 16 as well)

kostaskougios commented 3 years ago

Oh sorry just saw that with this change, a test in TestCloner fails. This is because the fields have to be accessible even if those are not going to be cloned. Cloner needs to set the value of the field in the clone object, even if the value doesn't have to be a deep-clone. So it does have to get it from the field (access it) in order to copy it to the new object.

I've pushed some changes to master

tweimer commented 3 years ago

Im sorry for that. Seems I forgot to re-run the full suite after I fixed the new test.

kostaskougios commented 3 years ago

no worries