Class UnitsAnnotatedTypeFactory

All Implemented Interfaces:

public class UnitsAnnotatedTypeFactory extends BaseAnnotatedTypeFactory
Annotated type factory for the Units Checker.

Handles multiple names for the same unit, with different prefixes, e.g. @kg is the same as @g(Prefix.kilo).

Supports relations between units. If m is a variable of type "@m" and s is a variable of type "@s", the division m / s is automatically annotated as "@mPERs", the correct unit for the result.

  • Field Details

  • Constructor Details

    • UnitsAnnotatedTypeFactory

      public UnitsAnnotatedTypeFactory(BaseTypeChecker checker)
  • Method Details

    • createAnnotatedTypeFormatter

      protected AnnotatedTypeFormatter createAnnotatedTypeFormatter()
      Description copied from class: AnnotatedTypeFactory
      Creates the AnnotatedTypeFormatter used by this type factory and all AnnotatedTypeMirrors it creates. The AnnotatedTypeFormatter is used in AnnotatedTypeMirror.toString and will affect the error messages printed for checkers that use this type factory.
      createAnnotatedTypeFormatter in class AnnotatedTypeFactory
      the AnnotatedTypeFormatter to pass to all instantiated AnnotatedTypeMirrors
    • canonicalAnnotation

      public AnnotationMirror canonicalAnnotation(AnnotationMirror anno)
      Description copied from class: AnnotatedTypeFactory
      Returns the canonical annotation for the passed annotation. Returns null if the passed annotation is not an alias of a canonical one in the framework.

      A canonical annotation is the internal annotation that will be used by the Checker Framework in the aliased annotation's place.

      canonicalAnnotation in class AnnotatedTypeFactory
      anno - the qualifier to check for an alias
      the canonical annotation, or null if none exists
    • getUnitsRel

      protected Map<@CanonicalName String,UnitsRelations> getUnitsRel()
      Returns a map from canonical class name to the corresponding UnitsRelations instance.
      a map from canonical class name to the corresponding UnitsRelations instance
    • createAnnotationClassLoader

      protected AnnotationClassLoader createAnnotationClassLoader()
      Description copied from class: AnnotatedTypeFactory
      Factory method to easily change what AnnotationClassLoader is created to load type annotation classes. Subclasses can override this method and return a custom AnnotationClassLoader subclass to customize loading logic.
      createAnnotationClassLoader in class AnnotatedTypeFactory
    • createSupportedTypeQualifiers

      protected Set<Class<? extends Annotation>> createSupportedTypeQualifiers()
      Description copied from class: AnnotatedTypeFactory
      Returns a mutable set of annotation classes that are supported by a checker.

      Subclasses may override this method to return a mutable set of their supported type qualifiers through one of the 5 approaches shown below.

      Subclasses should not call this method; they should call AnnotatedTypeFactory.getSupportedTypeQualifiers() instead.

      By default, a checker supports all annotations located in a subdirectory called qual that's located in the same directory as the checker. Note that only annotations defined with the @Target({ElementType.TYPE_USE}) meta-annotation (and optionally with the additional value of ElementType.TYPE_PARAMETER, but no other ElementType values) are automatically considered as supported annotations.

      To support a different set of annotations than those in the qual subdirectory, or that have other ElementType values, see examples below.

      In total, there are 5 ways to indicate annotations that are supported by a checker:

      1. Only support annotations located in a checker's qual directory:

        This is the default behavior. Simply place those annotations within the qual directory.

      2. Support annotations located in a checker's qual directory and a list of other annotations:

        Place those annotations within the qual directory, and override AnnotatedTypeFactory.createSupportedTypeQualifiers() by calling AnnotatedTypeFactory.getBundledTypeQualifiers(Class...) with a varargs parameter list of the other annotations. Code example:

         @Override protected Set<Class<? extends Annotation>> createSupportedTypeQualifiers() {
              return getBundledTypeQualifiers(Regex.class, PartialRegex.class, RegexBottom.class, UnknownRegex.class);
      3. Supporting only annotations that are explicitly listed: Override AnnotatedTypeFactory.createSupportedTypeQualifiers() and return a mutable set of the supported annotations. Code example:
         @Override protected Set<Class<? extends Annotation>> createSupportedTypeQualifiers() {
              return new HashSet<Class<? extends Annotation>>(
                      Arrays.asList(A.class, B.class));
        The set of qualifiers returned by AnnotatedTypeFactory.createSupportedTypeQualifiers() must be a fresh, mutable set. The methods AnnotatedTypeFactory.getBundledTypeQualifiers(Class...) must return a fresh, mutable set
      createSupportedTypeQualifiers in class AnnotatedTypeFactory
      the type qualifiers supported this processor, or an empty set if none
    • createTreeAnnotator

      public TreeAnnotator createTreeAnnotator()
      Description copied from class: GenericAnnotatedTypeFactory
      Returns a TreeAnnotator that adds annotations to a type based on the contents of a tree.

      The default tree annotator is a ListTreeAnnotator of the following:

      1. PropagationTreeAnnotator: Propagates annotations from subtrees
      2. LiteralTreeAnnotator: Adds annotations based on QualifierForLiterals meta-annotations
      3. DependentTypesTreeAnnotator: Adapts dependent annotations based on context

      Subclasses may override this method to specify additional tree annotators, for example:

       new ListTreeAnnotator(super.createTreeAnnotator(), new KeyLookupTreeAnnotator(this));
      createTreeAnnotator in class GenericAnnotatedTypeFactory<CFValue,CFStore,CFTransfer,CFAnalysis>
      a tree annotator
    • createQualifierHierarchy

      protected QualifierHierarchy createQualifierHierarchy()
      Set the Bottom qualifier as the bottom of the hierarchy.
      createQualifierHierarchy in class AnnotatedTypeFactory
      a QualifierHierarchy for this type system