public class ClassLoaderUtil
extends java.lang.Object
The Java reflection framework allows to load classes at runtime
for instance via Class.forName(String). In a configuration-file
configured classes can also be instantiated via the ServiceLoader.
But both approaches work only, if the Class to work with is known
by the current ClassLoader. This is no problem in a normal Java Application
since all classes are managed by the same ClassLoader. But in an
OSGI setup like Eclipse, each component (plug-in) has its own ClassLoader.
The consequence is that the reflection framework can only work with classes
in the same component as they are only known by the current ClassLoader.
To achieve the same behavior in all supported types provides this class utility methods which should be used instead:
getClassforName(String) instead of Class.forName(String)The application specific behavior is implemented in {@link IClassLoader} instances. The used instance is accessible via {@link #getClassLoader()} which is by default the {@link JavaApplicationClassLoader}. In an OSGI (Eclipse) setup the instance is replaced via {@link #setClassLoader(IClassLoader)} with an {@code OSGIClassLoader} instance.
IClassLoader,
JavaApplicationClassLoader| Modifier and Type | Field and Description |
|---|---|
private static IClassLoader |
classLoader
The
IClassLoader instance to use. |
| Constructor and Description |
|---|
ClassLoaderUtil() |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.Class<?> |
getClassforName(java.lang.String className)
Returns the
Class for the given class name similar
to Class.forName(String) but with same behavior in all
supported application types. |
static IClassLoader |
getClassLoader()
Returns the used
IClassLoader. |
static <S> java.lang.Iterable<S> |
loadServices(java.lang.Class<?> contextClass,
java.lang.Class<S> service)
Loads all configured services similar to
ServiceLoader.load(Class)
but with same behavior in all supported application types. |
static <S> java.lang.Iterable<S> |
loadServices(java.lang.Class<S> service)
Loads all configured services similar to
ServiceLoader.load(Class)
but with same behavior in all supported application types. |
static void |
setClassLoader(IClassLoader classLoader)
Sets the
IClassLoader instance to use. |
private static IClassLoader classLoader
IClassLoader instance to use.public static IClassLoader getClassLoader()
IClassLoader.IClassLoader.public static void setClassLoader(IClassLoader classLoader)
IClassLoader instance to use.classLoader - The IClassLoader instance to use.public static java.lang.Class<?> getClassforName(java.lang.String className)
throws java.lang.ClassNotFoundException
Class for the given class name similar
to Class.forName(String) but with same behavior in all
supported application types.className - The name of the class.Class instance.java.lang.ClassNotFoundException - Occurred Exception if Class is not available.public static <S> java.lang.Iterable<S> loadServices(java.lang.Class<S> service)
ServiceLoader.load(Class)
but with same behavior in all supported application types.service - The requested service.Iterable with the created service instances.public static <S> java.lang.Iterable<S> loadServices(java.lang.Class<?> contextClass,
java.lang.Class<S> service)
ServiceLoader.load(Class)
but with same behavior in all supported application types.contextClass - The calling Class which ClassLoader knows the configuration-file.service - The requested service.Iterable with the created service instances.