Class DynamicType.Default
- All Implemented Interfaces:
DynamicType
- Direct Known Subclasses:
DynamicType.Default.Loaded
,DynamicType.Default.Unloaded
- Enclosing interface:
- DynamicType
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
A default implementation of a loaded dynamic type.static class
A default implementation of an unloaded dynamic type.Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.DynamicType
DynamicType.Builder<T>, DynamicType.Default
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<? extends DynamicType>
A list of auxiliary types for this dynamic type.protected final byte[]
The byte array representing this dynamic type.private static final int
The size of a writing buffer.private static final String
The file name extension for Java class files.private static final int
A convenience representative of anInputStream
's end to improve the readability of the code.private static final int
A convenience index for the beginning of an array to improve the readability of the code.protected final LoadedTypeInitializer
The loaded type initializer for this dynamic type.private static final String
The default version of a jar file manifest.private static final String
A suffix for temporary files.protected final TypeDescription
A type description of this dynamic type. -
Constructor Summary
ConstructorsConstructorDescriptionDefault
(TypeDescription typeDescription, byte[] binaryRepresentation, LoadedTypeInitializer loadedTypeInitializer, List<? extends DynamicType> auxiliaryTypes) Creates a new dynamic type. -
Method Summary
Modifier and TypeMethodDescriptionprivate File
Injects this dynamic type into a source jar and writes the result to the target jar.Map<TypeDescription,
byte[]> Returns all types that are implied by this dynamic type.Map<TypeDescription,
byte[]> Returns a map of all auxiliary types that are required for making use of the main type.byte[]
getBytes()
Returns a byte array representing this dynamic type.Returns a map of all loaded type initializers for the main type and all auxiliary types, if any.Returns a description of this dynamic type.boolean
Checks if a dynamic type requires some form of explicit type initialization, either for itself or for one of its auxiliary types, if any.Injects the types of this dynamic type into a given jar file.Injects the types of this dynamic type into a given jar file.Saves a dynamic type in a given folder using the Java class file format while respecting the naming conventions for saving compiled Java classes.Saves the contents of this dynamic type inside a jar file.Saves the contents of this dynamic type inside a jar file.
-
Field Details
-
CLASS_FILE_EXTENSION
The file name extension for Java class files.- See Also:
-
MANIFEST_VERSION
The default version of a jar file manifest.- See Also:
-
BUFFER_SIZE
private static final int BUFFER_SIZEThe size of a writing buffer.- See Also:
-
FROM_BEGINNING
private static final int FROM_BEGINNINGA convenience index for the beginning of an array to improve the readability of the code.- See Also:
-
END_OF_FILE
private static final int END_OF_FILEA convenience representative of anInputStream
's end to improve the readability of the code.- See Also:
-
TEMP_SUFFIX
A suffix for temporary files.- See Also:
-
typeDescription
A type description of this dynamic type. -
binaryRepresentation
protected final byte[] binaryRepresentationThe byte array representing this dynamic type. -
loadedTypeInitializer
The loaded type initializer for this dynamic type. -
auxiliaryTypes
A list of auxiliary types for this dynamic type.
-
-
Constructor Details
-
Default
public Default(TypeDescription typeDescription, byte[] binaryRepresentation, LoadedTypeInitializer loadedTypeInitializer, List<? extends DynamicType> auxiliaryTypes) Creates a new dynamic type.- Parameters:
typeDescription
- A description of this dynamic type.binaryRepresentation
- A byte array containing the binary representation of this dynamic type. The array must not be modified.loadedTypeInitializer
- The loaded type initializer of this dynamic type.auxiliaryTypes
- The auxiliary type required for this dynamic type.
-
-
Method Details
-
getTypeDescription
Returns a description of this dynamic type.
Note: This description will most likely differ from the binary representation of this type. Normally, annotations and intercepted methods are not added to this type description.
- Specified by:
getTypeDescription
in interfaceDynamicType
- Returns:
- A description of this dynamic type.
-
getAllTypes
Returns all types that are implied by this dynamic type.- Specified by:
getAllTypes
in interfaceDynamicType
- Returns:
- A mapping from all type descriptions, the actual type and its auxiliary types to their binary representation
-
getLoadedTypeInitializers
Returns a map of all loaded type initializers for the main type and all auxiliary types, if any.
Note: The type descriptions will most likely differ from the binary representation of this type. Normally, annotations and intercepted methods are not added to the type descriptions of auxiliary types.
- Specified by:
getLoadedTypeInitializers
in interfaceDynamicType
- Returns:
- A mapping of all types' descriptions to their loaded type initializers.
-
hasAliveLoadedTypeInitializers
public boolean hasAliveLoadedTypeInitializers()Checks if a dynamic type requires some form of explicit type initialization, either for itself or for one of its auxiliary types, if any. This is the case when this dynamic type was defined to delegate method calls to a specific instance which is stored in a field of the created type. If this class serialized, it could not be used without its loaded type initializers since the field value represents a specific runtime context.- Specified by:
hasAliveLoadedTypeInitializers
in interfaceDynamicType
- Returns:
true
if this type requires explicit type initialization.
-
getBytes
public byte[] getBytes()Returns a byte array representing this dynamic type. This byte array might be reused by this dynamic type and must therefore not be altered.- Specified by:
getBytes
in interfaceDynamicType
- Returns:
- A byte array of the type's binary representation.
-
getAuxiliaryTypes
Returns a map of all auxiliary types that are required for making use of the main type.
Note: The type descriptions will most likely differ from the binary representation of this type. Normally, annotations and intercepted methods are not added to the type descriptions of auxiliary types.
- Specified by:
getAuxiliaryTypes
in interfaceDynamicType
- Returns:
- A map of all auxiliary types by their descriptions to their binary representation.
-
saveIn
Saves a dynamic type in a given folder using the Java class file format while respecting the naming conventions for saving compiled Java classes. All auxiliary types, if any, are saved in the same directory. The resulting folder structure will resemble the structure that is required for Java run times, i.e. each folder representing a segment of the package name. If the specified
folder
does not yet exist, it is created during the call of this method.Note: The type descriptions will most likely differ from the binary representation of this type. Normally, annotations and intercepted methods are not added to the type descriptions of auxiliary types.
- Specified by:
saveIn
in interfaceDynamicType
- Parameters:
folder
- The base target folder for storing this dynamic type and its auxiliary types, if any.- Returns:
- A map of type descriptions pointing to files with their stored binary representations within
folder
. - Throws:
IOException
- Thrown if the underlying file operations cause anIOException
.
-
inject
Injects the types of this dynamic type into a given jar file. Any pre-existent type with the same name is overridden during injection. The resulting jar is going to be a recreation of the original jar and not a patched version with a new central directory. No directory entries are added to the generated jar.- Specified by:
inject
in interfaceDynamicType
- Parameters:
sourceJar
- The original jar file.targetJar
- Thesource
jar file with the injected contents.- Returns:
- The
target
jar file. - Throws:
IOException
- If an I/O exception occurs while injecting from the source into the target.
-
inject
Injects the types of this dynamic type into a given jar file. Any pre-existent type with the same name is overridden during injection. The resulting jar is going to be a recreation of the original jar and not a patched version with a new central directory. No directory entries are added to the generated jar.- Specified by:
inject
in interfaceDynamicType
- Parameters:
jar
- The jar file to replace with an injected version.- Returns:
- The
jar
file. - Throws:
IOException
- If an I/O exception occurs while injecting into the jar.
-
doInject
Injects this dynamic type into a source jar and writes the result to the target jar.- Parameters:
sourceJar
- The source jar.targetJar
- The target jar.- Returns:
- The jar file that was written to.
- Throws:
IOException
- If an I/O error occurs.
-
toJar
Saves the contents of this dynamic type inside a jar file. The folder of the givenfile
must exist prior to calling this method. The jar file is created with a simple manifest that only contains a version number. No directory entries are added to the generated jar.- Specified by:
toJar
in interfaceDynamicType
- Parameters:
file
- The target file to which the jar is written to.- Returns:
- The given
file
. - Throws:
IOException
- If an I/O exception occurs while writing the file.
-
toJar
Saves the contents of this dynamic type inside a jar file. The folder of the givenfile
must exist prior to calling this method. No directory entries are added to the generated jar.- Specified by:
toJar
in interfaceDynamicType
- Parameters:
file
- The target file to which the jar is written to.manifest
- The manifest of the created jar.- Returns:
- The given
file
. - Throws:
IOException
- If an I/O exception occurs while writing the file.
-