Interface ClassFile
public sealed interface ClassFile
ClassFile is a preview API of the Java platform.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
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:
- 22
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfacePreview.Option describing attribute mappers for custom attributes.static enumPreview.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 interfacePreview.Option describing the class hierarchy resolver to use when generating stack maps.static enumPreview.Option describing whether to preserve the original constant pool when transforming a classfile.static enumPreview.Option describing whether to patch out unreachable code.static enumPreview.Option describing whether to filter unresolved labels.static enumPreview.Option describing whether to process or discard debug elements.static enumPreview.Option describing whether to process or discard line numbers.static interfacePreview.An option that affects the parsing and writing of classfiles.static enumPreview.Option describing whether to automatically rewrite short jumps to long when necessary.static enumPreview.Option describing whether to generate stackmaps. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe bit mask of ABSTRACT access and property modifier.static final intThe bit mask of ANNOTATION access and property modifier.static final intThe bit mask of BRIDGE access and property modifier.static final intThe bit mask of ENUM access and property modifier.static final intThe bit mask of FINAL access and property modifier.static final intThe bit mask of INTERFACE access and property modifier.static final intThe bit mask of MANDATED access and property modifier.static final intThe bit mask of MODULE access and property modifier.static final intThe bit mask of NATIVE access and property modifier.static final intThe bit mask of OPEN access and property modifier.static final intThe bit mask of PRIVATE access and property modifier.static final intThe bit mask of PROTECTED access and property modifier.static final intThe bit mask of PUBLIC access and property modifier.static final intThe bit mask of STATIC access and property modifier.static final intThe bit mask of STATIC_PHASE access and property modifier.static final intThe bit mask of STRICT access and property modifier.static final intThe bit mask of SUPER access and property modifier.static final intThe bit mask of SYNCHRONIZED access and property modifier.static final intThe bit mask of SYNTHETIC access and property modifier.static final intThe bit mask of TRANSIENT access and property modifier.static final intThe bit mask of TRANSITIVE access and property modifier.static final intThe bit mask of VARARGS access and property modifier.static final intThe bit mask of VOLATILE access and property modifier.static final intThe class major version of JAVA_1.static final intThe class major version of JAVA_10.static final intThe class major version of JAVA_11.static final intThe class major version of JAVA_12.static final intThe class major version of JAVA_13.static final intThe class major version of JAVA_14.static final intThe class major version of JAVA_15.static final intThe class major version of JAVA_16.static final intThe class major version of JAVA_17.static final intThe class major version of JAVA_18.static final intThe class major version of JAVA_19.static final intThe class major version of JAVA_2.static final intThe class major version of JAVA_20.static final intThe class major version of JAVA_21.static final intThe class major version of JAVA_22.static final intThe class major version of JAVA_23.static final intThe class major version of JAVA_24.static final intThe class major version of JAVA_3.static final intThe class major version of JAVA_4.static final intThe class major version of JAVA_5.static final intThe class major version of JAVA_6.static final intThe class major version of JAVA_7.static final intThe class major version of JAVA_8.static final intThe class major version of JAVA_9.static final int0xCAFEBABEstatic final intA 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(ClassEntryPREVIEW thisClassEntry, ConstantPoolBuilderPREVIEW constantPool, Consumer<? super ClassBuilderPREVIEW> handler) Build a classfile into a byte array using the provided constant pool builder.default byte[]build(ClassDesc thisClass, Consumer<? super ClassBuilderPREVIEW> handler) Build a classfile into a byte array.default byte[]buildModule(ModuleAttributePREVIEW moduleAttribute) Build a module descriptor into a byte array.default byte[]buildModule(ModuleAttributePREVIEW moduleAttribute, Consumer<? super ClassBuilderPREVIEW> handler) Build a module descriptor into a byte array.default voidbuildModuleTo(Path path, ModuleAttributePREVIEW moduleAttribute) Build a module descriptor into a file.default voidbuildModuleTo(Path path, ModuleAttributePREVIEW moduleAttribute, Consumer<? super ClassBuilderPREVIEW> handler) Build a module descriptor into a file.default voidbuildTo(Path path, ClassEntryPREVIEW thisClassEntry, ConstantPoolBuilderPREVIEW constantPool, Consumer<? super ClassBuilderPREVIEW> handler) Build a classfile into a file using the provided constant pool builder.default voidBuild a classfile into a file.static intReturns the latest major Java version.static intReturns the latest minor Java version.of()Returns a context with default options.of(ClassFile.OptionPREVIEW... options) Returns a new context with options altered from the default.parse(byte[] bytes) Parse a classfile into aClassModelPREVIEW.default ClassModelPREVIEWParse a classfile into aClassModelPREVIEW.default byte[]transformClass(ClassModelPREVIEW model, ClassTransformPREVIEW transform) Transform one classfile into a new classfile with the aid of aClassTransformPREVIEW.byte[]transformClass(ClassModelPREVIEW model, ClassEntryPREVIEW newClassName, ClassTransformPREVIEW transform) Transform one classfile into a new classfile with the aid of aClassTransformPREVIEW.default byte[]transformClass(ClassModelPREVIEW model, ClassDesc newClassName, ClassTransformPREVIEW transform) Transform one classfile into a new classfile with the aid of aClassTransformPREVIEW.verify(byte[] bytes) Verify a classfile.verify(ClassModelPREVIEW model) Verify a classfile.default List<VerifyError> Verify a classfile.withOptions(ClassFile.OptionPREVIEW... 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.- Since:
- 23
- See Also:
-
JAVA_24_VERSION
static final int JAVA_24_VERSIONThe class major version of JAVA_24.- Since:
- 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
-
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 aClassModelPREVIEW.- 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 aClassModelPREVIEW.- 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 aClassBuilderPREVIEW- Returns:
- the classfile bytes
- Throws:
IllegalArgumentException- ifthisClassrepresents a primitive type
-
build
byte[] build(ClassEntryPREVIEW thisClassEntry, ConstantPoolBuilderPREVIEW constantPool, Consumer<? super ClassBuilderPREVIEW> 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 aClassBuilderPREVIEW- Returns:
- the classfile bytes
-
buildTo
default void buildTo(Path path, ClassDesc thisClass, Consumer<ClassBuilderPREVIEW> 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 aClassBuilderPREVIEW- Throws:
IOException- if an I/O error occurs
-
buildTo
default void buildTo(Path path, ClassEntryPREVIEW thisClassEntry, ConstantPoolBuilderPREVIEW constantPool, Consumer<? super ClassBuilderPREVIEW> 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 aClassBuilderPREVIEW- Throws:
IOException- if an I/O error occurs
-
buildModule
Build a module descriptor into a byte array.- Parameters:
moduleAttribute- theModuleattribute- Returns:
- the classfile bytes
-
buildModule
default byte[] buildModule(ModuleAttributePREVIEW moduleAttribute, Consumer<? super ClassBuilderPREVIEW> handler) Build a module descriptor into a byte array.- Parameters:
moduleAttribute- theModuleattributehandler- a handler that receives aClassBuilderPREVIEW- Returns:
- the classfile bytes
-
buildModuleTo
Build a module descriptor into a file.- Parameters:
path- the file to writemoduleAttribute- theModuleattribute- Throws:
IOException- if an I/O error occurs
-
buildModuleTo
default void buildModuleTo(Path path, ModuleAttributePREVIEW moduleAttribute, Consumer<? super ClassBuilderPREVIEW> handler) throws IOException Build a module descriptor into a file.- Parameters:
path- the file to writemoduleAttribute- theModuleattributehandler- a handler that receives aClassBuilderPREVIEW- Throws:
IOException- if an I/O error occurs
-
transformClass
Transform one classfile into a new classfile with the aid of aClassTransformPREVIEW. The transform will receive each element of this class, as well as aClassBuilderPREVIEW 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
default byte[] transformClass(ClassModelPREVIEW model, ClassDesc newClassName, ClassTransformPREVIEW transform) Transform one classfile into a new classfile with the aid of aClassTransformPREVIEW. The transform will receive each element of this class, as well as aClassBuilderPREVIEW 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
byte[] transformClass(ClassModelPREVIEW model, ClassEntryPREVIEW newClassName, ClassTransformPREVIEW transform) Transform one classfile into a new classfile with the aid of aClassTransformPREVIEW. The transform will receive each element of this class, as well as aClassBuilderPREVIEW 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
-
ClassFilewhen preview features are enabled.