matchAnnotation function

bool matchAnnotation(
  1. TypeChecker typeChecker,
  2. ElementAnnotation annotation
)

Checks if an ElementAnnotation node is exactly the type specified by url.

The url is of the form package:full.package.name/source/path.dart#Symbol.

It will attempt to compute the constant value of the annotation in case the annotation was declared in a file other than the one currently being compiled.

If a logger is provided, a warning is output if it fails to resolve the annotation, otherwise an ArgumentError is thrown.

Implementation

bool matchAnnotation(TypeChecker typeChecker, ElementAnnotation annotation) {
  final object = annotation.computeConstantValue();
  // TODO(b/123715184) Surface the constantEvaluationErrors.
  try {
    return typeChecker.isExactlyType(object!.type!);
  } catch (_) {
    var message = ''
        'Could not determine type of annotation. It resolved to '
        '${annotation.computeConstantValue()}. '
        'Are you missing a dependency?';
    if (annotation is ElementAnnotationImpl) {
      message += ''
          '\n'
          '${annotation.annotationAst.toSource()} in '
          '${annotation.librarySource.uri.toString()}';
    }
    throw ArgumentError.value(
      annotation,
      'annotation',
      message,
    );
  }
}