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
  • Field Details

    • MAGIC_NUMBER

      static final int MAGIC_NUMBER
      0xCAFEBABE
      See Also:
    • ACC_PUBLIC

      static final int ACC_PUBLIC
      The bit mask of PUBLIC access and property modifier.
      See Also:
    • ACC_PROTECTED

      static final int ACC_PROTECTED
      The bit mask of PROTECTED access and property modifier.
      See Also:
    • ACC_PRIVATE

      static final int ACC_PRIVATE
      The bit mask of PRIVATE access and property modifier.
      See Also:
    • ACC_INTERFACE

      static final int ACC_INTERFACE
      The bit mask of INTERFACE access and property modifier.
      See Also:
    • ACC_ENUM

      static final int ACC_ENUM
      The bit mask of ENUM access and property modifier.
      See Also:
    • ACC_ANNOTATION

      static final int ACC_ANNOTATION
      The bit mask of ANNOTATION access and property modifier.
      See Also:
    • ACC_SUPER

      static final int ACC_SUPER
      The bit mask of SUPER access and property modifier.
      See Also:
    • ACC_ABSTRACT

      static final int ACC_ABSTRACT
      The bit mask of ABSTRACT access and property modifier.
      See Also:
    • ACC_VOLATILE

      static final int ACC_VOLATILE
      The bit mask of VOLATILE access and property modifier.
      See Also:
    • ACC_TRANSIENT

      static final int ACC_TRANSIENT
      The bit mask of TRANSIENT access and property modifier.
      See Also:
    • ACC_SYNTHETIC

      static final int ACC_SYNTHETIC
      The bit mask of SYNTHETIC access and property modifier.
      See Also:
    • ACC_STATIC

      static final int ACC_STATIC
      The bit mask of STATIC access and property modifier.
      See Also:
    • ACC_FINAL

      static final int ACC_FINAL
      The bit mask of FINAL access and property modifier.
      See Also:
    • ACC_SYNCHRONIZED

      static final int ACC_SYNCHRONIZED
      The bit mask of SYNCHRONIZED access and property modifier.
      See Also:
    • ACC_BRIDGE

      static final int ACC_BRIDGE
      The bit mask of BRIDGE access and property modifier.
      See Also:
    • ACC_VARARGS

      static final int ACC_VARARGS
      The bit mask of VARARGS access and property modifier.
      See Also:
    • ACC_NATIVE

      static final int ACC_NATIVE
      The bit mask of NATIVE access and property modifier.
      See Also:
    • ACC_STRICT

      static final int ACC_STRICT
      The bit mask of STRICT access and property modifier.
      See Also:
    • ACC_MODULE

      static final int ACC_MODULE
      The bit mask of MODULE access and property modifier.
      See Also:
    • ACC_OPEN

      static final int ACC_OPEN
      The bit mask of OPEN access and property modifier.
      See Also:
    • ACC_MANDATED

      static final int ACC_MANDATED
      The bit mask of MANDATED access and property modifier.
      See Also:
    • ACC_TRANSITIVE

      static final int ACC_TRANSITIVE
      The bit mask of TRANSITIVE access and property modifier.
      See Also:
    • ACC_STATIC_PHASE

      static final int ACC_STATIC_PHASE
      The bit mask of STATIC_PHASE access and property modifier.
      See Also:
    • JAVA_1_VERSION

      static final int JAVA_1_VERSION
      The class major version of JAVA_1.
      See Also:
    • JAVA_2_VERSION

      static final int JAVA_2_VERSION
      The class major version of JAVA_2.
      See Also:
    • JAVA_3_VERSION

      static final int JAVA_3_VERSION
      The class major version of JAVA_3.
      See Also:
    • JAVA_4_VERSION

      static final int JAVA_4_VERSION
      The class major version of JAVA_4.
      See Also:
    • JAVA_5_VERSION

      static final int JAVA_5_VERSION
      The class major version of JAVA_5.
      See Also:
    • JAVA_6_VERSION

      static final int JAVA_6_VERSION
      The class major version of JAVA_6.
      See Also:
    • JAVA_7_VERSION

      static final int JAVA_7_VERSION
      The class major version of JAVA_7.
      See Also:
    • JAVA_8_VERSION

      static final int JAVA_8_VERSION
      The class major version of JAVA_8.
      See Also:
    • JAVA_9_VERSION

      static final int JAVA_9_VERSION
      The class major version of JAVA_9.
      See Also:
    • JAVA_10_VERSION

      static final int JAVA_10_VERSION
      The class major version of JAVA_10.
      See Also:
    • JAVA_11_VERSION

      static final int JAVA_11_VERSION
      The class major version of JAVA_11.
      See Also:
    • JAVA_12_VERSION

      static final int JAVA_12_VERSION
      The class major version of JAVA_12.
      See Also:
    • JAVA_13_VERSION

      static final int JAVA_13_VERSION
      The class major version of JAVA_13.
      See Also:
    • JAVA_14_VERSION

      static final int JAVA_14_VERSION
      The class major version of JAVA_14.
      See Also:
    • JAVA_15_VERSION

      static final int JAVA_15_VERSION
      The class major version of JAVA_15.
      See Also:
    • JAVA_16_VERSION

      static final int JAVA_16_VERSION
      The class major version of JAVA_16.
      See Also:
    • JAVA_17_VERSION

      static final int JAVA_17_VERSION
      The class major version of JAVA_17.
      See Also:
    • JAVA_18_VERSION

      static final int JAVA_18_VERSION
      The class major version of JAVA_18.
      See Also:
    • JAVA_19_VERSION

      static final int JAVA_19_VERSION
      The class major version of JAVA_19.
      See Also:
    • JAVA_20_VERSION

      static final int JAVA_20_VERSION
      The class major version of JAVA_20.
      See Also:
    • JAVA_21_VERSION

      static final int JAVA_21_VERSION
      The class major version of JAVA_21.
      See Also:
    • JAVA_22_VERSION

      static final int JAVA_22_VERSION
      The class major version of JAVA_22.
      See Also:
    • JAVA_23_VERSION

      static final int JAVA_23_VERSION
      The class major version of JAVA_23.
      See Also:
    • JAVA_24_VERSION

      static final int JAVA_24_VERSION
      The class major version of JAVA_24.
      See Also:
    • PREVIEW_MINOR_VERSION

      static final int PREVIEW_MINOR_VERSION
      A 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

      static ClassFile of()
      Returns a context with default options.
      Returns:
      a context with default options
    • of

      static ClassFile of(ClassFile.Option... options)
      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

      ClassFile withOptions(ClassFile.Option... options)
      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

      ClassModel parse(byte[] bytes)
      Parse a classfile into a ClassModel.
      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

      default ClassModel parse(Path path) throws IOException
      Parse a classfile into a ClassModel.
      Parameters:
      path - the path to the classfile
      Returns:
      the class model
      Throws:
      IOException - if an I/O error occurs
      IllegalArgumentException - or its subclass if the classfile format is not supported or an incompatibility prevents parsing of the classfile
    • build

      default byte[] build(ClassDesc thisClass, Consumer<? super ClassBuilder> handler)
      Build a classfile into a byte array.
      Parameters:
      thisClass - the name of the class to build
      handler - a handler that receives a ClassBuilder
      Returns:
      the classfile bytes
      Throws:
      IllegalArgumentException - if thisClass 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 build
      constantPool - the constant pool builder
      handler - a handler that receives a ClassBuilder
      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 write
      thisClass - the name of the class to build
      handler - a handler that receives a ClassBuilder
      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 write
      thisClassEntry - the name of the class to build
      constantPool - the constant pool builder
      handler - a handler that receives a ClassBuilder
      Throws:
      IOException - if an I/O error occurs
    • buildModule

      default byte[] buildModule(ModuleAttribute moduleAttribute)
      Build a module descriptor into a byte array.
      Parameters:
      moduleAttribute - the Module attribute
      Returns:
      the classfile bytes
    • buildModule

      default byte[] buildModule(ModuleAttribute moduleAttribute, Consumer<? super ClassBuilder> handler)
      Build a module descriptor into a byte array.
      Parameters:
      moduleAttribute - the Module attribute
      handler - a handler that receives a ClassBuilder
      Returns:
      the classfile bytes
    • buildModuleTo

      default void buildModuleTo(Path path, ModuleAttribute moduleAttribute) throws IOException
      Build a module descriptor into a file.
      Parameters:
      path - the file to write
      moduleAttribute - the Module 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 write
      moduleAttribute - the Module attribute
      handler - a handler that receives a ClassBuilder
      Throws:
      IOException - if an I/O error occurs
    • transformClass

      default byte[] transformClass(ClassModel model, ClassTransform transform)
      Transform one classfile into a new classfile with the aid of a ClassTransform. The transform will receive each element of this class, as well as a ClassBuilder 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 transform
      transform - the transform
      Returns:
      the bytes of the new class
    • transformClass

      default byte[] transformClass(ClassModel model, ClassDesc newClassName, ClassTransform transform)
      Transform one classfile into a new classfile with the aid of a ClassTransform. The transform will receive each element of this class, as well as a ClassBuilder 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 transform
      newClassName - new class name
      transform - the transform
      Returns:
      the bytes of the new class
    • transformClass

      byte[] transformClass(ClassModel model, ClassEntry newClassName, ClassTransform transform)
      Transform one classfile into a new classfile with the aid of a ClassTransform. The transform will receive each element of this class, as well as a ClassBuilder 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 transform
      newClassName - new class name
      transform - the transform
      Returns:
      the bytes of the new class
    • verify

      List<VerifyError> verify(ClassModel model)
      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

      List<VerifyError> verify(byte[] bytes)
      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

      default List<VerifyError> verify(Path path) throws IOException
      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