Example code in JDK8 requiring change for modular function
paul.sandoz at oracle.com
Mon May 7 00:45:50 PDT 2012
On May 7, 2012, at 9:31 AM, Alan Bateman wrote:
> On 07/05/2012 04:36, David Holmes wrote:
>> On 7/05/2012 12:17 AM, Alan Bateman wrote:
>>> On 06/05/2012 13:36, David Holmes wrote:
>>>>> When running in "modular mode" the system class loader is the class
>>>>> loader of the entry module, where as what we really want to use is
>>>>> the class loader of the callee (the class loader used to load Files).
>>>> The system class loader needed not be the classloader of the callee.
>>>> So if we expect this to use the loader of the callee then the code is
>>>> broken regardless of module-mode.
>>> This code works as specified, if you have a custom file type detector
>>> then you put it on the system class path and it will be located.
>> Okay - then the expectation is _not_ to use the loader of the callee at all. That's fine.
> I think caller vs. callee has got a bit confused in this discussion. For ServiceLoader usages then the important code is the code that is invoking ServiceLoader. So assume we are running with modules and the application invokes Files.probeContentType to probe the type of a file. This gives us application -> Files.probeContentType -> ... Files$FileTypeDetectors$1.loadInstalledDetectors -> ServiceLoader. If the code is changed to use loadInstalled (one of the choices, not necessarily the best choice here)
Given that it has not been decided yet whether all classes should return a non-null classloader or only those not in base i think it best practice to try and avoid explicit reference to a classloader if possible. A downside is it's less efficient.
> then ServiceLoader.loadInstalled will need to look at the caller frame. In this case the caller (of ServiceLoader.loadInstalled) is in the base module and the base module will have declared that it requires service java.nio.file.spi.FileTypeDetector. At configuration time then the base module will have been linked with all modules that provide implementations of the service so everything works as expected. Hopefully this is clearer now.
More information about the jigsaw-dev