Class Loader


  • public final class Loader
    extends Object
    Provides methods for dynamically loading classes.
    • Method Detail

      • getClassLoader

        public static ClassLoader getClassLoader()
        Returns the class loader that should be used consistently throughout the application.
        Returns:
        the class loader that should be used consistently throughout the application.
      • getClass

        public static Class<?> getClass​(String name)
        Returns the Class object associated with the class or interface with the given name, or null if the class could not be returned for any reason.
        Parameters:
        name - the fully qualified name of the desired class.
        Returns:
        the Class object for the class with the specified name.
      • newInstance

        public static Object newInstance​(String name)
        Creates a new instance of a named class. The class is instantiated as if by a new expression with an empty argument list. If the class cannot be instantiated for any reason, null is returned.
        Parameters:
        name - the fully qualified name of the class to instantiate.
        Returns:
        the newly instantiated object, or null if it could not be instantiated.
      • newInstance

        public static <T> T newInstance​(String name,
                                        Class<T> type)
        Creates a new instance of a named class. The class is instantiated as if by a new expression with an empty argument list. If the class cannot be instantiated for any reason, null is returned.
        Type Parameters:
        T - class type
        Parameters:
        name - the fully qualified name of the class to instantiate.
        type - the class of the type of object to instantiate.
        Returns:
        the newly instantiated object, or null if it could not be instantiated.
      • loadMap

        public static <K,​V extends Indexed<K>> Map<K,​V> loadMap​(Class<V> serviceType)
        Loads services of a particular type into a map. Such services implement the Indexed interface to provide a key to index the service by in the map.
        Type Parameters:
        K - key type
        V - service type
        Parameters:
        serviceType - the class type of services to load.
        Returns:
        a map containing the loaded services, indexed by the services' keys.
      • loadMap

        public static <K,​V extends Indexed<K>> Map<K,​V> loadMap​(Class<V> serviceType,
                                                                            ClassLoader classLoader)
        Loads services of a particular type into a map. Such services implement the Indexed interface to provide a key to index the service by in the map.
        Type Parameters:
        K - key type
        V - service type
        Parameters:
        serviceType - the class type of services to load.
        classLoader - The classLoader to use.
        Returns:
        a map containing the loaded services, indexed by the services' keys.
      • loadList

        public static <E> List<E> loadList​(Class<E> serviceType)
        Loads services of a particular type into a list.
        Type Parameters:
        E - service type
        Parameters:
        serviceType - the class type of services to load.
        Returns:
        a list containing the loaded services.
      • loadList

        public static <E> List<E> loadList​(Class<E> serviceType,
                                           ClassLoader classLoader)
        Loads services of a particular type into a list.
        Type Parameters:
        E - service type
        Parameters:
        serviceType - the class type of services to load.
        classLoader - the classLoader to use.
        Returns:
        a list containing the loaded services.
      • getResource

        public static URL getResource​(String name)
        Finds the resource with the given name.
        Parameters:
        name - the resource name.
        Returns:
        A URL object for reading the resource, or null if the resource could not be found.
        See Also:
        ClassLoader.getResource(java.lang.String)