Package net.bytebuddy.dynamic
Class ClassFileLocator.ForClassLoader
- java.lang.Object
-
- net.bytebuddy.dynamic.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 theCloseable
interface as this is typically not intended.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
ClassFileLocator.ForClassLoader.BootLoaderProxyCreationAction
A privileged action for creating a proxy class loader for the boot class loader.static class
ClassFileLocator.ForClassLoader.WeaklyReferenced
A class file locator that queries a class loader for binary representations of class files.-
Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.ClassFileLocator
ClassFileLocator.AgentBased, ClassFileLocator.Compound, ClassFileLocator.ForClassLoader, ClassFileLocator.ForFolder, ClassFileLocator.ForJarFile, ClassFileLocator.ForModule, ClassFileLocator.ForModuleFile, ClassFileLocator.ForUrl, ClassFileLocator.NoOp, ClassFileLocator.PackageDiscriminating, ClassFileLocator.Resolution, ClassFileLocator.Simple
-
-
Field Summary
Fields Modifier and Type Field Description private static 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.private java.lang.ClassLoader
classLoader
The class loader to query.-
Fields inherited from interface net.bytebuddy.dynamic.ClassFileLocator
CLASS_FILE_EXTENSION
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ForClassLoader(java.lang.ClassLoader classLoader)
Creates a new class file locator for the given class loader.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
protected static ClassFileLocator.Resolution
locate(java.lang.ClassLoader classLoader, java.lang.String name)
Locates the class file for the supplied type by requesting a resource from the class loader.ClassFileLocator.Resolution
locate(java.lang.String name)
Locates the class file for a given type and returns the binary data of the class file.static ClassFileLocator
of(java.lang.ClassLoader classLoader)
Creates a class file locator for a given class loader.static ClassFileLocator
ofBootLoader()
Creates a class file locator that queries the boot loader.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.static ClassFileLocator
ofSystemLoader()
Creates a class file locator that queries the system class loader.static byte[]
read(java.lang.Class<?> type)
Attempts to create a binary representation of a loaded type by requesting data from itsClassLoader
.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 respectiveClassLoader
s.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 respectiveClassLoader
s.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 respectiveClassLoader
s.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 respectiveClassLoader
s.
-
-
-
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 benull
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 itsClassLoader
.- 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 respectiveClassLoader
s.- 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 respectiveClassLoader
s.- 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 respectiveClassLoader
s.- 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 respectiveClassLoader
s.- 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 interfaceClassFileLocator
- 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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.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.
-
-