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 MethodsModifier and TypeMethodDescriptionstatic <T> List<T> getInstances(Class<T> service) Load the classes that implement the interfaceserviceand 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.loadResources(String name, ClassLoader classLoader) Get the url list of the resources for the given name.static <T> voidloadViaMetaInf(Class<T> klass, List<T> list, ClassLoader cl) Load the classes using the names listed in theMETA-INF/services/class name resources found by the classloader.
-
Method Details
-
getInstances
Load the classes that implement the interfaceserviceand 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
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 loadedcl- the class loader to use for loading the service implementations- Returns:
- a list containing instances of classes implementing the specified service interface
-
loadViaMetaInf
Load the classes using the names listed in theMETA-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 loadedlist- the list to fill the instances to
-
loadResources
Get the url list of the resources for the given name.- Parameters:
name- the name of the resourceclassLoader- the class loader to use ornullto use the system class loader- Returns:
- the list of the urls
- Throws:
IOException- if the resources cannot be loaded
-