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 Detail

      • REGEX

        protected final javax.lang.model.element.AnnotationMirror REGEX
        The @Regex annotation.
      • REGEXBOTTOM

        protected final javax.lang.model.element.AnnotationMirror REGEXBOTTOM
        The @RegexBottom annotation.
      • PARTIALREGEX

        protected final javax.lang.model.element.AnnotationMirror PARTIALREGEX
        The @PartialRegex annotation.
      • POLYREGEX

        protected final javax.lang.model.element.AnnotationMirror POLYREGEX
        The @PolyRegex annotation.
      • UNKNOWNREGEX

        protected final javax.lang.model.element.AnnotationMirror UNKNOWNREGEX
        The @UnknownRegex annotation.
      • regexValueElement

        protected final javax.lang.model.element.ExecutableElement regexValueElement
        The method that returns the value element of a @Regex annotation.
    • Constructor Detail

      • RegexAnnotatedTypeFactory

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

      • createSupportedTypeQualifiers

        protected java.util.Set<java.lang.Class<? extends java.lang.annotation.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
      • getGroupCount

        public int getGroupCount​(javax.lang.model.element.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 java.lang.String regexp)
        Returns the number of groups in the given regex String.
      • getWidenedAnnotations

        public AnnotationMirrorSet getWidenedAnnotations​(AnnotationMirrorSet annos,
                                                         javax.lang.model.type.TypeKind typeKind,
                                                         javax.lang.model.type.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