Class MoreAnnotations

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

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

    • getAnnotation

      public static Optional<com.sun.tools.javac.code.Attribute.Compound> getAnnotation(Element sym, String qualifiedName)
      Returns the annotation on the given element with the specified qualified binary or canonical name.
    • getAnnotation

      public static Optional<com.sun.tools.javac.code.Attribute.Compound> getAnnotation(Element sym, Name qualifiedName)
      Returns the annotation on the given element with the specified qualified name.
    • getAnnotationWithSimpleName

      public static Optional<com.sun.tools.javac.code.Attribute.Compound> getAnnotationWithSimpleName(Element sym, String simpleName)
      Returns the annotation on the given element with the specified simple name.
    • getValue

      public static Optional<AnnotationValue> getValue(AnnotationMirror annotationMirror, String name)
      Returns the value of the annotation element-value pair with the given name if it is explicitly set.
    • getValue

      @InlineMe(replacement="MoreAnnotations.getValue((AnnotationMirror) attribute, name).map(Attribute.class::cast)", imports={"com.google.errorprone.util.MoreAnnotations","com.sun.tools.javac.code.Attribute","javax.lang.model.element.AnnotationMirror"}) @Deprecated 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.
    • getStringValue

      public static Optional<String> getStringValue(AnnotationMirror attribute, String name)
      Shorthand for extracting a string value of a named element from an annotation mirror.
    • getStringValue

      public static Optional<String> getStringValue(Element sym, String annotationName)
      Shorthand for extracting the "value" string of a named annotation on an element.
    • getBooleanValue

      public static Optional<Boolean> getBooleanValue(AnnotationMirror attribute, String name)
      Shorthand for extracting a boolean value of a named element from an annotation mirror.
    • getBooleanValue

      public static Optional<Boolean> getBooleanValue(Element sym, String annotationName)
      Shorthand for extracting the "value" boolean of a named annotation on an element.
    • getEnumValue

      public static Optional<com.sun.tools.javac.code.Symbol.VarSymbol> getEnumValue(AnnotationMirror attribute, String name)
      Shorthand for extracting an enum value of a named element from an annotation mirror.
    • getEnumValue

      public static Optional<com.sun.tools.javac.code.Symbol.VarSymbol> getEnumValue(Element sym, String annotationName)
      Shorthand for extracting the "value" enum of a named annotation on an element.
    • getEnumValues

      public static com.google.common.collect.ImmutableList<com.sun.tools.javac.code.Symbol.VarSymbol> getEnumValues(AnnotationMirror attribute, String name)
      Shorthand for extracting enum values of a named element from an annotation mirror.
    • getEnumValues

      public static com.google.common.collect.ImmutableList<com.sun.tools.javac.code.Symbol.VarSymbol> getEnumValues(Element sym, String annotationName)
      Shorthand for extracting the "value" enum values of a named annotation on an element.
    • getStrings

      public static com.google.common.collect.ImmutableList<String> getStrings(AnnotationMirror attribute, String name)
      Shorthand for extracting a list of strings for a named element (e.g. array or single string).
    • getStrings

      public static com.google.common.collect.ImmutableList<String> getStrings(Element sym, String annotationName)
      Shorthand for extracting a list of strings from the "value" element of a named annotation on an element.
    • getTypes

      public static com.google.common.collect.ImmutableList<TypeMirror> getTypes(AnnotationMirror attribute, String name)
      Shorthand for extracting a list of types for a named element (e.g. Class[] or Class).
    • getTypes

      public static com.google.common.collect.ImmutableList<TypeMirror> getTypes(Element sym, String annotationName)
      Shorthand for extracting a list of types from the "value" element of a named annotation on an element.
    • getTypeValue

      public static Optional<TypeMirror> getTypeValue(AnnotationMirror attribute, String name)
      Shorthand for extracting a type value of a named element from an annotation mirror.
    • getTypeValue

      public static Optional<TypeMirror> getTypeValue(Element sym, String annotationName)
      Shorthand for extracting the "value" type of a named annotation on an element.
    • asBooleanValue

      public static Optional<Boolean> asBooleanValue(AnnotationValue a)
      Converts the given attribute to a boolean value.
    • 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 a string value.
    • asEnumValue

      public static Optional<com.sun.tools.javac.code.Symbol.VarSymbol> asEnumValue(AnnotationValue a)
      Converts the given attribute to an enum value.
    • asEnumValue

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

      public static Stream<com.sun.tools.javac.code.Symbol.VarSymbol> asEnumValues(AnnotationValue v)
      Converts the given annotation value to one or more enum values.
    • asEnumValues

      @Deprecated public static <T extends Enum<T>> EnumSet<T> asEnumValues(Class<T> clazz, AnnotationValue a)
      Deprecated.
      Converts the given attribute to enum values.
    • asTypeValue

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

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

      public static Optional<AnnotationMirror> asAnnotationValue(AnnotationValue a)
      Converts the given attribute to an annotation mirror.
    • asAnnotations

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

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

      @InlineMe(replacement="MoreAnnotations.getValue(annotationMirror, name)", imports="com.google.errorprone.util.MoreAnnotations") @Deprecated public static Optional<AnnotationValue> getAnnotationValue(AnnotationMirror annotationMirror, String name)
      Deprecated.
    • getAnnotationValue

      @InlineMe(replacement="MoreAnnotations.getValue((AnnotationMirror) attribute, name)", imports={"com.google.errorprone.util.MoreAnnotations","javax.lang.model.element.AnnotationMirror"}) @Deprecated public static Optional<AnnotationValue> getAnnotationValue(com.sun.tools.javac.code.Attribute.Compound attribute, String name)
    • asElement

      public static com.sun.tools.javac.code.Symbol.TypeSymbol asElement(AnnotationMirror annotationMirror)
      Returns the Symbol.TypeSymbol representing the declaration of the annotation mirror.
    • getTargetElementTypes

      public static @Nullable Set<ElementType> getTargetElementTypes(Element annotationSymbol)
      Returns the set of ElementType targets specified in the annotation's @Target meta-annotation, or null if no @Target meta-annotation is present.
    • isTypeAnnotation

      public static boolean isTypeAnnotation(Element annotationSymbol)
      Returns whether the annotation is a type annotation (i.e. its @Target includes ElementType.TYPE_USE or ElementType.TYPE_PARAMETER).
    • getRetentionPolicy

      public static RetentionPolicy getRetentionPolicy(Element annotationSymbol)
      Returns the effective RetentionPolicy of the annotation symbol (defaulting to RetentionPolicy.CLASS per JLS 9.6.4.2 if omitted or not found).
    • targetsElement

      public static boolean targetsElement(Element annotationSymbol, ElementType target)
      Returns whether the annotation applies to the given ElementType. If the annotation has no @Target meta-annotation, it is applicable in all declaration contexts (per JLS 9.6.4.1), which includes all element types except ElementType.TYPE_USE and ElementType.TYPE_PARAMETER.