dart - Getting ClassMirror instances for all classes that have an annotation -
i have annotation
class target{ final string value; const target(this.value); }
and 2 classes annotated it
@target("/313") class c1{ } @target("/314") class c2{ }
how can list of classmirror
instances classes have target annotation?
based on selected answer if knew library calsses exist in
var mirrorsystem = currentmirrorsystem(); var librarymirror = mirrorsystem.findlibrary(#testlib); for(classmirror classmirror in librarymirror.declarations.values){ if(classmirror.metadata!=null){ for(var meta in classmirror.metadata){ if(meta.type == reflectclass(path)){ print(classmirror.simplename); print(meta.getfield(#value)); } } } }
this searches libraries in current isolate classes annotated @target('/313')
@mirrorsused(metatargets: target) // might necessary when build code javascript import 'dart:mirrors'; class target { final string id; const target(this.id); } @target('/313') class c1{ } @target('/314') class c2{ } @target('/313') @target('/314') class c3{ } void main() { mirrorsystem mirrorsystem = currentmirrorsystem(); mirrorsystem.libraries.foreach((lk, l) { l.declarations.foreach((dk, d) { if(d classmirror) { classmirror cm = d classmirror; cm.metadata.foreach((md) { instancemirror metadata = md instancemirror; if(metadata.type == reflectclass(target) && metadata.getfield(#id).reflectee == '/313') { print('found: ${cm.simplename}'); } }); } }); }); }
found: symbol("c3")
found: symbol("c1")
Comments
Post a Comment