Class Insertion

java.lang.Object
org.checkerframework.afu.annotator.find.Insertion
Direct Known Subclasses:
AnnotationInsertion, CastInsertion, CloseParenthesisInsertion, TypedInsertion

public abstract class Insertion extends Object
Specifies something that needs to be inserted into a source file, including the "what" and the "where".
  • Field Details

    • packageNames

      protected @Growable Set<String> packageNames
      The package names for the annotations being inserted by this Insertion. This will be empty unless getText(boolean) is called with abbreviate=true.
    • alwaysQualify

      protected static Set<String> alwaysQualify
      Set of annotation names that should always be inserted fully-qualified, even when getText(boolean) is called with abbreviate=true.
  • Constructor Details

    • Insertion

      public Insertion(Criteria criteria, boolean separateLine)
      Creates a new insertion.
      Parameters:
      criteria - where to insert the text
      separateLine - if true, insert the text on its own line
  • Method Details

    • getCriteria

      public Criteria getCriteria()
      Gets the insertion criteria.
      Returns:
      the criteria
    • getText

      public String getText()
      Gets the insertion text (not commented or abbreviated, and without added leading or trailing whitespace).
      Returns:
      the text to insert
    • getText

      public String getText(boolean abbreviate, boolean gotSeparateLine, int pos, char precedingChar)
      Gets the insertion text with a leading and/or trailing space added based on the values of the gotSeparateLine, pos, and precedingChar parameters.
      Parameters:
      abbreviate - if true, the package name will be removed from the annotations. The package name can be retrieved again by calling the getPackageNames() method.
      gotSeparateLine - true if this insertion is actually added on a separate line
      pos - the source position where this insertion will be inserted
      precedingChar - the character directly preceding where this insertion will be inserted. This value will be ignored if pos is 0.
      Returns:
      the text to insert
    • getText

      protected abstract String getText(boolean abbreviate)
      Gets the insertion text.
      Parameters:
      abbreviate - if true, the package name will be removed from the annotations. The package name can be retrieved again by calling the getPackageNames() method.
      Returns:
      the text to insert
    • addLeadingSpace

      protected boolean addLeadingSpace(boolean gotSeparateLine, int pos, char precedingChar)
      Indicates if a preceding space should be added to this insertion. Subclasses may override this method for custom leading space rules.
      Parameters:
      gotSeparateLine - true if this insertion is actually added on a separate line
      pos - the source position where this insertion will be inserted
      precedingChar - the character directly preceding where this insertion will be inserted. This value will be ignored if pos is 0.
      Returns:
      true if a leading space should be added, false otherwise
    • addTrailingSpace

      protected boolean addTrailingSpace(boolean gotSeparateLine)
      Indicates if a trailing space should be added to this insertion. Subclasses may override this method for custom trailing space rules.
      Parameters:
      gotSeparateLine - true if this insertion is actually added on a separate line
      Returns:
      true if a trailing space should be added, false otherwise
    • getPackageNames

      public Set<String> getPackageNames()
      Gets the package name.
      Returns:
      the package name of the annotation being inserted by this Insertion. This will be empty unless getText(boolean) is called with abbreviate=true.
    • getAlwaysQualify

      public static Set<String> getAlwaysQualify()
      Gets the set of annotation names that should always be inserted fully-qualified.
      Returns:
      the annotation names that should always be inserted fully-qualified
    • setAlwaysQualify

      public static void setAlwaysQualify(Set<String> set)
      Sets the set of annotation names that should always be qualified.
      Parameters:
      set - the annotation names that should always be inserted fully-qualified
    • isSeparateLine

      public boolean isSeparateLine()
      Returns true if the insertion goes on a separate line.
      Returns:
      true if the insertion goes on a separate line
    • isInserted

      public boolean isInserted()
      Returns true if this insertion has already been inserted into source code.
      Returns:
      true if this insertion has already been inserted, false otherwise
    • setInserted

      public void setInserted(boolean inserted)
      Sets whether this insertion has already been inserted into source code.
      Parameters:
      inserted - true if this insertion has already been inserted, false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toStringWithoutClass

      public String toStringWithoutClass()
      Format this without reporting its class name.
      Returns:
      a representation of this, without its class name
    • collectionToString

      public static String collectionToString(Collection<? extends Insertion> list)
      Format a list of insertions, with one insertion on each line.
      Parameters:
      list - a collection of insertions
      Returns:
      a multi-line string representation of the list
    • getKind

      public abstract Insertion.Kind getKind()
      Gets the kind of this insertion.
      Returns:
      the kind of this insertion
    • removePackage

      public static Insertion.PackageAndAnnotation removePackage(String s)
      Removes and returns the leading package.
      Parameters:
      s - the string representation of an annotation
      Returns:
      given @com.foo.bar(baz) it returns com.foo and @bar(baz)
    • typeToString

      public String typeToString(Type type, boolean abbreviate)
      Converts the given type to a String. This method can't be in the Type class because this method relies on the Insertion class to format annotations, and the Insertion class is not available from Type.
      Parameters:
      type - the type to convert
      abbreviate - if true, the package name will be removed from the annotations. The package name can be retrieved again by calling the getPackageNames() method.
      Returns:
      the type as a string
    • decorateType

      public static void decorateType(List<Insertion> innerTypeInsertions, Type outerType)
      Adds each of the given inner type insertions to the correct part of the type, based on the insertion's type path.
      Parameters:
      innerTypeInsertions - the insertions to add to the type. These must be inner type insertions, meaning each of the insertions' Criteria must contain a GenericArrayLocationCriterion and GenericArrayLocationCriterion.getLocation() must return a non-empty list.
      outerType - the type to add the insertions to
    • decorateType

      public static void decorateType(List<Insertion> innerTypeInsertions, Type outerType, ASTPath outerPath)