hasAnnotation function

bool hasAnnotation(
  1. AnnotatedNode node,
  2. String annotationName
)

Checks if an AnnotatedNode contains an annotation with the given annotationName (e.g. 'visibleForTesting', 'pragma', 'Native').

Matches both simple names (e.g. @visibleForTesting), prefixed names (e.g. @meta.visibleForTesting), and constructor invocations (e.g. @Meta.visibleForTesting()).

Implementation

bool hasAnnotation(AnnotatedNode node, String annotationName) {
  for (final meta in node.metadata) {
    final rawName = meta.name.name;
    final baseName = rawName.contains('.') ? rawName.split('.').last : rawName;
    if (baseName == annotationName) {
      return true;
    }
    final constructorName = meta.constructorName?.name;
    if (constructorName == annotationName) {
      return true;
    }
  }
  return false;
}