Interface ServiceLoaded

All Known Subinterfaces:
TuriClass, TuriFunction, TuriMacro

public interface ServiceLoaded
This helper interface can load classes that implement a specific interface using the service loader. If the service loader does not see the classes (the program sees that zero service instances were loaded), then as a fallback it tries to load the META-INF/services/className files and the classes listed in those.

This is a very general interface, it was copied here from the project Jamal.

  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static <T> List<T>
    getInstances(Class<T> service)
    Load the classes that implement the interface service and are provided by the modules or are available.
    static <T> List<T>
    getInstances(Class<T> service, ClassLoader cl)
    Retrieves a list of instances that implement the specified service interface using the provided class loader.
    static List<URL>
    loadResources(String name, ClassLoader classLoader)
    Get the url list of the resources for the given name.
    static <T> void
    loadViaMetaInf(Class<T> klass, List<T> list, ClassLoader cl)
    Load the classes using the names listed in the META-INF/services/class name resources found by the classloader.
  • Method Details

    • getInstances

      static <T> List<T> getInstances(Class<T> service)
      Load the classes that implement the interface service and are provided by the modules or are available.
      Type Parameters:
      T - the interface
      Parameters:
      service - the interface for which the implementing class instances are needed
      Returns:
      the list of instances
    • getInstances

      static <T> List<T> getInstances(Class<T> service, ClassLoader cl)
      Retrieves a list of instances that implement the specified service interface using the provided class loader. This method attempts to load the implementations through the ServiceLoader API. If no implementations are found, a secondary mechanism is used to try loading via META-INF services resources.
      Type Parameters:
      T - the type of the service interface
      Parameters:
      service - the service interface class for which implementations are to be loaded
      cl - the class loader to use for loading the service implementations
      Returns:
      a list containing instances of classes implementing the specified service interface
    • loadViaMetaInf

      static <T> void loadViaMetaInf(Class<T> klass, List<T> list, ClassLoader cl)
      Load the classes using the names listed in the META-INF/services/class name resources found by the classloader.

      The implementation mimics the behaviour of the class loader using the provider() public static method if it exists in the implementation.

      Class types and assignability is not checked by the method. If there is any discrepancy a class cast exception will occur.

      Type Parameters:
      T - the klass types
      Parameters:
      klass - the interface for which the classes are to be loaded
      list - the list to fill the instances to
    • loadResources

      static List<URL> loadResources(String name, ClassLoader classLoader) throws IOException
      Get the url list of the resources for the given name.
      Parameters:
      name - the name of the resource
      classLoader - the class loader to use or null to use the system class loader
      Returns:
      the list of the urls
      Throws:
      IOException - if the resources cannot be loaded