/** * Creates a new instance of the given SPI class. * * @param <S> Any type extends T * @param impl The SPI class has to be instantiated * @return A new instance of the given SPI class * @throws DiscoveryException if the class implementing * the SPI cannot be found, cannot be loaded and * instantiated, or if the resulting class does not implement * (or extend) the SPI * @throws InstantiationException see {@link Class#newInstance()} * @throws IllegalAccessException see {@link Class#newInstance()} * @throws NoSuchMethodException see {@link Class#newInstance()} * @throws InvocationTargetException see {@link Class#newInstance()} */ public <S extends T> S newInstance(Class<S> impl) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { verifyAncestory(impl); return ClassUtils.newInstance(impl, paramClasses, params); }
/** * Returns the default class, loading it if necessary * and verifying that it implements the SPI * (this forces the check, no way out..). * * @param <S> Any type extends the SPI type * @param spi non-null SPI * @param loaders Used only if class needs to be loaded. * @return The default Class. */ public <S extends T> Class<S> getDefaultClass(SPInterface<T> spi, ClassLoaders loaders) { if (defaultClass == null) { DiscoverClasses<T> classDiscovery = new DiscoverClasses<T>(loaders); ResourceClassIterator<T> classes = classDiscovery.findResourceClasses(getDefaultName()); if (classes.hasNext()) { ResourceClass<T> info = classes.nextResourceClass(); try { defaultClass = info.loadClass(); } catch (Exception e) { // ignore } } } if (defaultClass != null) { spi.verifyAncestory(defaultClass); } @SuppressWarnings("unchecked") // the SPInterface.verifyAncestory already asserted Class<S> returned = (Class<S>) defaultClass; return returned; }