getAnnotationsByName function

Iterable<ElementAnnotation> getAnnotationsByName(
  1. DartType dartType,
  2. String annotationName
)

Implementation

Iterable<ElementAnnotation> getAnnotationsByName(
    DartType dartType, String annotationName) {
  return dartType.element!.metadata.where((m) {
    DartType annotationType;
    if (m.element! is PropertyAccessorElement) {
      annotationType = (m.element! as PropertyAccessorElement).returnType;
    } else if (m.element! is ConstructorElement) {
      annotationType = (m.element! as ConstructorElement).returnType;
    } else {
      return false;
    }
    if (annotationType is InterfaceType) {
      final types = <InterfaceType>[
        annotationType,
        ...annotationType.allSupertypes
      ];
      return types.where((element) {
        return element.getDisplayString(withNullability: true) ==
            annotationName;
      }).isNotEmpty;
    }
    return false;
  });
}