From b09cd0605322d27367371f066d15275f5c0b05d5 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 28 Aug 2020 09:52:28 +0200 Subject: [PATCH] [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. --- .../main/java/org/jivesoftware/smack/packet/StanzaView.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaView.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaView.java index 7745ecc2a..003ddbeef 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaView.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaView.java @@ -90,11 +90,12 @@ public interface StanzaView extends XmlLangElement { default E getExtension(Class 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); } /**