ExtensionLoader { @SuppressWarnings("unchecked") public static <T> T instantiateExtension(Class<T> clazz, String extensionClassName) { try { Class<?> extClazz = Class.forName(extensionClassName); if (clazz.isAssignableFrom(extClazz)) { try { return (T) extClazz.newInstance(); } catch (InstantiationException e) { throw new ExtensionNotLoadedException(e); } catch (IllegalAccessException e) { throw new ExtensionNotLoadedException(e); } } else { throw new ExtensionNotLoadedException("Extension class '" + extClazz.getName() + "' needs to have type: " + clazz.getName()); } } catch (ClassNotFoundException e) { } if (isOsgiAvailable) { OSGiExtensionLoader extLoader = OSGiExtensionLoader.getInstance(); return extLoader.getExtension(clazz, extensionClassName); } throw new ExtensionNotLoadedException("Unable to find implementation for " + clazz.getName() + ", the class or service " + extensionClassName + " could not be located!"); } private  ExtensionLoader(); @SuppressWarnings("unchecked") static T instantiateExtension(Class<T> clazz, String extensionClassName);  }
@Test public void testLoadingStringGenerator() throws ClassNotFoundException { TestStringGenerator g = ExtensionLoader.instantiateExtension(TestStringGenerator.class, TestStringGeneratorImpl.class.getName()); Assert.assertEquals("test", g.generateTestString()); }