[core] Use XmppElementUtil.castOrThrow() in StanzaView.getExtension(Class)

This means that users get now exceptions with helpful error messages
instead of the dreaded ClassCastException, like

java.lang.ClassCastException: org.jivesoftware.smack.packet.StandardExtensionElement cannot be cast to org.jivesoftware.smackx.mam.element.MamElements$MamResultExtension
at

when StanzaView.getExtension(Class) is used to retrieve the extension.
listEqualsHashcode
Florian Schmaus 2020-08-28 09:52:28 +02:00
parent d06e9499e8
commit b09cd06053
1 changed files with 3 additions and 2 deletions

View File

@ -90,11 +90,12 @@ public interface StanzaView extends XmlLangElement {
default <E extends ExtensionElement> E getExtension(Class<E> extensionElementClass) {
QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
ExtensionElement extensionElement = getExtension(qname);
if (!extensionElementClass.isInstance(extensionElement)) {
if (extensionElement == null) {
return null;
}
return extensionElementClass.cast(extensionElement);
return XmppElementUtil.castOrThrow(extensionElement, extensionElementClass);
}
/**