Class BinaryStubWriter
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
FieldsModifier and TypeFieldDescriptionstatic final StringFile-name suffix appended to a source stub file's name to name its binary form (e.g.static final intMagic number identifying a binary stub bundle: a container of several per-file binary stubs for a whole-Astubsdirectory, written byBinaryStubFileGenerator's--bundlemode and read byorg.checkerframework.framework.stub.BinaryStubBundle.static final StringFile-name suffix appended to a source stub directory's name to name the bundle covering it (e.g.static final shortFormat version of the binary stub bundle container.static final StringFully-qualified name oforg.checkerframework.framework.qual.CFComment, which is never written to the binary format; seeBinaryStubWriter.AnnotationPool.addAnnotation(com.github.javaparser.ast.expr.AnnotationExpr, com.github.javaparser.ast.CompilationUnit, org.checkerframework.framework.stubifier.BinaryStubWriter).static final StringPrefix of the simple signature of a constructor, asorg.checkerframework.javacutil.ElementUtils#getSimpleSignaturewrites it:<init>(...).static final byteClassRecord.kindvalue for an annotation-type declaration.static final byteClassRecord.kindvalue for a class or interface declaration: bothElementKind.CLASSandElementKind.INTERFACEmap to this constant.static final byteClassRecord.kindvalue for an enum declaration.static final byteClassRecord.kindvalue for a record declaration.static final intMagic number identifying the Checker Framework binary stub format (CF+JDK: 0xCF non-ASCII marker byte,'J'0x4A,'D'0x44,'K'0x4B).static final StringFile name of the binary stub output file.static final intAnnotation-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 byteConstant for an array component path step (JVMS §4.7.20.2: 0).static final byteConstant for a nested type path step (JVMS §4.7.20.2: 1).static final byteConstant for a type argument path step (JVMS §4.7.20.2: 3).static final byteConstant for a wildcard bound path step (JVMS §4.7.20.2: 2).static final byteForTYPE_PATH_KIND_WILDCARD, repurposedtype_argument_indexindicating an extends bound.static final byteForTYPE_PATH_KIND_WILDCARD, repurposedtype_argument_indexindicating a super bound.static final shortFormat version of the binary stub file. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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; seeBinaryStubWriter(boolean, boolean).BinaryStubWriter(boolean omitUnannotatedMembers, boolean skipUnloadableAnnotations) Creates a writer. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisPlatformPackage(String name) Returns true ifnameis in a package that the Java platform declares.voidprocess(com.github.javaparser.ast.CompilationUnit cu) Processes a single compilation unit, extracting annotations for its classes, methods, and fields.voidprocessStubUnit(List<com.github.javaparser.ast.CompilationUnit> cus) Processes the compilation units of one stub file.voidsetSourceFingerprint(byte[] sourceBytes) Records the source.astubfile this writer's output is generated from, sowriteTo(java.io.File)embeds a fingerprint that a reader can use to detect a stale binary.voidsetSourceName(String sourceName) Sets the name of the stub file being written, for diagnostics; seesourceName.static byte[]sha256(byte[] bytes) Computes the SHA-256 digest ofbytes.voidWrites the accumulated class records and constant pool to the specified file in a compressed binary format.voidwriteTo(OutputStream out) Writes the accumulated class records and constant pool to the given stream in a compressed binary format, exactly aswriteTo(File)does.
-
Field Details
-
MAGIC
public static final int MAGICMagic 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#MAGICreferences it.- See Also:
-
VERSION
public static final short VERSIONFormat version of the binary stub file. This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData#VERSIONreferences 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
-Astubsfiles (#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;
intandfloatliterals 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_MAGICMagic number identifying a binary stub bundle: a container of several per-file binary stubs for a whole-Astubsdirectory, written byBinaryStubFileGenerator's--bundlemode and read byorg.checkerframework.framework.stub.BinaryStubBundle. Distinct fromMAGICso that a reader can tell a bundle from a per-file binary stub whose name collides with it (seeBUNDLE_SUFFIX) and ignore it rather than fail.- See Also:
-
BUNDLE_VERSION
public static final short BUNDLE_VERSIONFormat version of the binary stub bundle container. Independent ofVERSION: 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
File-name suffix appended to a source stub directory's name to name the bundle covering it (e.g. a-Astubsdirectory namedmystubs→ sibling filemystubs.astub.bin.gz). UnlikeBIN_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#SUFFIXreferences it.- See Also:
-
OUTPUT_FILENAME
File name of the binary stub output file. This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData#FILENAMEreferences it.- See Also:
-
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_SUFFIXreferences it.- See Also:
-
KIND_CLASS_OR_INTERFACE
public static final byte KIND_CLASS_OR_INTERFACEClassRecord.kindvalue for a class or interface declaration: bothElementKind.CLASSandElementKind.INTERFACEmap to this constant. This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_CLASS_OR_INTERFACEreferences it.- See Also:
-
KIND_ENUM
public static final byte KIND_ENUMClassRecord.kindvalue for an enum declaration. This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_ENUMreferences it.- See Also:
-
KIND_ANNOTATION_TYPE
public static final byte KIND_ANNOTATION_TYPEClassRecord.kindvalue for an annotation-type declaration. This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_ANNOTATION_TYPEreferences it.- See Also:
-
KIND_RECORD
public static final byte KIND_RECORDClassRecord.kindvalue for a record declaration. This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData.ClassRecord#KIND_RECORDreferences it.- See Also:
-
CONSTRUCTOR_SIG_PREFIX
Prefix of the simple signature of a constructor, asorg.checkerframework.javacutil.ElementUtils#getSimpleSignaturewrites it:<init>(...). This is the canonical definition;org.checkerframework.framework.stub.BinaryStubData#CONSTRUCTOR_SIG_PREFIXreferences it.- See Also:
-
TYPE_PATH_KIND_ARRAY
public static final byte TYPE_PATH_KIND_ARRAYConstant 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_TYPEConstant 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_WILDCARDConstant 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_ARGUMENTConstant 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_EXTENDSForTYPE_PATH_KIND_WILDCARD, repurposedtype_argument_indexindicating an extends bound.- See Also:
-
TYPE_PATH_WILDCARD_BOUND_SUPER
public static final byte TYPE_PATH_WILDCARD_BOUND_SUPERForTYPE_PATH_KIND_WILDCARD, repurposedtype_argument_indexindicating a super bound.- See Also:
-
TYPE_ANNO_UNBOUNDED_WILDCARD
public static final int TYPE_ANNO_UNBOUNDED_WILDCARDAnnotation-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 notIGNORED, which is never written.- See Also:
-
CF_COMMENT
Fully-qualified name oforg.checkerframework.framework.qual.CFComment, which is never written to the binary format; seeBinaryStubWriter.AnnotationPool.addAnnotation(com.github.javaparser.ast.expr.AnnotationExpr, com.github.javaparser.ast.CompilationUnit, org.checkerframework.framework.stubifier.BinaryStubWriter).BinaryStubDiffCheckerfilters 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 inBinaryStubDiffCheckerworks.- 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; seeBinaryStubWriter(boolean, boolean).- Parameters:
omitUnannotatedMembers- whether to omit unannotated field records and demote unannotated method records to presence-only signature entries; seeomitUnannotatedMembers. 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; seeomitUnannotatedMembers. Pass true only for the annotated JDK.skipUnloadableAnnotations- whether to drop an annotation whose@Targetcannot be read, with a warning, instead of aborting the writer; seeskipUnloadableAnnotationsfor the trade-off this accepts.
-
-
Method Details
-
isPlatformPackage
Returns true ifnameis in a package that the Java platform declares. Also used byorg.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.astubfile this writer's output is generated from, sowriteTo(java.io.File)embeds a fingerprint that a reader can use to detect a stale binary. OnlyBinaryStubFileGeneratorcalls this: the annotated JDK and other output ofJavaStubifierare 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 ofbytes. Also used byorg.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
Sets the name of the stub file being written, for diagnostics; seesourceName.- 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
Processes the compilation units of one stub file. A stub file may contain multiplepackagesections, which the stub parser represents as multiple compilation units; the text parser resolves names against the imports of the first unit only (seeAnnotationFileParser.getImportedAnnotations), and this method does the same.- Parameters:
cus- the compilation units of the stub file, in order
-
writeTo
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
Writes the accumulated class records and constant pool to the given stream in a compressed binary format, exactly aswriteTo(File)does. Lets a caller assemble several writers' output in memory (seeBinaryStubFileGenerator's--bundlemode) 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
-