Interface ClassFile
public sealed interface ClassFile
Represents a context for parsing, transforming, and generating classfiles.
A
ClassFile
has a set of options that condition how parsing and
generation is done.- Since:
- 24
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Option describing attribute mappers for custom attributes.static enum
Option describing whether to process or discard unrecognized or problematic original attributes when a class, record component, field, method or code is transformed in its exploded form.static interface
Option describing the class hierarchy resolver to use when generating stack maps.static enum
Option describing whether to preserve the original constant pool when transforming a classfile.static enum
Option describing whether to patch out unreachable code.static enum
Option describing whether to filter unresolved labels.static enum
Option describing whether to process or discard debug elements.static enum
Option describing whether to process or discard line numbers.static interface
An option that affects the parsing and writing of classfiles.static enum
Option describing whether to automatically rewrite short jumps to long when necessary.static enum
Option describing whether to generate stackmaps. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The bit mask of ABSTRACT access and property modifier.static final int
The bit mask of ANNOTATION access and property modifier.static final int
The bit mask of BRIDGE access and property modifier.static final int
The bit mask of ENUM access and property modifier.static final int
The bit mask of FINAL access and property modifier.static final int
The bit mask of INTERFACE access and property modifier.static final int
The bit mask of MANDATED access and property modifier.static final int
The bit mask of MODULE access and property modifier.static final int
The bit mask of NATIVE access and property modifier.static final int
The bit mask of OPEN access and property modifier.static final int
The bit mask of PRIVATE access and property modifier.static final int
The bit mask of PROTECTED access and property modifier.static final int
The bit mask of PUBLIC access and property modifier.static final int
The bit mask of STATIC access and property modifier.static final int
The bit mask of STATIC_PHASE access and property modifier.static final int
The bit mask of STRICT access and property modifier.static final int
The bit mask of SUPER access and property modifier.static final int
The bit mask of SYNCHRONIZED access and property modifier.static final int
The bit mask of SYNTHETIC access and property modifier.static final int
The bit mask of TRANSIENT access and property modifier.static final int
The bit mask of TRANSITIVE access and property modifier.static final int
The bit mask of VARARGS access and property modifier.static final int
The bit mask of VOLATILE access and property modifier.static final int
The class major version of JAVA_1.static final int
The class major version of JAVA_10.static final int
The class major version of JAVA_11.static final int
The class major version of JAVA_12.static final int
The class major version of JAVA_13.static final int
The class major version of JAVA_14.static final int
The class major version of JAVA_15.static final int
The class major version of JAVA_16.static final int
The class major version of JAVA_17.static final int
The class major version of JAVA_18.static final int
The class major version of JAVA_19.static final int
The class major version of JAVA_2.static final int
The class major version of JAVA_20.static final int
The class major version of JAVA_21.static final int
The class major version of JAVA_22.static final int
The class major version of JAVA_23.static final int
The class major version of JAVA_24.static final int
The class major version of JAVA_3.static final int
The class major version of JAVA_4.static final int
The class major version of JAVA_5.static final int
The class major version of JAVA_6.static final int
The class major version of JAVA_7.static final int
The class major version of JAVA_8.static final int
The class major version of JAVA_9.static final int
0xCAFEBABEstatic final int
A minor version number indicating a class uses preview features of a Java SE version since 12, for major versions 56 and above. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
build
(ClassEntry thisClassEntry, ConstantPoolBuilder constantPool, Consumer<? super ClassBuilder> handler) Build a classfile into a byte array using the provided constant pool builder.default byte[]
build
(ClassDesc thisClass, Consumer<? super ClassBuilder> handler) Build a classfile into a byte array.default byte[]
buildModule
(ModuleAttribute moduleAttribute) Build a module descriptor into a byte array.default byte[]
buildModule
(ModuleAttribute moduleAttribute, Consumer<? super ClassBuilder> handler) Build a module descriptor into a byte array.default void
buildModuleTo
(Path path, ModuleAttribute moduleAttribute) Build a module descriptor into a file.default void
buildModuleTo
(Path path, ModuleAttribute moduleAttribute, Consumer<? super ClassBuilder> handler) Build a module descriptor into a file.default void
buildTo
(Path path, ClassEntry thisClassEntry, ConstantPoolBuilder constantPool, Consumer<? super ClassBuilder> handler) Build a classfile into a file using the provided constant pool builder.default void
buildTo
(Path path, ClassDesc thisClass, Consumer<ClassBuilder> handler) Build a classfile into a file.static int
Returns the latest major Java version.static int
Returns the latest minor Java version.static ClassFile
of()
Returns a context with default options.static ClassFile
of
(ClassFile.Option... options) Returns a new context with options altered from the default.parse
(byte[] bytes) Parse a classfile into aClassModel
.default ClassModel
Parse a classfile into aClassModel
.default byte[]
transformClass
(ClassModel model, ClassTransform transform) Transform one classfile into a new classfile with the aid of aClassTransform
.byte[]
transformClass
(ClassModel model, ClassEntry newClassName, ClassTransform transform) Transform one classfile into a new classfile with the aid of aClassTransform
.default byte[]
transformClass
(ClassModel model, ClassDesc newClassName, ClassTransform transform) Transform one classfile into a new classfile with the aid of aClassTransform
.verify
(byte[] bytes) Verify a classfile.verify
(ClassModel model) Verify a classfile.default List
<VerifyError> Verify a classfile.withOptions
(ClassFile.Option... options) Returns a copy of the context with altered options.
-
Field Details
-
MAGIC_NUMBER
static final int MAGIC_NUMBER0xCAFEBABE- See Also:
-
ACC_PUBLIC
static final int ACC_PUBLICThe bit mask of PUBLIC access and property modifier.- See Also:
-
ACC_PROTECTED
static final int ACC_PROTECTEDThe bit mask of PROTECTED access and property modifier.- See Also:
-
ACC_PRIVATE
static final int ACC_PRIVATEThe bit mask of PRIVATE access and property modifier.- See Also:
-
ACC_INTERFACE
static final int ACC_INTERFACEThe bit mask of INTERFACE access and property modifier.- See Also:
-
ACC_ENUM
static final int ACC_ENUMThe bit mask of ENUM access and property modifier.- See Also:
-
ACC_ANNOTATION
static final int ACC_ANNOTATIONThe bit mask of ANNOTATION access and property modifier.- See Also:
-
ACC_SUPER
static final int ACC_SUPERThe bit mask of SUPER access and property modifier.- See Also:
-
ACC_ABSTRACT
static final int ACC_ABSTRACTThe bit mask of ABSTRACT access and property modifier.- See Also:
-
ACC_VOLATILE
static final int ACC_VOLATILEThe bit mask of VOLATILE access and property modifier.- See Also:
-
ACC_TRANSIENT
static final int ACC_TRANSIENTThe bit mask of TRANSIENT access and property modifier.- See Also:
-
ACC_SYNTHETIC
static final int ACC_SYNTHETICThe bit mask of SYNTHETIC access and property modifier.- See Also:
-
ACC_STATIC
static final int ACC_STATICThe bit mask of STATIC access and property modifier.- See Also:
-
ACC_FINAL
static final int ACC_FINALThe bit mask of FINAL access and property modifier.- See Also:
-
ACC_SYNCHRONIZED
static final int ACC_SYNCHRONIZEDThe bit mask of SYNCHRONIZED access and property modifier.- See Also:
-
ACC_BRIDGE
static final int ACC_BRIDGEThe bit mask of BRIDGE access and property modifier.- See Also:
-
ACC_VARARGS
static final int ACC_VARARGSThe bit mask of VARARGS access and property modifier.- See Also:
-
ACC_NATIVE
static final int ACC_NATIVEThe bit mask of NATIVE access and property modifier.- See Also:
-
ACC_STRICT
static final int ACC_STRICTThe bit mask of STRICT access and property modifier.- See Also:
-
ACC_MODULE
static final int ACC_MODULEThe bit mask of MODULE access and property modifier.- See Also:
-
ACC_OPEN
static final int ACC_OPENThe bit mask of OPEN access and property modifier.- See Also:
-
ACC_MANDATED
static final int ACC_MANDATEDThe bit mask of MANDATED access and property modifier.- See Also:
-
ACC_TRANSITIVE
static final int ACC_TRANSITIVEThe bit mask of TRANSITIVE access and property modifier.- See Also:
-
ACC_STATIC_PHASE
static final int ACC_STATIC_PHASEThe bit mask of STATIC_PHASE access and property modifier.- See Also:
-
JAVA_1_VERSION
static final int JAVA_1_VERSIONThe class major version of JAVA_1.- See Also:
-
JAVA_2_VERSION
static final int JAVA_2_VERSIONThe class major version of JAVA_2.- See Also:
-
JAVA_3_VERSION
static final int JAVA_3_VERSIONThe class major version of JAVA_3.- See Also:
-
JAVA_4_VERSION
static final int JAVA_4_VERSIONThe class major version of JAVA_4.- See Also:
-
JAVA_5_VERSION
static final int JAVA_5_VERSIONThe class major version of JAVA_5.- See Also:
-
JAVA_6_VERSION
static final int JAVA_6_VERSIONThe class major version of JAVA_6.- See Also:
-
JAVA_7_VERSION
static final int JAVA_7_VERSIONThe class major version of JAVA_7.- See Also:
-
JAVA_8_VERSION
static final int JAVA_8_VERSIONThe class major version of JAVA_8.- See Also:
-
JAVA_9_VERSION
static final int JAVA_9_VERSIONThe class major version of JAVA_9.- See Also:
-
JAVA_10_VERSION
static final int JAVA_10_VERSIONThe class major version of JAVA_10.- See Also:
-
JAVA_11_VERSION
static final int JAVA_11_VERSIONThe class major version of JAVA_11.- See Also:
-
JAVA_12_VERSION
static final int JAVA_12_VERSIONThe class major version of JAVA_12.- See Also:
-
JAVA_13_VERSION
static final int JAVA_13_VERSIONThe class major version of JAVA_13.- See Also:
-
JAVA_14_VERSION
static final int JAVA_14_VERSIONThe class major version of JAVA_14.- See Also:
-
JAVA_15_VERSION
static final int JAVA_15_VERSIONThe class major version of JAVA_15.- See Also:
-
JAVA_16_VERSION
static final int JAVA_16_VERSIONThe class major version of JAVA_16.- See Also:
-
JAVA_17_VERSION
static final int JAVA_17_VERSIONThe class major version of JAVA_17.- See Also:
-
JAVA_18_VERSION
static final int JAVA_18_VERSIONThe class major version of JAVA_18.- See Also:
-
JAVA_19_VERSION
static final int JAVA_19_VERSIONThe class major version of JAVA_19.- See Also:
-
JAVA_20_VERSION
static final int JAVA_20_VERSIONThe class major version of JAVA_20.- See Also:
-
JAVA_21_VERSION
static final int JAVA_21_VERSIONThe class major version of JAVA_21.- See Also:
-
JAVA_22_VERSION
static final int JAVA_22_VERSIONThe class major version of JAVA_22.- See Also:
-
JAVA_23_VERSION
static final int JAVA_23_VERSIONThe class major version of JAVA_23.- See Also:
-
JAVA_24_VERSION
static final int JAVA_24_VERSIONThe class major version of JAVA_24.- See Also:
-
PREVIEW_MINOR_VERSION
static final int PREVIEW_MINOR_VERSIONA minor version number indicating a class uses preview features of a Java SE version since 12, for major versions 56 and above.- See Also:
-
-
Method Details
-
of
Returns a context with default options.- Returns:
- a context with default options
-
of
Returns a new context with options altered from the default.- Parameters:
options
- the desired processing options- Returns:
- a new context with options altered from the default
-
withOptions
Returns a copy of the context with altered options.- Parameters:
options
- the desired processing options- Returns:
- a copy of the context with altered options
-
parse
Parse a classfile into aClassModel
.- Parameters:
bytes
- the bytes of the classfile- Returns:
- the class model
- Throws:
IllegalArgumentException
- or its subclass if the classfile format is not supported or an incompatibility prevents parsing of the classfile
-
parse
Parse a classfile into aClassModel
.- Parameters:
path
- the path to the classfile- Returns:
- the class model
- Throws:
IOException
- if an I/O error occursIllegalArgumentException
- or its subclass if the classfile format is not supported or an incompatibility prevents parsing of the classfile
-
build
Build a classfile into a byte array.- Parameters:
thisClass
- the name of the class to buildhandler
- a handler that receives aClassBuilder
- Returns:
- the classfile bytes
- Throws:
IllegalArgumentException
- ifthisClass
represents a primitive type
-
build
byte[] build(ClassEntry thisClassEntry, ConstantPoolBuilder constantPool, Consumer<? super ClassBuilder> handler) Build a classfile into a byte array using the provided constant pool builder.- Parameters:
thisClassEntry
- the name of the class to buildconstantPool
- the constant pool builderhandler
- a handler that receives aClassBuilder
- Returns:
- the classfile bytes
-
buildTo
default void buildTo(Path path, ClassDesc thisClass, Consumer<ClassBuilder> handler) throws IOException Build a classfile into a file.- Parameters:
path
- the path to the file to writethisClass
- the name of the class to buildhandler
- a handler that receives aClassBuilder
- Throws:
IOException
- if an I/O error occurs
-
buildTo
default void buildTo(Path path, ClassEntry thisClassEntry, ConstantPoolBuilder constantPool, Consumer<? super ClassBuilder> handler) throws IOException Build a classfile into a file using the provided constant pool builder.- Parameters:
path
- the path to the file to writethisClassEntry
- the name of the class to buildconstantPool
- the constant pool builderhandler
- a handler that receives aClassBuilder
- Throws:
IOException
- if an I/O error occurs
-
buildModule
Build a module descriptor into a byte array.- Parameters:
moduleAttribute
- theModule
attribute- Returns:
- the classfile bytes
-
buildModule
Build a module descriptor into a byte array.- Parameters:
moduleAttribute
- theModule
attributehandler
- a handler that receives aClassBuilder
- Returns:
- the classfile bytes
-
buildModuleTo
Build a module descriptor into a file.- Parameters:
path
- the file to writemoduleAttribute
- theModule
attribute- Throws:
IOException
- if an I/O error occurs
-
buildModuleTo
default void buildModuleTo(Path path, ModuleAttribute moduleAttribute, Consumer<? super ClassBuilder> handler) throws IOException Build a module descriptor into a file.- Parameters:
path
- the file to writemoduleAttribute
- theModule
attributehandler
- a handler that receives aClassBuilder
- Throws:
IOException
- if an I/O error occurs
-
transformClass
Transform one classfile into a new classfile with the aid of aClassTransform
. The transform will receive each element of this class, as well as aClassBuilder
for building the new class. The transform is free to preserve, remove, or replace elements as it sees fit.- Implementation Note:
- This method behaves as if:
this.build(model.thisClass(), ConstantPoolBuilder.of(model), clb -> clb.transform(model, transform));
- Parameters:
model
- the class model to transformtransform
- the transform- Returns:
- the bytes of the new class
-
transformClass
Transform one classfile into a new classfile with the aid of aClassTransform
. The transform will receive each element of this class, as well as aClassBuilder
for building the new class. The transform is free to preserve, remove, or replace elements as it sees fit.- Parameters:
model
- the class model to transformnewClassName
- new class nametransform
- the transform- Returns:
- the bytes of the new class
-
transformClass
Transform one classfile into a new classfile with the aid of aClassTransform
. The transform will receive each element of this class, as well as aClassBuilder
for building the new class. The transform is free to preserve, remove, or replace elements as it sees fit.- Implementation Note:
- This method behaves as if:
this.build(newClassName, ConstantPoolBuilder.of(model), clb -> clb.transform(model, transform));
- Parameters:
model
- the class model to transformnewClassName
- new class nametransform
- the transform- Returns:
- the bytes of the new class
-
verify
Verify a classfile. Any verification errors found will be returned.- Parameters:
model
- the class model to verify- Returns:
- a list of verification errors, or an empty list if no errors are found
-
verify
Verify a classfile. Any verification errors found will be returned.- Parameters:
bytes
- the classfile bytes to verify- Returns:
- a list of verification errors, or an empty list if no errors are found
-
verify
Verify a classfile. Any verification errors found will be returned.- Parameters:
path
- the classfile path to verify- Returns:
- a list of verification errors, or an empty list if no errors are found
- Throws:
IOException
- if an I/O error occurs
-
latestMajorVersion
static int latestMajorVersion()Returns the latest major Java version.- Returns:
- the latest major Java version
-
latestMinorVersion
static int latestMinorVersion()Returns the latest minor Java version.- Returns:
- the latest minor Java version
-