Class MoreAnnotations

java.lang.Object
com.google.errorprone.util.MoreAnnotations

public final class MoreAnnotations extends Object
Annotation-related utilities.
  • Method Details

    • getDeclarationAndTypeAttributes

      public static Stream<com.sun.tools.javac.code.Attribute.Compound> getDeclarationAndTypeAttributes(com.sun.tools.javac.code.Symbol sym)
      Returns declaration annotations of the given symbol, as well as 'top-level' type annotations, including :
      • Type annotations of the return type of a method.
      • Type annotations on the type of a formal parameter or field.

      One might expect this to be equivalent to information returned by Type.getAnnotationMirrors(), but javac doesn't associate type annotation information with types for symbols completed from class files, so that approach doesn't work across compilation boundaries.

    • getTopLevelTypeAttributes

      public static Stream<com.sun.tools.javac.code.Attribute.TypeCompound> getTopLevelTypeAttributes(com.sun.tools.javac.code.Symbol sym)
      Returns "top-level" type annotations of the given symbol, including:
      • Type annotations of the return type of a method.
      • Type annotations on the type of a formal parameter or field.

      These annotations are not always included in those returned by Type.getAnnotationMirrors() because javac doesn't associate type annotation information with types for symbols completed from class files. These type annotations won't be included when the symbol is not in the current compilation.

    • getValue

      public static Optional<com.sun.tools.javac.code.Attribute> getValue(com.sun.tools.javac.code.Attribute.Compound attribute, String name)
      Returns the value of the annotation element-value pair with the given name if it is explicitly set.
    • getAnnotationValue

      public static Optional<AnnotationValue> getAnnotationValue(com.sun.tools.javac.code.Attribute.Compound attribute, String name)
      Returns the value of the annotation element-value pair with the given name if it is explicitly set.
    • asIntegerValue

      public static Optional<Integer> asIntegerValue(AnnotationValue a)
      Converts the given attribute to an integer value.
    • asStringValue

      public static Optional<String> asStringValue(AnnotationValue a)
      Converts the given attribute to an string value.
    • asEnumValue

      public static <T extends Enum<T>> Optional<T> asEnumValue(Class<T> clazz, AnnotationValue a)
      Converts the given attribute to an enum value.
    • asTypeValue

      public static Optional<TypeMirror> asTypeValue(AnnotationValue a)
      Converts the given attribute to an enum value.
    • asStrings

      public static Stream<String> asStrings(AnnotationValue v)
      Converts the given annotation value to one or more strings.
    • asTypes

      public static Stream<TypeMirror> asTypes(AnnotationValue v)
      Converts the given annotation value to one or more annotations.