lundi 18 février 2019

How to reflect the `redirecting constructor` in Dart?

I'm trying to build a JSON serialization library based on reflection (by dart:mirrors), but I cannot found a way to reflect a redirecting constructor.

For example, here a class T with a redirecting constructor, I try lots of ways to reflect the T(String) constructor, such as found it at the cls.staticMembers and cls.declarations fields, but I can't got it.

class T {
  final String test;
  const T._(this.test);
  const factory T(String test) = T._;
}


Tried:

  var cls = reflectClass(T);
  cls.declarations.entries.forEach(print);
  cls.staticMembers.entries.forEach(print);


Result:

MapEntry(Symbol("test"): VariableMirror on 'test')
MapEntry(Symbol("_redirecting#"): VariableMirror on '_redirecting#')
MapEntry(Symbol("T._"): MethodMirror on 'T._')
MapEntry(Symbol("_redirecting#"): Instance of '_SyntheticAccessor')
MapEntry(Symbol("_redirecting#="): Instance of '_SyntheticAccessor')


By the way, neither the VariableMirror on '_redirecting#' or the Instance of '_SyntheticAccessor' have the fields I wanted.

A most common example is the built-in Symbol type, it has a redirecting constructor like this:

abstract class Symbol {
  ...
  const factory Symbol(String name) = internal.Symbol;
  ...
}



What's more, for the Symbol class, I should even not found anything like Symbol("_redirecting#"). Tried:

  var cls = reflectClass(Symbol);
  cls.declarations.entries.forEach(print);
  cls.staticMembers.entries.forEach(print);


Only found:

MapEntry(Symbol("unaryMinus"): VariableMirror on 'unaryMinus')
MapEntry(Symbol("empty"): VariableMirror on 'empty')
MapEntry(Symbol("hashCode"): MethodMirror on 'hashCode')
MapEntry(Symbol("=="): MethodMirror on '==')
MapEntry(Symbol("unaryMinus"): Instance of '_SyntheticAccessor')
MapEntry(Symbol("empty"): Instance of '_SyntheticAccessor')






Aucun commentaire:

Enregistrer un commentaire