1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 03:27:59 +01:00

[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.
This commit is contained in:
Florian Schmaus 2020-08-28 09:52:28 +02:00
parent d06e9499e8
commit b09cd06053

View file

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