Description
I'm using docx4j-core version 8.3.14 to parse some XLSX content, and I ran into what looks like a bug with the ExternalLinkPart class. It seems the generic type might be off:
public class ExternalLinkPart extends JaxbSmlPart<JAXBElement<CTExternalLink>>
Because of this, I get a
java.lang.ClassCastException: org.xlsx4j.sml.CTExternalLink cannot be cast to javax.xml.bind.JAXBElement
when I call ExternalLinkPart#getJaxbElement().
Here’s the code that triggers the exception:
final ExternalLinkPart externalLinkPart = (ExternalLinkPart) workbookPart;
final JAXBElement<CTExternalLink> jaxbElement = externalLinkPart.getJaxbElement(); \\ <---- this line
final CTExternalLink ctExternalLink = jaxbElement.getValue();
I checked the other classes in the package, and none of them wrap their generic types in an extra JAXBElement.
It also seems like in 11.5.2 version there would be the same issue, but I haven't checked it.
Possible Solution
So, I think solution here would be simple - is to just change Generic type for this class to <CTExternalLink> without a wrapper:
public class ExternalLinkPart extends JaxbSmlPart<CTExternalLink>
Workaround
Interestingly, if I manually cast to the parent class JaxbSmlPart, everything works fine.
Here’s the workaround for this issue:
final JaxbSmlPart<CTExternalLink> externalLinkPart = (JaxbSmlPart<CTExternalLink>) workbookPart;
final CTExternalLink ctExternalLink = externalLinkPart.getJaxbElement();
Peace,
Nikolai
Description
I'm using docx4j-core version 8.3.14 to parse some XLSX content, and I ran into what looks like a bug with the ExternalLinkPart class. It seems the generic type might be off:
public class ExternalLinkPart extends JaxbSmlPart<JAXBElement<CTExternalLink>>Because of this, I get a
java.lang.ClassCastException: org.xlsx4j.sml.CTExternalLink cannot be cast to javax.xml.bind.JAXBElementwhen I call
ExternalLinkPart#getJaxbElement().Here’s the code that triggers the exception:
final ExternalLinkPart externalLinkPart = (ExternalLinkPart) workbookPart;final JAXBElement<CTExternalLink> jaxbElement = externalLinkPart.getJaxbElement(); \\ <---- this linefinal CTExternalLink ctExternalLink = jaxbElement.getValue();I checked the other classes in the package, and none of them wrap their generic types in an extra JAXBElement.
It also seems like in 11.5.2 version there would be the same issue, but I haven't checked it.
Possible Solution
So, I think solution here would be simple - is to just change Generic type for this class to
<CTExternalLink>without a wrapper:public class ExternalLinkPart extends JaxbSmlPart<CTExternalLink>Workaround
Interestingly, if I manually cast to the parent class JaxbSmlPart, everything works fine.
Here’s the workaround for this issue:
final JaxbSmlPart<CTExternalLink> externalLinkPart = (JaxbSmlPart<CTExternalLink>) workbookPart;final CTExternalLink ctExternalLink = externalLinkPart.getJaxbElement();Peace,
Nikolai