Class ClassFileLocator.ForClassLoader

java.lang.Object
net.bytebuddy.dynamic.ClassFileLocator.ForClassLoader
All Implemented Interfaces:
Closeable, AutoCloseable, ClassFileLocator
Enclosing interface:
ClassFileLocator

@Enhance public static class ClassFileLocator.ForClassLoader extends 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.

  • Constructor Details

    • ForClassLoader

      protected ForClassLoader(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 Details

    • 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(@MaybeNull 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(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 Map<Class<?>,byte[]> read(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 Map<Class<?>,byte[]> read(Collection<? extends 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 Map<String,byte[]> readToNames(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 Map<String,byte[]> readToNames(Collection<? extends 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(String name) throws 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:
      IOException - If reading a class file causes an error.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • locate

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