highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
108 stars 54 forks source link

search accessible clone() to fix java17 compatability (#130) #163

Closed Boris-de closed 7 months ago

Boris-de commented 1 year ago

XMLGregorianCalender is not cloneable with the current implementation when using java 17. The implementation did not select the method XMLGregorianCalendar.clone() which is accessible to anyone, but rather the method XMLGregorianCalenderImpl.clone() to which access is prohibited in recent java versions ending up in an IllegalAccessError.

This fixes the problem by searching for the first clone() method in the classes hierarchy that is public and accessible. This removes the need of calling setAccessible() because the method will be accessible. For java8 it will still use the old logic because Method.canAccess() does not exist until java9.

(This is a possible fix for #130)

Boris-de commented 1 year ago

Note: This is using reflection to use Method.canAccess(). An alternative would be to create a multi-release jar that provides different implementations for java8 and java9+. As the implementation is heavily using reflection anyway going the reflective way seemed fine to me, but I can adapt this PR to a multi-release jar if you want.

Another very simple fix might be to have a special handling for XMLGregorianCalendar (and possibly Duration?) in DefaultCopyStrategy.copyInternal(). This would only fix it for those classes, which might be enough because in reality it should only affect classes from the java.xml module?

laurentschoelens commented 7 months ago

Closing as worked done in jaxb-tools repository