annotatedDeclarations function
Returns a List of declarations that have given annotation.
If on
is null
, finds declarations at the top level of any library in the current
MirrorSystem, otherwise finds declarations (variables, properties, methods and constructors) declared on on
.
If recursive
is true
and on
is provided, superclasses, annotated methods
defined on superclasses and interfaces will be included.
If an abstract method is annotated on both a class and on the implementing class, then only the overridden method is included.
eg. given the annotations
class Foo { const Foo(); }
class Bar {
final String name;
const Bar(this.name);
}
@Foo()
bar();
@Bar('Bar Name')
class Clazz {
@Foo()
myMethod() {}
}
then
final annotatedDeclaration = annotatedDeclarations(Bar).single;
is an AnnotatedDeclaration with AnnotatedDeclaration.declaration being a ClassMirror on Clazz
and
AnnotatedDeclaration.annotation set to const Bar('Bar Name')
.
Top level vs scoped declarations
// A MethodMirror on the top level method `bar`
annotatedDeclarations(Foo).single.declaration;
// A MethodMirror on `Clazz.myMethod`.
annotatedDeclarations(Foo, on: Clazz).single.declaration;
Implementation
List<AnnotatedDeclaration> annotatedDeclarations(Type annotation,
{Type? on, bool recursive = false}) {
final annotationMirror = reflectClass(annotation);
return UnmodifiableListView(on == null
? _topLevelAnnotatedDeclarations(annotationMirror)
: _findDeclarationsOn(reflectClass(on), annotationMirror,
recursive: recursive));
}