Class RegexAnnotatedTypeFactory

All Implemented Interfaces:
AnnotationProvider

public class RegexAnnotatedTypeFactory extends BaseAnnotatedTypeFactory
Adds Regex to the type of tree, in the following cases:
  1. a String or char literal that is a valid regular expression
  2. concatenation of two valid regular expression values (either String or char) or two partial regular expression values that make a valid regular expression when concatenated.
  3. for calls to Pattern.compile, change the group count value of the return type to be the same as the parameter. For calls to the asRegex methods of the classes in asRegexClasses, the returned @Regex String gets the same group count as the second argument to the call to asRegex.
Provides a basic analysis of concatenation of partial regular expressions to determine if a valid regular expression is produced by concatenating non-regular expression Strings. Do do this, PartialRegex is added to the type of tree in the following cases:
  1. a String literal that is not a valid regular expression.
  2. concatenation of two partial regex Strings that doesn't result in a regex String or a partial regex and regex String.
Also, adds PolyRegex to the type of String/char concatenation of a Regex and a PolyRegex or two PolyRegexs.
  • Field Details

  • Constructor Details

    • RegexAnnotatedTypeFactory

      public RegexAnnotatedTypeFactory(BaseTypeChecker checker)
      Create a new RegexAnnotatedTypeFactory.
      Parameters:
      checker - the checker
  • Method Details

    • 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
      Overrides:
      createSupportedTypeQualifiers in class AnnotatedTypeFactory
      Returns:
      the type qualifiers supported this processor, or an empty set if none
    • createFlowTransferFunction

      public CFTransfer createFlowTransferFunction(CFAbstractAnalysis<CFValue,CFStore,CFTransfer> analysis)
      Description copied from class: GenericAnnotatedTypeFactory
      Returns the appropriate transfer function that is used for the given org.checkerframework.dataflow analysis.

      This implementation uses the checker naming convention to create the appropriate transfer function. If no transfer function is found, it returns an instance of CFTransfer.

      Subclasses have to override this method to create the appropriate transfer function if they do not follow the checker naming convention.

      Overrides:
      createFlowTransferFunction in class GenericAnnotatedTypeFactory<CFValue,CFStore,CFTransfer,CFAnalysis>
      Parameters:
      analysis - a dataflow analysis
      Returns:
      a new transfer function
    • createQualifierHierarchy

      protected QualifierHierarchy createQualifierHierarchy()
      Description copied from class: AnnotatedTypeFactory
      Returns the QualifierHierarchy to be used by this checker.

      The implementation builds the type qualifier hierarchy for the AnnotatedTypeFactory.getSupportedTypeQualifiers() using the meta-annotations found in them. The current implementation returns an instance of NoElementQualifierHierarchy.

      Subclasses must override this method if their qualifiers have elements; the method must return an implementation of QualifierHierarchy, such as ElementQualifierHierarchy.

      Overrides:
      createQualifierHierarchy in class AnnotatedTypeFactory
      Returns:
      a QualifierHierarchy for this type system
    • getGroupCount

      public int getGroupCount(AnnotationMirror anno)
      Returns the group count value of the given annotation or 0 if there's a problem getting the group count value.
      Parameters:
      anno - a @Regex annotation
      Returns:
      the value element of the annotation
    • getGroupCount

      public static int getGroupCount(@Regex String regexp)
      Returns the number of groups in the given regex String.
    • getWidenedAnnotations

      public AnnotationMirrorSet getWidenedAnnotations(AnnotationMirrorSet annos, TypeKind typeKind, TypeKind widenedTypeKind)
      Description copied from class: AnnotatedTypeFactory
      Returns annotations applicable to type widenedTypeKind, that are copied or adapted from annos.
      Overrides:
      getWidenedAnnotations in class AnnotatedTypeFactory
      Parameters:
      annos - annotations to widen, from a primitive or boxed primitive
      typeKind - primitive type to widen
      widenedTypeKind - target for the returned annotations; a primitive type that is wider than typeKind (in the sense of JLS 5.1.2)
      Returns:
      result of converting annos from typeKind to widenedTypeKind
    • 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));
       
      Overrides:
      createTreeAnnotator in class GenericAnnotatedTypeFactory<CFValue,CFStore,CFTransfer,CFAnalysis>
      Returns:
      a tree annotator