Skip to content

8.3.14-core. java.lang.ClassCastException when call ExternalLinkPart#getJaxbElement() from SpreadsheetML package. #613

Description

@butthurter

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions