From cf48f125655b009791b54082cb0a34a1361cc8e4 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 24 Sep 2019 23:28:45 +0200 Subject: [PATCH] Add INSTANCE shortcut to SmackTestUtil --- .../jivesoftware/smack/test/util/SmackTestUtil.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestUtil.java b/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestUtil.java index 5f5dcf8d0..a81c11c97 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestUtil.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestUtil.java @@ -125,10 +125,19 @@ public class SmackTestUtil { return parser; } + @SuppressWarnings("unchecked") private static > P providerClassToProvider(Class

providerClass) { P provider; - // TODO: Consider adding a shortcut in case there is a static INSTANCE field holding an instance of the - // requested provider. + + try { + provider = (P) providerClass.getDeclaredField("INSTANCE").get(null); + return provider; + } catch (NoSuchFieldException e) { + // Continue with the next approach. + } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) { + throw new AssertionError(e); + } + try { provider = providerClass.getDeclaredConstructor().newInstance(); }