Class ElementAnnotationApplier

java.lang.Object
org.checkerframework.framework.type.ElementAnnotationApplier

public final class ElementAnnotationApplier extends Object
Utility methods for adding the annotations that are stored in an Element to the type that represents that element (or a use of that Element).

In a way, this class is a hack: the Type representation for the Elements should contain all annotations that we want. However, due to javac bugs decoding the type annotations from the Element is necessary.

Even once these bugs are fixed, this class might be useful: in TypesIntoElements it is easy to add additional annotations to the element and have them stored in the bytecode by the compiler. It would be more work (and might not work in the end) to instead modify the Type directly. The interaction between TypeFromElement and TypesIntoElements allows us to write the defaulted annotations into the Element and have them read later by other parts.

  • Method Details

    • apply

      public static void apply(AnnotatedTypeMirror type, Element element, AnnotatedTypeFactory typeFactory)
      Add all of the relevant annotations stored in Element to type. This includes both top-level primary annotations and nested annotations. For the most part the TypeAnnotationPosition of the element annotations are used to locate the annotation in the right AnnotatedTypeMirror location though the individual applier classes may have special rules (such as those for upper and lower bounds and intersections).

      Note: Element annotations come from two sources.

      1. Annotations found on elements may represent those in source code or bytecode; these are added to the element by the compiler.
      2. The annotations may also represent those that were inferred or defaulted by the Checker Framework after a previous call to this method. The Checker Framework will store any annotations on declarations back into the elements that represent them (see TypesIntoElements). Subsequent, calls to apply will encounter these annotations on the provided element.
      Note: This is not the ONLY place that annotations are explicitly added to types. See TypeFromTree.
      Parameters:
      type - the type to which we wish to apply the element's annotations
      element - an element that possibly contains annotations
      typeFactory - the typeFactory used to create the given type
    • annotateSupers

      public static void annotateSupers(List<AnnotatedTypeMirror.AnnotatedDeclaredType> supertypes, TypeElement subtypeElement)
      Annotate the list of supertypes using the annotations on the TypeElement representing a class or interface.
      Parameters:
      supertypes - types representing supertype declarations of TypeElement
      subtypeElement - an element representing the declaration of the class which is a subtype of supertypes
    • getParamAndLambdaTree

      public static @Nullable org.plumelib.util.IPair<VariableTree,LambdaExpressionTree> getParamAndLambdaTree(VariableElement varEle, AnnotatedTypeFactory typeFactory)
      Helper method to get the lambda tree for ParamApplier. Ideally, this method would be located in ElementAnnotationUtil but since AnnotatedTypeFactory.declarationFromElement is protected, it has been placed here.
      Parameters:
      varEle - the element that may represent a lambda's parameter
      Returns:
      a LambdaExpressionTree if the varEle represents a parameter in a lambda expression, otherwise null