ServiceLoader in the JDK

David M. Lloyd david.lloyd at redhat.com
Wed May 23 10:04:12 PDT 2012


On 05/23/2012 11:31 AM, Paul Sandoz wrote:
> Hi,
>
> Here is some proposed changes in the JDK related to service loader
> (not tested yet! just want some easily feedback):
>
> http://cr.openjdk.java.net/~psandoz/jigsaw/jdk-services/webrev/
>
> I have attempted where possible to tidy things up to avoid an
> explicit reference to a class loader for common patterns hence the
> addition of the methods ServiceLoader.loadFromClass,
> ServiceLoader.loadFromSystem and ServiceLoader.loadFromCaller (when
> in module mode most ServiceLoader.load* method do the equivalent of
> the latter). The names could be better.

You really don't want to have loadFromCaller().  We experimented with 
this and found the performance cost to be unjustifiable, especially when 
you consider what a simple matter it is for the user to pass in their 
class loader.  The recommendation that we make to users in ##java as 
well as internally and externally here at JBoss is to always use the 
three-argument form of Class.forName() for this reason as well.

Changing load() to depend on the mode is hazardous!  There is a lot of 
code that assumes it is using TCCL.  You cannot make TCCL go away.  It 
is *well-established* to mean "the current application", of which there 
may be more than one in the VM and it may well be a different module 
than the one calling load() (in fact this is very likely to be the case 
in Java EE).

By depending on mode, the loadFromSystem() semantics are very 
unintuitive.  I would not use this method and would discourage users 
from using it for this reason.

Overall I find this change to be very discouraging, and also very naive.

-- 
- DML



More information about the jigsaw-dev mailing list