Class BinaryStubReaderTest

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

public class BinaryStubReaderTest extends Object
Unit test for BinaryStubReader.classRecordKind(javax.lang.model.element.ElementKind).

Regression test for using the annotated JDK across JDK versions whose API can drift out from under a fixed stub source: java.nio.ByteOrder became a real enum in JDK 26 after being a plain class through JDK 25, while the annotated JDK's own ByteOrder.java stub still declares it as a class. AnnotationFileParser.processTypeDecl has an existing, unrelated defensive check for exactly this kind of drift on the text side (it warns and skips the whole type when the real element's kind does not match the stub declaration's kind); classRecordKind is the corresponding building block for the binary side, mapping a real TypeElement's kind to the BinaryStubData.ClassRecord.KIND_* constant it must match.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Confirms a real annotation type maps to KIND_ANNOTATION_TYPE, not KIND_CLASS_OR_INTERFACE.
    void
    Confirms a real class and a real interface both map to KIND_CLASS_OR_INTERFACE, and so are treated as interchangeable with each other but not with an enum or annotation type.
    void
    Confirms BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind) narrows and widens between every numeric kind, and converts a character value for a numeric member through its code point, matching the text parser's convert((int) charValue, valueKind).
    void
    Confirms BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind) leaves a value alone when the member's kind is not a numeric primitive -- in particular TypeKind.NONE, which addValueToBuilder uses when the annotation member cannot be found at all.
    void
    Confirms a real enum maps to KIND_ENUM, not KIND_CLASS_OR_INTERFACE -- exactly the distinction that a stub still declaring java.nio.ByteOrder as a class must trip on JDK 26, where the real type is now an enum.
    void
    Confirms that AnnotationConstants.findField(javax.lang.model.element.TypeElement, java.lang.String, javax.lang.model.util.Elements, boolean) also finds an enum constant: TimeUnit.SECONDS is an ENUM_CONSTANT, not a FIELD, and the same lookup resolves both kinds of 'e' value.
    void
    Regression test for the traversal order of the field lookup, AnnotationConstants.findField(javax.lang.model.element.TypeElement, java.lang.String, javax.lang.model.util.Elements, boolean): the binary reader's lookup used to exhaust the whole superclass chain before ever looking at an interface, whereas the text parser's AnnotationFileParser#findFieldElement loops over ElementUtils.getAllFieldsIn, which interleaves interfaces with superclasses (a type's directly-implemented interfaces are visited before its superclass's superclass).
    void
    Regression test for the root cause of a bug in BinaryStubReader.resolveSingleValue(java.lang.Object, javax.lang.model.type.TypeKind, java.lang.String, org.checkerframework.framework.type.AnnotatedTypeFactory, org.checkerframework.framework.stub.BinaryStubData, org.checkerframework.framework.stub.AnnotationFileElementTypes): the writer tags every FieldAccessExpr annotation value with the same 'e' tag (see BinaryStubWriter#writeValue), regardless of whether the referenced member is an enum constant or a plain static final field, so the reader cannot tell which one it is from the tag alone.
    void
    Confirms ElementUtils.getModuleElement (used by AnnotationFileParser#processModule to resolve a stub-declared module name to a real element, mirroring processPackage's elements.getPackageElement) resolves a real module name to a non-null element, and returns null for a nonexistent one.
    void
    Regression test for BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind): an integral literal written for a floating-point annotation member must be converted to that member's kind.
    void
    Regression test for the root cause of a bug in BinaryStubReader.applyRecordComponents: a record component's RECORD_COMPONENT-kind element (returned by TypeElement.getRecordComponents()) is a distinct Element from its compiler-generated backing FIELD-kind element (returned by ElementFilter.fieldsIn), even though both share the same simple name.
    void
    Confirms a real record maps to KIND_RECORD, not KIND_CLASS_OR_INTERFACE.

    Methods inherited from class java.lang.Object

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

    • BinaryStubReaderTest

      public BinaryStubReaderTest()
  • Method Details

    • classAndInterfaceMapToClassOrInterface

      public void classAndInterfaceMapToClassOrInterface() throws Exception
      Confirms a real class and a real interface both map to KIND_CLASS_OR_INTERFACE, and so are treated as interchangeable with each other but not with an enum or annotation type.
      Throws:
      Exception - if a test compilation cannot be created
    • enumMapsToEnumNotClassOrInterface

      public void enumMapsToEnumNotClassOrInterface() throws Exception
      Confirms a real enum maps to KIND_ENUM, not KIND_CLASS_OR_INTERFACE -- exactly the distinction that a stub still declaring java.nio.ByteOrder as a class must trip on JDK 26, where the real type is now an enum.
      Throws:
      Exception - if the test compilation cannot be created
    • annotationTypeMapsToAnnotationType

      public void annotationTypeMapsToAnnotationType() throws Exception
      Confirms a real annotation type maps to KIND_ANNOTATION_TYPE, not KIND_CLASS_OR_INTERFACE.
      Throws:
      Exception - if the test compilation cannot be created
    • getModuleElementResolvesRealModule

      public void getModuleElementResolvesRealModule() throws Exception
      Confirms ElementUtils.getModuleElement (used by AnnotationFileParser#processModule to resolve a stub-declared module name to a real element, mirroring processPackage's elements.getPackageElement) resolves a real module name to a non-null element, and returns null for a nonexistent one. Elements.getModuleElement(CharSequence) itself is JDK 9+ only, so this also exercises that ElementUtils's reflective wrapper works on the JDK actually running this test.
      Throws:
      Exception - if the test compilation cannot be created
    • findFieldResolvesANonEnumStaticConstant

      public void findFieldResolvesANonEnumStaticConstant()
      Regression test for the root cause of a bug in BinaryStubReader.resolveSingleValue(java.lang.Object, javax.lang.model.type.TypeKind, java.lang.String, org.checkerframework.framework.type.AnnotatedTypeFactory, org.checkerframework.framework.stub.BinaryStubData, org.checkerframework.framework.stub.AnnotationFileElementTypes): the writer tags every FieldAccessExpr annotation value with the same 'e' tag (see BinaryStubWriter#writeValue), regardless of whether the referenced member is an enum constant or a plain static final field, so the reader cannot tell which one it is from the tag alone. resolveSingleValue used to only look for an ENUM_CONSTANT member and silently drop the value otherwise, so a stub value like IntRange(to = Integer.MAX_VALUE) lost the member entirely. This test confirms that the lookup, AnnotationConstants.findField(javax.lang.model.element.TypeElement, java.lang.String, javax.lang.model.util.Elements, boolean), finds a real, non-enum constant field and that BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind) then narrows its value to the annotation member's declared kind, matching what AnnotationFileParser.getValueOfExpressionInAnnotation does for the text parser.
    • findFieldMatchesAnEnumConstant

      public void findFieldMatchesAnEnumConstant()
      Confirms that AnnotationConstants.findField(javax.lang.model.element.TypeElement, java.lang.String, javax.lang.model.util.Elements, boolean) also finds an enum constant: TimeUnit.SECONDS is an ENUM_CONSTANT, not a FIELD, and the same lookup resolves both kinds of 'e' value.
    • findFieldMatchesTextParserOrderForAmbiguousName

      public void findFieldMatchesTextParserOrderForAmbiguousName() throws Exception
      Regression test for the traversal order of the field lookup, AnnotationConstants.findField(javax.lang.model.element.TypeElement, java.lang.String, javax.lang.model.util.Elements, boolean): the binary reader's lookup used to exhaust the whole superclass chain before ever looking at an interface, whereas the text parser's AnnotationFileParser#findFieldElement loops over ElementUtils.getAllFieldsIn, which interleaves interfaces with superclasses (a type's directly-implemented interfaces are visited before its superclass's superclass). For an ambiguous simple name declared in both Iface (implemented directly by Child) and Grandparent (two levels up Child's superclass chain), the old recursion returned the superclass field; getAllFieldsIn -- and so the text parser -- returns the interface field first. This test pins the field-lookup order to match the text parser.
      Throws:
      Exception - if the test compilation cannot be created
    • recordMapsToRecordNotClassOrInterface

      public void recordMapsToRecordNotClassOrInterface() throws Exception
      Confirms a real record maps to KIND_RECORD, not KIND_CLASS_OR_INTERFACE.
      Throws:
      Exception - if the test compilation cannot be created
    • integralValueIsWidenedToFloatingPointMember

      public void integralValueIsWidenedToFloatingPointMember()
      Regression test for BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind): an integral literal written for a floating-point annotation member must be converted to that member's kind.

      The writer records every integral literal as a long, so @DoubleExample(0) and @DoubleExample(0L) (both legal Java when DoubleExample.value() is a double; see checker/jtreg/stubs/issue1542/Stub.astub) reach the reader as a Long. Returning an Integer for them, as the reader used to, makes AnnotationBuilder.setValue throw and the whole annotation get silently dropped. The text parser's AnnotationFileParser.convert has always had the FLOAT and DOUBLE cases.

    • coerceToKindConvertsBetweenNumericKinds

      public void coerceToKindConvertsBetweenNumericKinds()
      Confirms BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind) narrows and widens between every numeric kind, and converts a character value for a numeric member through its code point, matching the text parser's convert((int) charValue, valueKind).
    • coerceToKindLeavesNonNumericKindsAlone

      public void coerceToKindLeavesNonNumericKindsAlone()
      Confirms BinaryStubReader.coerceToKind(java.lang.Object, javax.lang.model.type.TypeKind) leaves a value alone when the member's kind is not a numeric primitive -- in particular TypeKind.NONE, which addValueToBuilder uses when the annotation member cannot be found at all.
    • recordComponentElementDiffersFromItsBackingField

      public void recordComponentElementDiffersFromItsBackingField() throws Exception
      Regression test for the root cause of a bug in BinaryStubReader.applyRecordComponents: a record component's RECORD_COMPONENT-kind element (returned by TypeElement.getRecordComponents()) is a distinct Element from its compiler-generated backing FIELD-kind element (returned by ElementFilter.fieldsIn), even though both share the same simple name. AnnotationFileParser.processRecordField (the text parser) stores a component's annotated type in atypes keyed by the FIELD element; applyRecordComponents previously keyed it by the RECORD_COMPONENT element instead, which silently made a component's type-use annotations unreachable through AnnotationFileElementTypes#getAnnotatedTypeMirror (a bare Element-keyed lookup with no name-based fallback). This test locks in the fact that motivated the fix, so a future change cannot reintroduce the mismatch without also breaking this test.
      Throws:
      Exception - if the test compilation cannot be created