@Test public void throwsAppropriateExceptionIfClassNotFound() { ExtensionObjectFactory factory = new ExtensionObjectFactory(extensionFactory -> name -> { throw new ClassNotFoundException(); }); Exception e = assertThrows(ExtensionLoadingException.class, () -> factory.newInstance(new SpiExtensionFactory("some.FakeClass", "/fake/class/path"), Object.class)); assertEquals("no class=some.FakeClass is found in the specified classpath=/fake/class/path", e.getMessage()); }
public <T> T newInstance(SpiExtensionFactory extensionFactory, Class<T> type) throws ExtensionLoadingException { try { Class<?> extensionClass = loadClass(extensionFactory); Object instance = extensionClass.newInstance(); return type.cast(instance); } catch (ExtensionLoadingException e) { throw e; } catch (Exception e) { throw new ExtensionLoadingException("error instantiating class=" + extensionFactory.factoryClass(), e); } }
ExtensionObjectFactory { public <T> T newInstance(SpiExtensionFactory extensionFactory, Class<T> type) throws ExtensionLoadingException { try { Class<?> extensionClass = loadClass(extensionFactory); Object instance = extensionClass.newInstance(); return type.cast(instance); } catch (ExtensionLoadingException e) { throw e; } catch (Exception e) { throw new ExtensionLoadingException("error instantiating class=" + extensionFactory.factoryClass(), e); } } }
ExtensionObjectFactory { public <T> T newInstance(SpiExtensionFactory extensionFactory, Class<T> type) throws ExtensionLoadingException { try { Class<?> extensionClass = loadClass(extensionFactory); Object instance = extensionClass.newInstance(); return type.cast(instance); } catch (ExtensionLoadingException e) { throw e; } catch (Exception e) { throw new ExtensionLoadingException("error instantiating class=" + extensionFactory.factoryClass(), e); } } private  ExtensionObjectFactory(); @VisibleForTesting  ExtensionObjectFactory(Function<SpiExtensionFactory, ClassSource> extensionLocator); }
ExtensionObjectFactory { public <T> T newInstance(SpiExtensionFactory extensionFactory, Class<T> type) throws ExtensionLoadingException { try { Class<?> extensionClass = loadClass(extensionFactory); Object instance = extensionClass.newInstance(); return type.cast(instance); } catch (ExtensionLoadingException e) { throw e; } catch (Exception e) { throw new ExtensionLoadingException("error instantiating class=" + extensionFactory.factoryClass(), e); } } private  ExtensionObjectFactory(); @VisibleForTesting  ExtensionObjectFactory(Function<SpiExtensionFactory, ClassSource> extensionLocator); T newInstance(SpiExtensionFactory extensionFactory, Class<T> type); }
ExtensionObjectFactory { public <T> T newInstance(SpiExtensionFactory extensionFactory, Class<T> type) throws ExtensionLoadingException { try { Class<?> extensionClass = loadClass(extensionFactory); Object instance = extensionClass.newInstance(); return type.cast(instance); } catch (ExtensionLoadingException e) { throw e; } catch (Exception e) { throw new ExtensionLoadingException("error instantiating class=" + extensionFactory.factoryClass(), e); } } private  ExtensionObjectFactory(); @VisibleForTesting  ExtensionObjectFactory(Function<SpiExtensionFactory, ClassSource> extensionLocator); T newInstance(SpiExtensionFactory extensionFactory, Class<T> type); static final ExtensionObjectFactory EXTENSION_OBJECT_FACTORY; }