Class VariableApplier

java.lang.Object
org.checkerframework.framework.util.element.VariableApplier

public class VariableApplier extends Object
Applies annotations to variable declaration (providing they are not the use of a TYPE_PARAMETER).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Element
    An Element that type represents.
    protected final AnnotatedTypeMirror
    The type to which we wish to apply annotations.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    accepts(AnnotatedTypeMirror typeMirror, Element element)
    Returns true if this is a variable declaration including fields an enum constants.
    protected com.sun.tools.javac.code.TargetType[]
    Returns the TargetTypes that identify annotations we wish to apply with this object.
    static void
    Apply annotations from element to type.
    void
    Reads the list of annotations that apply to this element (see getRawTypeAttributes).
    protected Iterable<com.sun.tools.javac.code.Attribute.TypeCompound>
    Annotations on elements are represented as Attribute.TypeCompounds ( a subtype of AnnotationMirror) that are usually accessed through a getRawTypeAttributes method on the element.
    protected void
    handleInvalid(List<com.sun.tools.javac.code.Attribute.TypeCompound> invalid)
    This implementation reports all invalid annotations as errors.
    protected void
    handleTargeted(List<com.sun.tools.javac.code.Attribute.TypeCompound> targeted)
    This method should apply all annotations that are handled by this object.
    protected void
    handleValid(List<com.sun.tools.javac.code.Attribute.TypeCompound> valid)
    The default implementation of this method does nothing.
    protected boolean
    Tests element/type fields to ensure that this TargetedElementAnnotationApplier is valid for this element/type pair.
    protected Map<org.checkerframework.framework.util.element.TargetedElementAnnotationApplier.TargetClass,List<com.sun.tools.javac.code.Attribute.TypeCompound>>
    sift(Iterable<com.sun.tools.javac.code.Attribute.TypeCompound> typeCompounds)
    Separate the input annotations into a Map of TargetClass (TARGETED, VALID, INVALID) to the annotations that fall into each of those categories.
    protected com.sun.tools.javac.code.TargetType[]
    Returns the TargetTypes that identify annotations that are valid but we wish to ignore.

    Methods inherited from class java.lang.Object

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

    • type

      protected final AnnotatedTypeMirror type
      The type to which we wish to apply annotations.
    • element

      protected final Element element
      An Element that type represents.
  • Method Details

    • apply

      Apply annotations from element to type.
      Throws:
      ElementAnnotationUtil.UnexpectedAnnotationLocationException
    • accepts

      public static boolean accepts(AnnotatedTypeMirror typeMirror, Element element)
      Returns true if this is a variable declaration including fields an enum constants.
      Parameters:
      typeMirror - ignored
      Returns:
      true if this is a variable declaration including fields an enum constants
    • annotatedTargets

      protected com.sun.tools.javac.code.TargetType[] annotatedTargets()
      Returns the TargetTypes that identify annotations we wish to apply with this object. Any annotations that have these target types will be passed to handleTargeted.
      Returns:
      the TargetTypes that identify annotations we wish to apply with this object. Any annotations that have these target types will be passed to handleTargeted
    • validTargets

      protected com.sun.tools.javac.code.TargetType[] validTargets()
      Returns the TargetTypes that identify annotations that are valid but we wish to ignore. Any annotations that have these target types will be passed to handleValid, providing they aren't also in annotatedTargets.
      Returns:
      the TargetTypes that identify annotations that are valid but we wish to ignore
    • getRawTypeAttributes

      protected Iterable<com.sun.tools.javac.code.Attribute.TypeCompound> getRawTypeAttributes()
      Annotations on elements are represented as Attribute.TypeCompounds ( a subtype of AnnotationMirror) that are usually accessed through a getRawTypeAttributes method on the element.

      In Java 8 and later these annotations are generally contained by elements to which they apply. However, in earlier versions of Java many of these annotations are handled by either the enclosing method, e.g. parameters and method type parameters, or enclosing class, e.g. class type parameters. Therefore, many annotations are addressed by first getting all annotations on a method or class and the picking out only the ones we wish to target (see extractAndApply).

      Returns:
      the annotations that we MAY wish to apply to the given type
    • isAccepted

      protected boolean isAccepted()
      Tests element/type fields to ensure that this TargetedElementAnnotationApplier is valid for this element/type pair.
      Returns:
      true if the type/element members are handled by this class false otherwise
    • handleTargeted

      protected void handleTargeted(List<com.sun.tools.javac.code.Attribute.TypeCompound> targeted) throws ElementAnnotationUtil.UnexpectedAnnotationLocationException
      This method should apply all annotations that are handled by this object.
      Parameters:
      targeted - the list of annotations that were returned by getRawTypeAttributes and had a TargetType contained by annotatedTargets
      Throws:
      ElementAnnotationUtil.UnexpectedAnnotationLocationException
    • extractAndApply

      Reads the list of annotations that apply to this element (see getRawTypeAttributes). Sifts them into three groups (TARGETED, INVALID, VALID) and then calls the appropriate handle method on them. The handleTargeted method should apply all annotations that are handled by this object.

      This method will throw a runtime exception if isAccepted returns false.

      Throws:
      ElementAnnotationUtil.UnexpectedAnnotationLocationException
    • handleValid

      protected void handleValid(List<com.sun.tools.javac.code.Attribute.TypeCompound> valid)
      The default implementation of this method does nothing.
      Parameters:
      valid - the list of annotations that were returned by getRawTypeAttributes and had a TargetType contained by valid and NOT annotatedTargets
    • handleInvalid

      protected void handleInvalid(List<com.sun.tools.javac.code.Attribute.TypeCompound> invalid)
      This implementation reports all invalid annotations as errors.
      Parameters:
      invalid - the list of annotations that were returned by getRawTypeAttributes and were not handled by handleTargeted or handleValid
    • sift

      protected Map<org.checkerframework.framework.util.element.TargetedElementAnnotationApplier.TargetClass,List<com.sun.tools.javac.code.Attribute.TypeCompound>> sift(Iterable<com.sun.tools.javac.code.Attribute.TypeCompound> typeCompounds)
      Separate the input annotations into a Map of TargetClass (TARGETED, VALID, INVALID) to the annotations that fall into each of those categories.
      Parameters:
      typeCompounds - annotations to sift through, should be those returned by getRawTypeAttributes
      Returns:
      a Map<TargetClass => Annotations>.