public class ServiceLoader<S>
extends java.lang.Object
implements java.lang.Iterable<S>
Modifier and Type | Field and Description |
---|---|
private java.lang.Class<S> |
expectedType |
private java.lang.ClassLoader |
loader |
private java.util.Set<S> |
providers |
private java.lang.String |
serviceFile |
private static java.lang.String |
SERVICES |
Modifier | Constructor and Description |
---|---|
private |
ServiceLoader(java.lang.Class<S> service,
java.lang.ClassLoader loader) |
Modifier and Type | Method and Description |
---|---|
S |
createInstance(java.lang.String line) |
java.util.Set<S> |
getProviders() |
java.util.Iterator<S> |
iterator()
Lazily loads the available providers of this loader's service.
|
static <S> ServiceLoader<S> |
load(java.lang.Class<S> service)
Creates a new service loader for the given service type, using the current
thread's context class loader.
|
static <S> ServiceLoader<S> |
load(java.lang.Class<S> service,
java.lang.ClassLoader loader)
Creates a new service loader for the given service type and class loader.
|
static <S> ServiceLoader<S> |
loadInstalled(java.lang.Class<S> service)
Creates a new service loader for the given service type, using the
extension class loader.
|
void |
reload()
Clear this loader's provider cache so that all providers will be reloaded.
|
java.lang.String |
toString()
Returns a string describing this service.
|
private static final java.lang.String SERVICES
private final java.lang.String serviceFile
private java.lang.Class<S> expectedType
private final java.lang.ClassLoader loader
private java.util.Set<S> providers
private ServiceLoader(java.lang.Class<S> service, java.lang.ClassLoader loader)
public static <S> ServiceLoader<S> load(java.lang.Class<S> service)
ServiceLoader.load(service)</code>
is equivalent to
ServiceLoader.load(service,
Thread.currentThread().getContextClassLoader())
service
- The interface or abstract class representing the servicepublic static <S> ServiceLoader<S> load(java.lang.Class<S> service, java.lang.ClassLoader loader)
service
- The interface or abstract class representing the serviceloader
- The class loader to be used to load provider-configuration
files and provider classes, or null if the system class loader
(or, failing that, the bootstrap class loader) is to be usedpublic static <S> ServiceLoader<S> loadInstalled(java.lang.Class<S> service)
ServiceLoader.load(service, extClassLoader)
If the extension class loader cannot be found then the system class loader
is used; if there is no system class loader then the bootstrap class
loader is used.
This method is intended for use when only installed providers are desired.
The resulting service will only find and load providers that have been
installed into the current Java virtual machine; providers on the
application's class path will be ignored.service
- The interface or abstract class representing the servicepublic void reload()
public S createInstance(java.lang.String line) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.NoClassDefFoundError, java.lang.InstantiationException, java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
java.lang.NoClassDefFoundError
java.lang.InstantiationException
java.lang.IllegalAccessException
public java.util.Iterator<S> iterator()
iterator
in interface java.lang.Iterable<S>
public java.util.Set<S> getProviders()
public java.lang.String toString()
toString
in class java.lang.Object