Class ClassFileLocator.ForClassLoader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ClassFileLocator
    Enclosing interface:
    ClassFileLocator

    @Enhance
    public static class ClassFileLocator.ForClassLoader
    extends java.lang.Object
    implements ClassFileLocator

    A class file locator that queries a class loader for binary representations of class files.

    Important: Even when calling Closeable.close() on this class file locator, no underlying class loader is closed if it implements the Closeable interface as this is typically not intended.

    • Field Detail

      • BOOT_LOADER_PROXY

        private static final java.lang.ClassLoader BOOT_LOADER_PROXY
        A class loader that does not define resources of its own but allows querying for resources supplied by the boot loader.
      • classLoader

        private final java.lang.ClassLoader classLoader
        The class loader to query.
    • Constructor Detail

      • ForClassLoader

        protected ForClassLoader​(java.lang.ClassLoader classLoader)
        Creates a new class file locator for the given class loader.
        Parameters:
        classLoader - The class loader to query which must not be the bootstrap class loader, i.e. null.
    • Method Detail

      • ofSystemLoader

        public static ClassFileLocator ofSystemLoader()
        Creates a class file locator that queries the system class loader.
        Returns:
        A class file locator that queries the system class loader.
      • ofPlatformLoader

        public static ClassFileLocator ofPlatformLoader()
        Creates a class file locator that queries the plaform class loader or the extension class loader if the current VM is not at least of version 9.
        Returns:
        A class file locator that queries the plaform class loader or the extension class loader.
      • ofBootLoader

        public static ClassFileLocator ofBootLoader()
        Creates a class file locator that queries the boot loader.
        Returns:
        A class file locator that queries the boot loader.
      • of

        public static ClassFileLocator of​(java.lang.ClassLoader classLoader)
        Creates a class file locator for a given class loader.
        Parameters:
        classLoader - The class loader to be used which might be null to represent the bootstrap loader.
        Returns:
        A corresponding source locator.
      • read

        public static byte[] read​(java.lang.Class<?> type)
        Attempts to create a binary representation of a loaded type by requesting data from its ClassLoader.
        Parameters:
        type - The type of interest.
        Returns:
        The binary representation of the supplied type.
      • read

        public static java.util.Map<java.lang.Class<?>,​byte[]> read​(java.lang.Class<?>... type)
        Attempts to create a binary representation of several loaded types by requesting data from their respective ClassLoaders.
        Parameters:
        type - The types of interest.
        Returns:
        A mapping of the supplied types to their binary representation.
      • read

        public static java.util.Map<java.lang.Class<?>,​byte[]> read​(java.util.Collection<? extends java.lang.Class<?>> types)
        Attempts to create a binary representation of several loaded types by requesting data from their respective ClassLoaders.
        Parameters:
        types - The types of interest.
        Returns:
        A mapping of the supplied types to their binary representation.
      • readToNames

        public static java.util.Map<java.lang.String,​byte[]> readToNames​(java.lang.Class<?>... type)
        Attempts to create a binary representation of several loaded types by requesting data from their respective ClassLoaders.
        Parameters:
        type - The types of interest.
        Returns:
        A mapping of the supplied types' names to their binary representation.
      • readToNames

        public static java.util.Map<java.lang.String,​byte[]> readToNames​(java.util.Collection<? extends java.lang.Class<?>> types)
        Attempts to create a binary representation of several loaded types by requesting data from their respective ClassLoaders.
        Parameters:
        types - The types of interest.
        Returns:
        A mapping of the supplied types' names to their binary representation.
      • locate

        public ClassFileLocator.Resolution locate​(java.lang.String name)
                                           throws java.io.IOException
        Locates the class file for a given type and returns the binary data of the class file.
        Specified by:
        locate in interface ClassFileLocator
        Parameters:
        name - The name of the type to locate a class file representation for.
        Returns:
        Any binary representation of the type which might be illegal.
        Throws:
        java.io.IOException - If reading a class file causes an error.
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • locate

        protected static ClassFileLocator.Resolution locate​(java.lang.ClassLoader classLoader,
                                                            java.lang.String name)
                                                     throws java.io.IOException
        Locates the class file for the supplied type by requesting a resource from the class loader.
        Parameters:
        classLoader - The class loader to query for the resource.
        name - The name of the type for which to locate a class file.
        Returns:
        A resolution for the class file.
        Throws:
        java.io.IOException - If reading the class file causes an exception.