static <T> T newInstance(Class<T> type, String className, ClassLoader cl, boolean doFallback, boolean useBSClsLoader) throws FactoryConfigurationError { assert type != null; if (System.getSecurityManager() != null) { if (className != null && className.startsWith(DEFAULT_PACKAGE)) { cl = null; useBSClsLoader = true; } } try { Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader); if (!type.isAssignableFrom(providerClass)) { throw new ClassCastException(className + " cannot be cast to " + type.getName()); } Object instance = providerClass.getConstructor().newInstance(); final ClassLoader clD = cl; dPrint(()->"created new instance of " + providerClass + " using ClassLoader: " + clD); return type.cast(instance); } catch (ClassNotFoundException x) { throw new FactoryConfigurationError(x, "Provider " + className + " not found"); } catch (Exception x) { throw new FactoryConfigurationError(x, "Provider " + className + " could not be instantiated: " + x); } }