ServiceLoader.load(Class, ClassLoader) bug

Peter Levart peter.levart at gmail.com
Fri Sep 14 08:20:59 UTC 2012


I reported this as Bug ID: 7198496

Regards, Peter

On 09/13/2012 05:13 PM, Peter Levart wrote:
> 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 <tt>null</tt> 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
>
>
>
>




More information about the core-libs-dev mailing list