[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 2 years ago
parent d06e9499e8
commit b09cd06053
  1. 5
      smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaView.java

@ -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);
}
/**

Loading…
Cancel
Save