ServiceLoader.load(Class, ClassLoader) bug (original) (raw)
Peter Levart peter.levart at gmail.com
Thu Sep 13 15:13:39 UTC 2012
- Previous message: hg: jdk8/tl/jdk: 7197637: (ch) sun.nio.ch.Default* cause providers for other platforms to be included in rt.jar
- Next message: ServiceLoader.load(Class, ClassLoader) bug
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi,
The javadoc for java.util.ServiceLoader.load(Class service, ClassLoader loader) method says about it's parameters:
* @param service
* The interface or abstract class representing the service
*
* @param loader
* 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 * used
So one might think that calling:
ServiceLoader.load(service, null);
is equivalent to:
ServiceLoader.load(service, ClassLoader.getSystemClassLoader());
But it is not. Either this is a bug in ServiceLoader or javadoc has to be changed.
If null is specified as ClassLoader then ServiceLoader locates META-INF/services/interface.name resources using system ClassLoader (using ClassLoader.getSystemResources), but loads implementation classes using bootstrap ClassLoader (using Class.forName(className, true, null)).
If this is not the expected behaviour then the fix is simple (in the private constructor of ServiceLoader[217]):
- loader = cl;
- loader = cl == null ? ClassLoader.getSystemClassLoader() : cl;
Regards, Peter
- Previous message: hg: jdk8/tl/jdk: 7197637: (ch) sun.nio.ch.Default* cause providers for other platforms to be included in rt.jar
- Next message: ServiceLoader.load(Class, ClassLoader) bug
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]