Class BinaryStubWriter

java.lang.Object
org.checkerframework.framework.stubifier.BinaryStubWriter

public class BinaryStubWriter extends Object
Writes parsed stub compilation units into the compressed binary stub format read by org.checkerframework.framework.stub.BinaryStubReader. The output is a GZIP-compressed file (see writeTo(java.io.File)), conventionally named with the BIN_SUFFIX suffix (.bin.gz, e.g. OUTPUT_FILENAME). Used at build time for the annotated JDK (via JavaStubifier) and for the built-in checker stub files (via BinaryStubFileGenerator).

This extracts annotations structurally from class, interface, enum, annotation-type, and record declarations and their members — including type-parameter bound annotations, enum constants, annotation-type members, and record components (including an explicit canonical constructor's parameter annotation overrides) — and writes them into a dense binary format optimized for rapid loading without parsing overhead at checker startup.

Name resolution and member filtering deliberately mirror the text parser ( AnnotationFileParser): private declarations are skipped, annotation names resolve through explicit imports, java.lang, and asterisk imports, and @CFComment is dropped.

A writer constructed with omitUnannotatedMembers drops field records that carry no annotations and demotes unannotated method records to bare presence-only signature entries (see BinaryStubWriter.ClassRecord.presenceOnlyMethodSigs); that is correct only for the annotated JDK. See that field's documentation. Equivalence is enforced by BinaryStubDiffChecker; run NullnessBinaryStubDiffTest after changing this class.

A writer constructed with skipUnloadableAnnotations drops, rather than fails on, an annotation whose @Target cannot be read; see that field's documentation for the trade-off this accepts.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    File-name suffix appended to a source stub file's name to name its binary form (e.g.
    static final int
    Magic number identifying a binary stub bundle: a container of several per-file binary stubs for a whole -Astubs directory, written by BinaryStubFileGenerator's --bundle mode and read by org.checkerframework.framework.stub.BinaryStubBundle.
    static final String
    File-name suffix appended to a source stub directory's name to name the bundle covering it (e.g.
    static final short
    Format version of the binary stub bundle container.
    static final String
    Fully-qualified name of org.checkerframework.framework.qual.CFComment, which is never written to the binary format; see BinaryStubWriter.AnnotationPool.addAnnotation(com.github.javaparser.ast.expr.AnnotationExpr, com.github.javaparser.ast.CompilationUnit, org.checkerframework.framework.stubifier.BinaryStubWriter).
    static final String
    Prefix of the simple signature of a constructor, as org.checkerframework.javacutil.ElementUtils#getSimpleSignature writes it: <init>(...).
    static final byte
    ClassRecord.kind value for an annotation-type declaration.
    static final byte
    ClassRecord.kind value for a class or interface declaration: both ElementKind.CLASS and ElementKind.INTERFACE map to this constant.
    static final byte
    ClassRecord.kind value for an enum declaration.
    static final byte
    ClassRecord.kind value for a record declaration.
    static final int
    Magic number identifying the Checker Framework binary stub format (CF + JDK: 0xCF non-ASCII marker byte, 'J' 0x4A, 'D' 0x44, 'K' 0x4B).
    static final String
    File name of the binary stub output file.
    static final int
    Annotation-pool index that makes a type annotation a marker: it carries no annotation, and its type path locates a plain wildcard ?, one without a bound or an annotation.
    static final byte
    Constant for an array component path step (JVMS §4.7.20.2: 0).
    static final byte
    Constant for a nested type path step (JVMS §4.7.20.2: 1).
    static final byte
    Constant for a type argument path step (JVMS §4.7.20.2: 3).
    static final byte
    Constant for a wildcard bound path step (JVMS §4.7.20.2: 2).
    static final byte
    For TYPE_PATH_KIND_WILDCARD, repurposed type_argument_index indicating an extends bound.
    static final byte
    For TYPE_PATH_KIND_WILDCARD, repurposed type_argument_index indicating a super bound.
    static final short
    Format version of the binary stub file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a writer for a built-in stub file, which keeps every member record.
    BinaryStubWriter(boolean omitUnannotatedMembers)
    Creates a writer that fails on an unloadable annotation; see BinaryStubWriter(boolean, boolean).
    BinaryStubWriter(boolean omitUnannotatedMembers, boolean skipUnloadableAnnotations)
    Creates a writer.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Returns true if name is in a package that the Java platform declares.
    void
    process(com.github.javaparser.ast.CompilationUnit cu)
    Processes a single compilation unit, extracting annotations for its classes, methods, and fields.
    void
    processStubUnit(List<com.github.javaparser.ast.CompilationUnit> cus)
    Processes the compilation units of one stub file.
    void
    setSourceFingerprint(byte[] sourceBytes)
    Records the source .astub file this writer's output is generated from, so writeTo(java.io.File) embeds a fingerprint that a reader can use to detect a stale binary.
    void
    setSourceName(String sourceName)
    Sets the name of the stub file being written, for diagnostics; see sourceName.
    static byte[]
    sha256(byte[] bytes)
    Computes the SHA-256 digest of bytes.
    void
    writeTo(File file)
    Writes the accumulated class records and constant pool to the specified file in a compressed binary format.
    void
    Writes the accumulated class records and constant pool to the given stream in a compressed binary format, exactly as writeTo(File) does.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAGIC

      public static final int MAGIC
      Magic number identifying the Checker Framework binary stub format (CF + JDK: 0xCF non-ASCII marker byte, 'J' 0x4A, 'D' 0x44, 'K' 0x4B). This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData#MAGIC references it.
      See Also:
    • VERSION

      public static final short VERSION
      Format version of the binary stub file. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData#VERSION references it. Increment whenever the binary format changes, and add a line here.

      Version history:

      • 1: the first format (#1853).
      • 2: records the length and SHA-256 digest of the source stub file, for the staleness check of user-supplied -Astubs files (#1967).
      • 3: markers for plain wildcards ? (TYPE_ANNO_UNBOUNDED_WILDCARD); the annotations on a wildcard with a bound are written at the path of its other bound.
      • 4: constant expressions as trees of operations, evaluated by the reader; int and float literals keep their types; a simple name lists the classes that static imports can import it from; string values are unescaped.
      See Also:
    • BUNDLE_MAGIC

      public static final int BUNDLE_MAGIC
      Magic number identifying a binary stub bundle: a container of several per-file binary stubs for a whole -Astubs directory, written by BinaryStubFileGenerator's --bundle mode and read by org.checkerframework.framework.stub.BinaryStubBundle. Distinct from MAGIC so that a reader can tell a bundle from a per-file binary stub whose name collides with it (see BUNDLE_SUFFIX) and ignore it rather than fail.
      See Also:
    • BUNDLE_VERSION

      public static final short BUNDLE_VERSION
      Format version of the binary stub bundle container. Independent of VERSION: a bundle entry is itself a complete, ordinarily-versioned binary stub blob, so this version covers only the container (entry count and the path/length framing around each entry).
      See Also:
    • BUNDLE_SUFFIX

      public static final String BUNDLE_SUFFIX
      File-name suffix appended to a source stub directory's name to name the bundle covering it (e.g. a -Astubs directory named mystubs → sibling file mystubs.astub.bin.gz). Unlike BIN_SUFFIX, which is appended to a file that already ends in .astub, a directory name has no such extension to begin with, so this suffix spells it out. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubBundle#SUFFIX references it.
      See Also:
    • OUTPUT_FILENAME

      public static final String OUTPUT_FILENAME
      File name of the binary stub output file. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData#FILENAME references it.
      See Also:
    • BIN_SUFFIX

      public static final String BIN_SUFFIX
      File-name suffix appended to a source stub file's name to name its binary form (e.g. jdk.astub → jdk.astub.bin.gz). This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData#BIN_SUFFIX references it.
      See Also:
    • KIND_CLASS_OR_INTERFACE

      public static final byte KIND_CLASS_OR_INTERFACE
      ClassRecord.kind value for a class or interface declaration: both ElementKind.CLASS and ElementKind.INTERFACE map to this constant. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_CLASS_OR_INTERFACE references it.
      See Also:
    • KIND_ENUM

      public static final byte KIND_ENUM
      ClassRecord.kind value for an enum declaration. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_ENUM references it.
      See Also:
    • KIND_ANNOTATION_TYPE

      public static final byte KIND_ANNOTATION_TYPE
      ClassRecord.kind value for an annotation-type declaration. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_ANNOTATION_TYPE references it.
      See Also:
    • KIND_RECORD

      public static final byte KIND_RECORD
      ClassRecord.kind value for a record declaration. This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_RECORD references it.
      See Also:
    • CONSTRUCTOR_SIG_PREFIX

      public static final String CONSTRUCTOR_SIG_PREFIX
      Prefix of the simple signature of a constructor, as org.checkerframework.javacutil.ElementUtils#getSimpleSignature writes it: <init>(...). This is the canonical definition; org.checkerframework.framework.stub.BinaryStubData#CONSTRUCTOR_SIG_PREFIX references it.
      See Also:
    • TYPE_PATH_KIND_ARRAY

      public static final byte TYPE_PATH_KIND_ARRAY
      Constant for an array component path step (JVMS §4.7.20.2: 0).
      See Also:
    • TYPE_PATH_KIND_INNER_TYPE

      public static final byte TYPE_PATH_KIND_INNER_TYPE
      Constant for a nested type path step (JVMS §4.7.20.2: 1).
      See Also:
    • TYPE_PATH_KIND_WILDCARD

      public static final byte TYPE_PATH_KIND_WILDCARD
      Constant for a wildcard bound path step (JVMS §4.7.20.2: 2).
      See Also:
    • TYPE_PATH_KIND_TYPE_ARGUMENT

      public static final byte TYPE_PATH_KIND_TYPE_ARGUMENT
      Constant for a type argument path step (JVMS §4.7.20.2: 3).
      See Also:
    • TYPE_PATH_WILDCARD_BOUND_EXTENDS

      public static final byte TYPE_PATH_WILDCARD_BOUND_EXTENDS
      For TYPE_PATH_KIND_WILDCARD, repurposed type_argument_index indicating an extends bound.
      See Also:
    • TYPE_PATH_WILDCARD_BOUND_SUPER

      public static final byte TYPE_PATH_WILDCARD_BOUND_SUPER
      For TYPE_PATH_KIND_WILDCARD, repurposed type_argument_index indicating a super bound.
      See Also:
    • TYPE_ANNO_UNBOUNDED_WILDCARD

      public static final int TYPE_ANNO_UNBOUNDED_WILDCARD
      Annotation-pool index that makes a type annotation a marker: it carries no annotation, and its type path locates a plain wildcard ?, one without a bound or an annotation. The reader gives that wildcard the annotations of the bounds of its type variable's declaration, as the text parser does ( AnnotationFileParser.annotateUnannotatedUnboundedWildcard). Negative, so it is never a pool index, and not IGNORED, which is never written.
      See Also:
    • CF_COMMENT

      public static final String CF_COMMENT
      Fully-qualified name of org.checkerframework.framework.qual.CFComment, which is never written to the binary format; see BinaryStubWriter.AnnotationPool.addAnnotation(com.github.javaparser.ast.expr.AnnotationExpr, com.github.javaparser.ast.CompilationUnit, org.checkerframework.framework.stubifier.BinaryStubWriter). BinaryStubDiffChecker filters the same annotation out of the text-parsed side.

      Must stay a compile-time constant (hence the literal, rather than CFComment.class.getCanonicalName()): javac interns it, so the reference-equality check in BinaryStubDiffChecker works.

      See Also:
  • Constructor Details

    • BinaryStubWriter

      public BinaryStubWriter()
      Creates a writer for a built-in stub file, which keeps every member record.
    • BinaryStubWriter

      public BinaryStubWriter(boolean omitUnannotatedMembers)
      Creates a writer that fails on an unloadable annotation; see BinaryStubWriter(boolean, boolean).
      Parameters:
      omitUnannotatedMembers - whether to omit unannotated field records and demote unannotated method records to presence-only signature entries; see omitUnannotatedMembers. Pass true only for the annotated JDK.
    • BinaryStubWriter

      public BinaryStubWriter(boolean omitUnannotatedMembers, boolean skipUnloadableAnnotations)
      Creates a writer.
      Parameters:
      omitUnannotatedMembers - whether to omit unannotated field records and demote unannotated method records to presence-only signature entries; see omitUnannotatedMembers. Pass true only for the annotated JDK.
      skipUnloadableAnnotations - whether to drop an annotation whose @Target cannot be read, with a warning, instead of aborting the writer; see skipUnloadableAnnotations for the trade-off this accepts.
  • Method Details

    • isPlatformPackage

      public static boolean isPlatformPackage(String name)
      Returns true if name is in a package that the Java platform declares. Also used by org.checkerframework.framework.stub.BinaryStubDiffChecker.
      Parameters:
      name - a fully-qualified annotation name, or a package name with a trailing "."
      Returns:
      true if the name is in a Java platform package
    • setSourceFingerprint

      public void setSourceFingerprint(byte[] sourceBytes)
      Records the source .astub file this writer's output is generated from, so writeTo(java.io.File) embeds a fingerprint that a reader can use to detect a stale binary. Only BinaryStubFileGenerator calls this: the annotated JDK and other output of JavaStubifier are never checked for staleness this way, and leave it unset.
      Parameters:
      sourceBytes - the source file's raw content
    • sha256

      public static byte[] sha256(byte[] bytes)
      Computes the SHA-256 digest of bytes. Also used by org.checkerframework.framework.stub.BinaryStubData, to check that a binary stub file matches its source.
      Parameters:
      bytes - the bytes to digest
      Returns:
      the 32-byte SHA-256 digest of bytes
    • setSourceName

      public void setSourceName(String sourceName)
      Sets the name of the stub file being written, for diagnostics; see sourceName.
      Parameters:
      sourceName - the name of the stub file
    • process

      public void process(com.github.javaparser.ast.CompilationUnit cu)
      Processes a single compilation unit, extracting annotations for its classes, methods, and fields.
      Parameters:
      cu - the compilation unit to process
    • processStubUnit

      public void processStubUnit(List<com.github.javaparser.ast.CompilationUnit> cus)
      Processes the compilation units of one stub file. A stub file may contain multiple package sections, which the stub parser represents as multiple compilation units; the text parser resolves names against the imports of the first unit only (see AnnotationFileParser.getImportedAnnotations), and this method does the same.
      Parameters:
      cus - the compilation units of the stub file, in order
    • writeTo

      public void writeTo(File file) throws IOException
      Writes the accumulated class records and constant pool to the specified file in a compressed binary format.
      Parameters:
      file - the output file (usually ending in .bin.gz)
      Throws:
      IOException - if writing to the file fails
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Writes the accumulated class records and constant pool to the given stream in a compressed binary format, exactly as writeTo(File) does. Lets a caller assemble several writers' output in memory (see BinaryStubFileGenerator's --bundle mode) instead of writing each one to its own file.
      Parameters:
      out - the stream to write to; closed by this method
      Throws:
      IOException - if writing to the stream fails