Class BinaryStubData

java.lang.Object
org.checkerframework.framework.stub.BinaryStubData

public class BinaryStubData extends Object
In-memory representation of a binary stub file: the annotated JDK (annotated-jdk.bin.gz) or the binary form of a built-in checker stub file (*.astub.bin.gz). The binary format stores annotation information extracted from the stub sources in a compact, pre-parsed form that is faster to load than the original text stubs.

Instances are immutable after construction and contain no javac objects (only strings and primitives), so they are cached per JVM and shared across compilations; see AnnotationFileElementTypes#loadedBinaryStubData.

The binary format consists of a GZIP-compressed stream containing:

  1. A 4-byte magic number (0xCF4A444B).
  2. A 2-byte version number.
  3. A source fingerprint: a 4-byte source length (-1 if none was recorded, as for the annotated JDK and other stub files generated by JavaStubifier), followed, when non-negative, by the 32-byte SHA-256 digest of the source .astub file's raw bytes. Used only to detect a stale binary for a user-supplied -Astubs file; see matchesSource(byte[]).
  4. A constant pool of UTF-8 strings (class names, field names, signatures, string literals).
  5. An annotation pool of structural annotation records.
  6. A sequence of BinaryStubData.ClassRecord entries, one per class, interface, enum, annotation type, or record.
  7. Package and module annotation records.
See Also:
  • Field Details

    • MAGIC

      public static final int MAGIC
      Magic number identifying the Checker Framework binary stub format. Defined once in BinaryStubWriter.MAGIC.
      See Also:
    • VERSION

      public static final short VERSION
      Format version of the binary stub file. Defined once in BinaryStubWriter.VERSION.
      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). Defined once in BinaryStubWriter.BIN_SUFFIX.
      See Also:
    • FILENAME

      public static final String FILENAME
      File name of the binary stub file. Defined once in BinaryStubWriter.OUTPUT_FILENAME.
      See Also:
    • CONSTRUCTOR_SIG_PREFIX

      public static final String CONSTRUCTOR_SIG_PREFIX
      Prefix of the simple signature of a constructor, as ElementUtils.getSimpleSignature writes it: <init>(...). Defined once in BinaryStubWriter.CONSTRUCTOR_SIG_PREFIX.
      See Also:
    • CF_COMMENT

      public static final String CF_COMMENT
      Fully-qualified name of CFComment, which is never written to the binary format. Defined once in BinaryStubWriter.CF_COMMENT.

      Re-exported here so that BinaryStubDiffChecker, which must filter the same annotation out of the text-parsed side, can name it without depending on the stubifier source set: framework-test, where that class lives, does not have the stubifier on its classpath. This is a compile-time constant, so reading it creates no runtime dependency.

      See Also:
    • stringPool

      public final String[] stringPool
      All strings referenced by the binary data.

      These are not interned. Nothing compares them by reference: class names and signatures are only ever used as map keys or compared with equals, and the one identity comparison on an annotation name (BinaryStubReader.isAnnotatedForThisChecker) compares the result of AnnotationUtils.annotationName, which is interned by its own contract, not a string from this pool.

      Interning them bought nothing and cost most of the load: interning this pool's ~24,000 strings took the annotated JDK's binary stub file from about 20 ms to about 130 ms to load (measured; the cost is the cold string-table insertions). That is once per JVM, since the parsed data is cached in AnnotationFileElementTypes#loadedBinaryStubData.

    • annotationPool

      public final BinaryStubData.AnnotationRecord[] annotationPool
      Pre-parsed structural annotations referenced by indices in the records.
    • classes

      public final Map<String,BinaryStubData.ClassRecord> classes
      Map from fully-qualified class name to its annotation record. The key uses '.' as separator and covers both top-level classes and inner classes (e.g. "com.sun.tools.javac.code.Symbol.Completer").
    • packages

      public final Map<String,int[]> packages
      Map from fully-qualified package name to annotation-pool indices of its declaration annotations.
    • modules

      public final Map<String,int[]> modules
      Map from module name to annotation-pool indices of its declaration annotations.
    • sourceLength

      public final int sourceLength
      Length in bytes of the source .astub file this binary was generated from, or -1 if no fingerprint was recorded (the annotated JDK, and other binaries JavaStubifier produces, are never checked for staleness this way: they are regenerated by the same build that packages their text source). See matchesSource(byte[]).
    • sourceDigest

      public final byte @Nullable [] sourceDigest
      SHA-256 digest of the source .astub file's raw bytes, or null iff sourceLength is -1.
  • Constructor Details

    • BinaryStubData

      public BinaryStubData(InputStream in) throws IOException
      Reads binary stub data from the given stream. The stream must supply the GZIP-compressed binary format written by BinaryStubWriter. Prefer read(java.io.InputStream), which additionally reports a damaged file's out-of-range pool index as an IOException.
      Parameters:
      in - the input stream to read from; the stream is closed when this constructor returns
      Throws:
      IOException - if the stream cannot be read or contains an invalid/unsupported format
  • Method Details

    • read

      public static BinaryStubData read(InputStream in) throws IOException
      Reads binary stub data from the given stream, reporting a damaged file as an IOException regardless of how the damage surfaces. A damaged file can hold an index that points outside a pool, which this class does not validate (unlike a count; see readCount(java.io.DataInputStream, java.lang.String)) and which surfaces as ArrayIndexOutOfBoundsException; converting it lets the caller fall back to text parsing instead of crashing the compilation.
      Parameters:
      in - the input stream to read from; the stream is closed when this method returns
      Returns:
      the binary stub data read from in
      Throws:
      IOException - if the stream cannot be read or contains an invalid/unsupported format
    • matchesSource

      public boolean matchesSource(byte[] sourceBytes)
      Returns true if this binary was generated from source bytes identical to sourceBytes. Always false if no fingerprint was recorded (see sourceLength).
      Parameters:
      sourceBytes - the current raw bytes of the .astub file this binary is claimed to correspond to
      Returns:
      true if sourceBytes matches the fingerprint this binary was generated from