On 12/24/24 08:54, Alan Bateman wrote:


On 23/12/2024 09:12, PavelTurk wrote:
Hello everyone,

I have `Module module`. If this module is automatic I need to get the list of 
all its services/providers, so,
as I understand the only way to do it is to read its META-INF/services folder. 
I tried:

if (module.getDescriptor().isAutomatic()) {
    ClassLoader classLoader = module.getClassLoader();
    Enumeration<URL> resources = classLoader.getResources("META-INF/services/");
    while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            ...
    }
}

but it doesn't work. All `resource`s have same path ending in 
`....META-INF/service`.
I tried:

    ModuleReference reference = ... ;
    var entries = reference.open().list().collect(Collectors.toList());

It worked, but I need only one folder, but not all with all classes etc. Could 
anyone what is the optimal way to do it?

In your second attempt then you course add a filter it to just the resources 
with names that start with META-INF/services.

It is clear that it is possible to use filter. The question was how to do it 
not iterating ALL entries in jar. That's why I was asking about the optimal way.
Do I understand correctly, that there is no way to do it. If so, maybe it is 
necessary to add method ModuleReference#open(String path)?

In any case, I assume the method you are looking for is 
ModuleDescriptor::provides. It returns the set of Provides objects for service 
providers in the module (it doesn't matter than the module is an automatic 
module).

Thank you. I thought that module descriptor returns providers only for explicit 
modules on the base of `module-info`.

-Alan.

Best regards, Pavel

Reply via email to