@Immutable public final class Loader extends Object
Constructor and Description |
---|
Loader(ClassLoader loader)
Constructs a new loader which uses the given class loader before using
the current thread context's class loader unless the latter is identical
to the former.
|
Modifier and Type | Method and Description |
---|---|
Class<?> |
classFor(String name)
Loads a class according to the algorithm described in the class Javadoc.
|
<S> S |
instanceOf(Class<S> spec,
Class<? extends S> impl)
Instantiates an implementation class which is named as the value of a
system property with the name of the given specification class as the
key.
|
<S> Iterable<S> |
instancesOf(Class<S> spec)
Returns a new iterable collection of instances of all implementation
classes of the given specification class which are advertised as
services on the class path.
|
static <T> T |
promote(Object object,
Class<T> type)
Promotes the given
object to an instance of the given
type . |
Iterable<URL> |
resourcesFor(String name)
Returns a new iterable collection of URLs for the given resource name.
|
public Loader(@CheckForNull ClassLoader loader)
loader
- the nullable primary class loader.
If this is null
, then the system class loader is used.public Class<?> classFor(String name) throws ServiceConfigurationError
name
- The fully qualified name of the class to classFor.ServiceConfigurationError
- if loading the class fails for some
reason.@Nullable public <S> S instanceOf(Class<S> spec, @CheckForNull Class<? extends S> impl) throws ServiceConfigurationError
null
.
The implementation class gets loaded using classFor(java.lang.String)
and
instantiated by calling its public no-arg constructor.
This method should be preferred over instancesOf(Class)
if
null
as the result or providing a default
service provider implementation is acceptable.
S
- the type of the service.spec
- the specification class of the service.impl
- the default implementation class of the service.null
if no implementation class is known.ServiceConfigurationError
- if loading or instantiating
the implementation class fails for some reason.public <S> Iterable<S> instancesOf(Class<S> spec)
instanceOf(Class, Class)
if more than one meaningful implementation class of a specification
class is expected at run time.
The implementation classes will get instantiated as if by calling
,
where ServiceLoader.load(spec, cl)
.iterator()cl
is the resolved class loader.
S
- the type of the services.spec
- the specification class of the services.@CheckForNull public static <T> T promote(@CheckForNull Object object, Class<T> type) throws IllegalArgumentException
object
to an instance of the given
type
.
The following steps are consecutively applied in order to promote the
given object by this utility method:
object
is an instance of String
and type
is
not the String
class instance, then object
is considered
to name a class, which is loaded by using a new Locator
with the class loader of type
as the primary class loader.
object
is an instance of Class
and type
is not the Class
class instance, then it gets instantiated by
calling it's public no-argument constructor.
object
is cast to T
and
returned.
T
- the desired type of the object.object
- the object to promote.type
- the class describing the desired type.null
if and only if
object
is null
.IllegalArgumentException
- if any promotion step fails.public Iterable<URL> resourcesFor(String name) throws ServiceConfigurationError
name
- The fully qualified name of the resources to locate.ServiceConfigurationError
- if locating the resources fails for
some reason.Copyright © 2012–2018 Schlichtherle IT Services. All rights reserved.