Search code examples
dartdart-mirrors

Reflectable: myAnnotation.annotatedClasses different result CmdApp<>Client


Say I have the following Annotation and 2 classes:

class AppModel extends Reflectable {
  final String name;
  const AppModel([this.name])
      : super(newInstanceCapability, metadataCapability);
}

const appModel = const AppModel();

@appModel
class ImGonnaBePickedUp {

}

@AppModel(' :( ')
class AndImNotPickedUpOnServer_IDoOnWebClient {

}

main() {
  appModel.annotatedClasses // that's what I mean by "Picked Up".
}

On CmdApp side (Server): only AndImNotPickedUpOnServer_IDoOnWebClient is given in appModel.annotatedClasses.

On the web side, both classes are given.

Long story short, how do I retrieve classes annotated with direct const constructor calls like in the example above @AppModel(' :( ') (for both CmdApp and Web)?


Solution

  • since version 0.5.4 reflectable classes doesn't support constructors with arguments

    This appears in reflectable documentation:

    Footnotes: 1. Currently, the only setup which is supported is when the metadata object is an instance of a direct subclass of the class [Reflectable], say MyReflectable, and that subclass defines a const constructor taking zero arguments. This ensures that every subclass of Reflectable used as metadata is a singleton class, which means that the behavior of the instance can be expressed by generating code in the class. Generalizations of this setup may be supported in the future if compelling use cases come up.

    one possible solution could be to use a second annotation to handle the name, for example:

    import 'package:reflectable/reflectable.dart';
    import 'package:drails_commons/drails_commons.dart';
    
    class AppModel extends Reflectable {
      const AppModel()
          : super(newInstanceCapability, metadataCapability);
    }
    
    const appModel = const AppModel();
    
    class TableName {
    
      final String name;
    
      const TableName(this.name);
    
    }
    
    @appModel
    class ImGonnaBePickedUp {
    
    }
    
    @appModel
    @TableName(' :( ')
    class AndImNotPickedUpOnServer_WorksOnWebClient {
    
    }
    
    main() {
      print(appModel.annotatedClasses); // that's what I mean by "Picked Up".
    
      print(new GetValueOfAnnotation<TableName>()
          .fromDeclaration(appModel.reflectType(AndImNotPickedUpOnServer_WorksOnWebClient)).name);
    }
    

    Note: I'm also using drails_common package