Patching serviceloader

Paulo Levi i30817 at
Tue Jul 7 01:28:44 UTC 2009

I'd like to patch service loader to disable caching of services, but i
didn't sign the SUN agreement, or platform to test a jdk change. I suppose
jdk classes can be tested with the Xbootclasspath option right? (instead of
the whole project).

The reload method appears to already do this, but it needs to be called on
each iteration of ServiceProvider. It also re-reads the meta-inf/service
files. I would just like that each iterator would always return a new
object, and never read the service files more than once.

The options are creating 3 more static factory methods (so that
ServiceLoader is still effectily immutable).

Regardless, my suggestion to change the iterator would be this, if you could
remove the duplication, it would be nice too :

    public Iterator<S> iterator() {
    return new Iterator<S>() {

        Iterator<Map.Entry<String,S>> knownProviders
        = providers.entrySet().iterator();

        public boolean hasNext() {
        if (knownProviders.hasNext())
            return true;
        return lookupIterator.hasNext();

        public S next() {
        if (knownProviders.hasNext())
            return newInstance(;

            private S newInstance(S object){
                  return object;
                String className = object.getClass().getCanonicalName();
                try {
                  return service.cast(Class.forName(className, true,
                } catch (ClassNotFoundException x) {
                    fail(service, "Provider " + className + " not found");
                } catch (Throwable x) {
                    fail(service, "Provider " + className + " could not be
instantiated: " + x, x);

        public void remove() {
        throw new UnsupportedOperationException();


the cached variable is a final one set on the static function factories.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the core-libs-dev mailing list