mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-25 21:42:07 +01:00
[core] Add XmppElementUtil.castOrThrow(ExtensionElement, Class)
This method throws an IllegalStateException if the provided extension element is not of the expected type and hints users towards potential causes.
This commit is contained in:
parent
99297e5a76
commit
d06e9499e8
1 changed files with 35 additions and 6 deletions
|
@ -23,7 +23,10 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.FullyQualifiedElement;
|
import org.jivesoftware.smack.packet.FullyQualifiedElement;
|
||||||
|
import org.jivesoftware.smack.packet.StandardExtensionElement;
|
||||||
|
import org.jivesoftware.smack.provider.ProviderManager;
|
||||||
|
|
||||||
import org.jxmpp.util.cache.LruCache;
|
import org.jxmpp.util.cache.LruCache;
|
||||||
|
|
||||||
|
@ -68,21 +71,47 @@ public class XmppElementUtil {
|
||||||
return qname;
|
return qname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <E extends FullyQualifiedElement, R extends FullyQualifiedElement> List<R> getElementsFrom(
|
public static <E extends ExtensionElement> List<E> getElementsFrom(
|
||||||
MultiMap<QName, E> elementMap, Class<R> extensionElementClass) {
|
MultiMap<QName, ExtensionElement> elementMap, Class<E> extensionElementClass) {
|
||||||
QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
|
QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
|
||||||
|
|
||||||
List<E> extensionElements = elementMap.getAll(qname);
|
List<ExtensionElement> extensionElements = elementMap.getAll(qname);
|
||||||
|
|
||||||
if (extensionElements.isEmpty()) {
|
if (extensionElements.isEmpty()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<R> res = new ArrayList<>(extensionElements.size());
|
List<E> res = new ArrayList<>(extensionElements.size());
|
||||||
for (E extensionElement : extensionElements) {
|
for (ExtensionElement extensionElement : extensionElements) {
|
||||||
R e = extensionElementClass.cast(extensionElement);
|
E e = castOrThrow(extensionElement, extensionElementClass);
|
||||||
res.add(e);
|
res.add(e);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <E extends ExtensionElement> E castOrThrow(ExtensionElement extensionElement, Class<E> extensionElementClass) {
|
||||||
|
if (!extensionElementClass.isInstance(extensionElement)) {
|
||||||
|
final QName qname = getQNameFor(extensionElementClass);
|
||||||
|
|
||||||
|
final String detailMessage;
|
||||||
|
if (extensionElement instanceof StandardExtensionElement) {
|
||||||
|
detailMessage = "because there is no according extension element provider registered with ProviderManager for "
|
||||||
|
+ qname
|
||||||
|
+ ". WARNING: This indicates a serious problem with your Smack setup, probably causing Smack not being able to properly initialize itself.";
|
||||||
|
} else {
|
||||||
|
Object provider = ProviderManager.getExtensionProvider(qname);
|
||||||
|
detailMessage = "because there is an inconsistency with the provider registered with ProviderManager: the active provider for "
|
||||||
|
+ qname + " '" + provider.getClass()
|
||||||
|
+ "' does not return instances of type " + extensionElementClass
|
||||||
|
+ ", but instead returns instances of type " + extensionElement.getClass() + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
String message = "Extension element is not of expected class '" + extensionElementClass.getName() + "', "
|
||||||
|
+ detailMessage;
|
||||||
|
throw new IllegalStateException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return extensionElementClass.cast(extensionElement);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue