2

I have this annotation

class Target{
  final String value;
  const Target(this.value);
}

and 2 classes that are annotated with it

@Target("/313")
class c1{

}

@Target("/314")
class c2{

}

how can i get a List of ClassMirror instances for the classes that have the Target annotation?

based on the selected answer that is if i knew what library my 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));
            }
          }
    }
  }
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
FPGA
  • 3,525
  • 10
  • 44
  • 73

1 Answers1

5

This searches all libraries in the current isolate for classes that are annotated with @Target('/313')

@MirrorsUsed(metaTargets: Target) // might be necessary when you build this code to 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 is ClassMirror) {
        ClassMirror cm = d as ClassMirror;
        cm.metadata.forEach((md) {
          InstanceMirror metadata = md as InstanceMirror;
          if(metadata.type == reflectClass(Target) && metadata.getField(#id).reflectee == '/313') {
            print('found: ${cm.simpleName}');
          }
        });
      }
    });
  });
}

found: Symbol("c3")
found: Symbol("c1")

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I think you can optimize it with the `MirrorsUsed(metaTargets: Target)` – JAre Jun 27 '14 at 18:02
  • @JAre i don't need it since the code is meant to run on the server – FPGA Jun 27 '14 at 18:05
  • @FPGA it's might speed up things or might do nothing. No harm :) – JAre Jun 27 '14 at 18:06
  • @JAre What do you mean by `optimize it`. `MirrorsUsed` prevents dart2js from tree-shaking these classes. Thanks for the hint anyway. I added it to the code. – Günter Zöchbauer Jun 27 '14 at 18:07
  • @GünterZöchbauer if i recall correctly, in case of dart2js, it will preserve only necessary data for reflecting this class and drop all that is not. But I'm not sure that it will be any good on server side. – JAre Jun 27 '14 at 18:09
  • 1
    As far as I know it's for preventing tree-shaking dropping classes/members when the classes are only referenced by reflective code (which tree-shaking can't recognize). Hopefully @lrn finds this question and sheds some additional light on it ;-) – Günter Zöchbauer Jun 27 '14 at 18:13
  • 2
    @GünterZöchbauer Im not sure, but I remember one time when dart2js complained that it had to export 500+ (things) and if I want to make it better then I should add this annotation and it helped (it exported less stuff) – JAre Jun 27 '14 at 18:15
  • 2
    If it finds `import 'dart:mirrors';` tree-shaking retains all classes/members because no `MirrorsUsed` provided additional information. I think it gets hairy if you access classes from other libraries using reflective code. Then it probably retains all information from the library that imports `dart:mirrors` but drops all from all libraries which don't (when not referenced otherwise). Which may lead to situations where your reflective code accesses classes that may not available anymore when built to JS. – Günter Zöchbauer Jun 27 '14 at 18:44
  • 2
    @GünterZöchbauer but i think mirrors are no problem i am doing something like JAX-RS in java so i could build a restful services using annotations, mirrors get used only upon initialization after that everything gets mapped, so its all on the server side, as far as i know the only problem with dart mirrors is dart2js – FPGA Jun 27 '14 at 18:56
  • 2
    @FPGA sorry for abusing your question with this discussion. This is only related to dart2js but not to server side code (it might be in the future though, when dart2dart is used for deployment of server side code - I have no idea if this is planned). – Günter Zöchbauer Jun 27 '14 at 18:58