-
Notifications
You must be signed in to change notification settings - Fork 57
/
reflectable_mirror_based.dart
1530 lines (1293 loc) · 50.9 KB
/
reflectable_mirror_based.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015, the Dart Team. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in
// the LICENSE file.
// TODO(sigurdm) doc: Make NoSuchCapability messages streamlined (the same in
// both `reflectable_mirror_based.dart` and
// `reflectable_transformer_based.dart`, and explaining as well as possible
// which capability is missing.)
/// Implementation of the reflectable interface using dart mirrors.
library reflectable.src.reflectable_implementation;
import 'dart:mirrors' as dm;
import 'reflectable_base.dart';
import '../capability.dart';
import '../mirrors.dart' as rm;
import '../reflectable.dart';
bool get isTransformed => false;
/// The name of the reflectable library.
const Symbol reflectableLibrarySymbol = #reflectable.reflectable;
rm.ClassMirror wrapClassMirrorIfSupported(
dm.ClassMirror classMirror, ReflectableImpl reflectable) {
if (classMirror is dm.FunctionTypeMirror) {
// TODO(sigurdm) clarify: Exactly what capabilities will allow reflecting on
// a function is still not clear.
return new _FunctionTypeMirrorImpl(classMirror, reflectable);
} else {
assert(classMirror is dm.ClassMirror);
if (!reflectable._supportedClasses
.contains(classMirror.originalDeclaration)) {
throw new NoSuchCapabilityError(
"Reflecting on class '$classMirror' without capability");
}
return new ClassMirrorImpl(classMirror, reflectable);
}
}
rm.DeclarationMirror wrapDeclarationMirror(
dm.DeclarationMirror declarationMirror, ReflectableImpl reflectable) {
if (declarationMirror is dm.MethodMirror) {
return new MethodMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.ParameterMirror) {
return new _ParameterMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.VariableMirror) {
return new VariableMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.TypeVariableMirror) {
return new _TypeVariableMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.TypedefMirror) {
return new _TypedefMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.ClassMirror) {
// Covers FunctionTypeMirror and ClassMirror.
return wrapClassMirrorIfSupported(declarationMirror, reflectable);
} else {
assert(declarationMirror is dm.LibraryMirror);
return new _LibraryMirrorImpl(declarationMirror, reflectable);
}
}
rm.InstanceMirror wrapInstanceMirror(
dm.InstanceMirror instanceMirror, ReflectableImpl reflectable) {
if (instanceMirror is dm.ClosureMirror) {
return new _ClosureMirrorImpl(instanceMirror, reflectable);
} else {
assert(instanceMirror is dm.InstanceMirror);
return new _InstanceMirrorImpl(instanceMirror, reflectable);
}
}
rm.ObjectMirror wrapObjectMirror(
dm.ObjectMirror m, ReflectableImpl reflectable) {
if (m is dm.LibraryMirror) {
return new _LibraryMirrorImpl(m, reflectable);
} else if (m is dm.InstanceMirror) {
return wrapInstanceMirror(m, reflectable);
} else {
assert(m is dm.ClassMirror);
return wrapClassMirrorIfSupported(m, reflectable);
}
}
rm.TypeMirror wrapTypeMirror(
dm.TypeMirror typeMirror, ReflectableImpl reflectable) {
if (typeMirror is dm.TypeVariableMirror) {
return new _TypeVariableMirrorImpl(typeMirror, reflectable);
} else if (typeMirror is dm.TypedefMirror) {
return new _TypedefMirrorImpl(typeMirror, reflectable);
} else if (typeMirror.reflectedType == dynamic) {
return new _SpecialTypeMirrorImpl(typeMirror, reflectable);
} else {
assert(typeMirror is dm.ClassMirror);
return wrapClassMirrorIfSupported(typeMirror, reflectable);
}
}
rm.CombinatorMirror wrapCombinatorMirror(dm.CombinatorMirror combinatorMirror) {
return new _CombinatorMirrorImpl(combinatorMirror);
}
dm.ClassMirror unwrapClassMirror(rm.ClassMirror m) {
if (m is ClassMirrorImpl) {
// This also works for _FunctionTypeMirrorImpl
return m._classMirror;
} else {
throw new ArgumentError("Unexpected subtype of ClassMirror");
}
}
dm.TypeMirror unwrapTypeMirror(rm.TypeMirror TypeMirror) {
if (TypeMirror is _TypeMirrorImpl) {
return TypeMirror._typeMirror;
} else {
throw new ArgumentError("Unexpected subtype of TypeMirror");
}
}
/// Returns true iff an element of the [metadata] is supported by the
/// [capability].
bool metadataSupported(
List<dm.InstanceMirror> metadata, MetadataQuantifiedCapability capability) {
return metadata
.map((dm.InstanceMirror x) => x.reflectee.runtimeType)
.contains(capability.metadataType);
}
bool impliesUpwardsClosure(List<ReflectCapability> capabilities) {
return capabilities.any((ReflectCapability capability) =>
capability is SuperclassQuantifyCapability);
}
List<dm.ClassMirror> extractUpwardsClosureBounds(
List<ReflectCapability> capabilities) {
List<dm.ClassMirror> result = <dm.ClassMirror>[];
capabilities.forEach((ReflectCapability capability) {
if (capability is SuperclassQuantifyCapability) {
dm.TypeMirror typeMirror = dm.reflectType(capability.upperBound);
if (typeMirror is dm.ClassMirror) {
result.add(typeMirror);
} else {
throw new ArgumentError("Unexpected kind of upper bound specified "
"for a `SuperclassQuantifyCapability`: $typeMirror.");
}
}
});
return result;
}
bool impliesDeclarations(List<ReflectCapability> capabilities) {
return capabilities.contains(declarationsCapability);
}
bool impliesTypes(List<ReflectCapability> capabilities) {
if (!impliesDeclarations(capabilities)) return false;
return capabilities.any((ReflectCapability capability) {
return capability is TypeCapability;
});
}
/// Returns true if [classMirror] supports reflective invoke of the
/// instance-member named [name] according to the
/// capabilities of [reflectable].
bool reflectableSupportsInstanceInvoke(
ReflectableImpl reflectable, String name, dm.ClassMirror classMirror) {
Symbol nameSymbol = new Symbol(name);
bool predicate(ApiReflectCapability capability) {
if (capability == invokingCapability ||
capability == instanceInvokeCapability) {
return true;
} else if (capability is InstanceInvokeCapability) {
return new RegExp(capability.namePattern).hasMatch(name);
} else if (capability is InstanceInvokeMetaCapability) {
// We have to use 'declarations' here instead of instanceMembers, because
// the synthetic field mirrors generated from fields do not have metadata
// attached.
// TODO(eernst) clarify: If we use `declarations` in a context where
// `instanceMembers` is expected, we must iterate over all superclasses
// "manually". Check whether there is a bug here, alternatively update
// the above comment.
dm.DeclarationMirror declarationMirror =
classMirror.declarations[nameSymbol];
return declarationMirror != null &&
metadataSupported(declarationMirror.metadata, capability);
} else {
return false;
}
}
return reflectable.hasCapability(predicate);
}
/// Returns true iff [reflectable] supports static invoke of [name].
bool reflectableSupportsStaticInvoke(ReflectableImpl reflectable, String name,
List<dm.InstanceMirror> metadata) {
bool predicate(ApiReflectCapability capability) {
if (capability == staticInvokeCapability) {
return true;
} else if (capability is StaticInvokeCapability) {
return new RegExp(capability.namePattern).hasMatch(name);
}
if (capability is StaticInvokeMetaCapability) {
return metadataSupported(metadata, capability);
}
return false;
}
return reflectable.hasCapability(predicate);
}
/// Returns true iff [reflectable] supports top-level invoke of [name].
bool reflectableSupportsTopLevelInvoke(ReflectableImpl reflectable, String name,
List<dm.InstanceMirror> metadata) {
// We don't have to check for `libraryCapability` because this method is only
// ever called from the methods of a `LibraryMirrorImpl` and the existence of
// an instance of a `LibraryMirrorImpl` implies the `libraryMirrorCapability`.
// Finally, we do not have to check for the capability as given to a subtype,
// because a library is never a subtype of anything.
return reflectable.hasCapability((ApiReflectCapability capability) {
if (capability == topLevelInvokeCapability) {
return true;
} else if (capability is TopLevelInvokeCapability) {
return new RegExp(capability.namePattern).hasMatch(name);
}
if (capability is TopLevelInvokeMetaCapability) {
return metadataSupported(metadata, capability);
}
return false;
});
}
/// Returns true iff [reflectable] supports invoke of [name].
bool reflectableSupportsConstructorInvoke(
ReflectableImpl reflectable, dm.ClassMirror classMirror, String name) {
bool predicate(ApiReflectCapability capability) {
if (capability == newInstanceCapability) {
return true;
} else if (capability is NewInstanceCapability) {
return new RegExp(capability.namePattern).hasMatch(name);
} else if (capability is NewInstanceMetaCapability) {
dm.MethodMirror constructorMirror = classMirror.declarations.values
.firstWhere((dm.DeclarationMirror declarationMirror) {
Symbol nameSymbol = new Symbol(name);
return declarationMirror is dm.MethodMirror &&
declarationMirror.isConstructor &&
declarationMirror.constructorName == nameSymbol;
});
if (constructorMirror == null ||
!constructorMirror.isConstructor) return false;
return metadataSupported(constructorMirror.metadata, metadataCapability);
}
return false;
}
return reflectable.hasCapability(predicate);
}
bool reflectableSupportsDeclarations(Reflectable reflectable) {
bool predicate(ApiReflectCapability capability) =>
capability == declarationsCapability;
return reflectable.hasCapability(predicate);
}
/// Returns [setterName] with final "=" removed.
/// If the it does not have a final "=" it is returned as is.
String _setterToGetter(String setterName) {
if (!setterName.endsWith("=")) {
return setterName;
}
return setterName.substring(0, setterName.length - 1);
}
/// Returns [getterName] with a final "=" added.
/// If [getter] already ends with "=" an error is thrown.
String _getterToSetter(String getterName) {
if (getterName.endsWith("=")) {
throw new ArgumentError(
"$getterName is a setter name (ends with `=`) already.");
}
return '$getterName=';
}
bool _supportsType(List<ReflectCapability> capabilities) {
return capabilities.any((ReflectCapability capability) =>
capability is TypeCapability);
}
abstract class _ObjectMirrorImplMixin implements rm.ObjectMirror {
ReflectableImpl get _reflectable;
}
class _LibraryMirrorImpl extends _DeclarationMirrorImpl
with _ObjectMirrorImplMixin
implements rm.LibraryMirror {
dm.LibraryMirror get _libraryMirror => _declarationMirror;
_LibraryMirrorImpl(dm.LibraryMirror m, ReflectableImpl reflectable)
: super(m, reflectable) {}
@override
Uri get uri => _libraryMirror.uri;
@override
Map<String, rm.DeclarationMirror> get declarations {
if (!reflectableSupportsDeclarations(_reflectable)) {
throw new NoSuchCapabilityError(
"Attempt to get declarations without capability");
}
Map<Symbol, dm.DeclarationMirror> decls = _libraryMirror.declarations;
Iterable<Symbol> relevantKeys = decls.keys.where((k) {
List<dm.InstanceMirror> metadata = decls[k].metadata;
for (var item in metadata) {
if (item.hasReflectee && item.reflectee is ReflectableImpl) return true;
}
return false;
});
return new Map<String, rm.DeclarationMirror>.fromIterable(relevantKeys,
key: (k) => dm.MirrorSystem.getName(k),
value: (v) => wrapDeclarationMirror(decls[v], _reflectable));
}
@override
Object invoke(String memberName, List positionalArguments,
[Map<Symbol, dynamic> namedArguments]) {
if (!reflectableSupportsTopLevelInvoke(_reflectable, memberName,
_libraryMirror.declarations[new Symbol(memberName)].metadata)) {
throw new NoSuchInvokeCapabilityError(
_receiver, memberName, positionalArguments, namedArguments);
}
return _libraryMirror
.invoke(new Symbol(memberName), positionalArguments, namedArguments)
.reflectee;
}
@override
Object invokeGetter(String getterName) {
if (!reflectableSupportsTopLevelInvoke(_reflectable, getterName,
_libraryMirror.declarations[new Symbol(getterName)].metadata)) {
throw new NoSuchInvokeCapabilityError(_receiver, getterName, [], null);
}
Symbol getterNameSymbol = new Symbol(getterName);
return _libraryMirror.getField(getterNameSymbol).reflectee;
}
@override
Object invokeSetter(String setterName, Object value) {
if (!reflectableSupportsTopLevelInvoke(_reflectable, setterName,
_libraryMirror.declarations[new Symbol(setterName)].metadata)) {
throw new NoSuchInvokeCapabilityError(
_receiver, setterName, [value], null);
}
String getterName = _setterToGetter(setterName);
Symbol getterNameSymbol = new Symbol(getterName);
return _libraryMirror.setField(getterNameSymbol, value).reflectee;
}
@override
bool operator ==(other) {
return other is _LibraryMirrorImpl
? _libraryMirror == other._libraryMirror
: false;
}
@override
int get hashCode => _libraryMirror.hashCode;
@override
List<rm.LibraryDependencyMirror> get libraryDependencies {
return _libraryMirror.libraryDependencies
.map((dep) => new _LibraryDependencyMirrorImpl(dep, _reflectable))
.toList();
}
get _receiver => "top-level";
@override
String toString() {
return "_LibraryMirrorImpl('${_libraryMirror.toString()}')";
}
}
class _LibraryDependencyMirrorImpl implements rm.LibraryDependencyMirror {
final dm.LibraryDependencyMirror _libraryDependencyMirror;
final ReflectableImpl _reflectable;
_LibraryDependencyMirrorImpl(
this._libraryDependencyMirror, this._reflectable);
@override
bool get isImport => _libraryDependencyMirror.isImport;
@override
bool get isExport => _libraryDependencyMirror.isExport;
@override
rm.LibraryMirror get sourceLibrary {
return new _LibraryMirrorImpl(
_libraryDependencyMirror.sourceLibrary, _reflectable);
}
@override
rm.LibraryMirror get targetLibrary {
return new _LibraryMirrorImpl(
_libraryDependencyMirror.targetLibrary, _reflectable);
}
@override
String get prefix => dm.MirrorSystem.getName(_libraryDependencyMirror.prefix);
@override
List<Object> get metadata =>
_libraryDependencyMirror.metadata.map((m) => m.reflectee).toList();
@override
rm.SourceLocation get location {
return new _SourceLocationImpl(_libraryDependencyMirror.location);
}
@override
List<rm.CombinatorMirror> get combinators {
return _libraryDependencyMirror.combinators
.map(wrapCombinatorMirror)
.toList();
}
@override
bool get isDeferred => _libraryDependencyMirror.isDeferred;
@override
String toString() {
return "_LibraryDependencyMirrorImpl('${_libraryDependencyMirror}')";
}
}
class _InstanceMirrorImpl extends _ObjectMirrorImplMixin
implements rm.InstanceMirror {
final dm.InstanceMirror _instanceMirror;
final ReflectableImpl _reflectable;
_InstanceMirrorImpl(this._instanceMirror, this._reflectable);
@override
rm.TypeMirror get type => wrapTypeMirror(_instanceMirror.type, _reflectable);
@override
bool get hasReflectee => _instanceMirror.hasReflectee;
@override
get reflectee => _instanceMirror.reflectee;
@override
Object invoke(String memberName, List positionalArguments,
[Map<Symbol, dynamic> namedArguments]) {
if (reflectableSupportsInstanceInvoke(
_reflectable, memberName, _instanceMirror.type)) {
Symbol memberNameSymbol = new Symbol(memberName);
return _instanceMirror
.invoke(memberNameSymbol, positionalArguments, namedArguments)
.reflectee;
}
throw new NoSuchInvokeCapabilityError(
_receiver, memberName, positionalArguments, namedArguments);
}
@override
Object invokeGetter(String fieldName) {
if (reflectableSupportsInstanceInvoke(
_reflectable, fieldName, _instanceMirror.type)) {
Symbol fieldNameSymbol = new Symbol(fieldName);
return _instanceMirror.getField(fieldNameSymbol).reflectee;
}
throw new NoSuchInvokeCapabilityError(_receiver, fieldName, [], null);
}
@override
Object invokeSetter(String setterName, Object value) {
if (reflectableSupportsInstanceInvoke(
_reflectable, setterName, _instanceMirror.type)) {
String getterName = _setterToGetter(setterName);
Symbol getterNameSymbol = new Symbol(getterName);
return _instanceMirror.setField(getterNameSymbol, value).reflectee;
}
throw new NoSuchInvokeCapabilityError(_receiver, setterName, [value], null);
}
@override
bool operator ==(other) => other is _InstanceMirrorImpl
? _instanceMirror == other._instanceMirror
: false;
@override
int get hashCode => _instanceMirror.hashCode;
@override
delegate(Invocation invocation) => _instanceMirror.delegate(invocation);
get _receiver => _instanceMirror.reflectee;
@override
String toString() => "_InstanceMirrorImpl('${_instanceMirror}')";
}
class ClassMirrorImpl extends _TypeMirrorImpl
with _ObjectMirrorImplMixin
implements rm.ClassMirror {
dm.ClassMirror get _classMirror => _declarationMirror;
ClassMirrorImpl(dm.ClassMirror classMirror, ReflectableImpl reflectable)
: super(classMirror, reflectable) {}
@override
List<rm.TypeVariableMirror> get typeVariables {
return _classMirror.typeVariables.map((v) {
return new _TypeVariableMirrorImpl(v, _reflectable);
}).toList();
}
@override
List<rm.TypeMirror> get typeArguments => _classMirror.typeArguments.map((a) {
return wrapTypeMirror(a, _reflectable);
}).toList();
@override
rm.ClassMirror get superclass {
dm.ClassMirror sup = _classMirror.superclass;
if (sup == null) return null; // For `Object`, do as `dm`.
return wrapClassMirrorIfSupported(sup, _reflectable);
}
@override
List<rm.ClassMirror> get superinterfaces {
List<dm.ClassMirror> superinterfaces = _classMirror.superinterfaces;
return superinterfaces
.where((dm.ClassMirror superinterface) => (_reflectable
._supportedClasses.contains(superinterface.originalDeclaration)))
.map((dm.TypeMirror superinterface) {
return wrapTypeMirror(superinterface, _reflectable);
}).toList();
}
@override
bool get isAbstract => _classMirror.isAbstract;
@override
Map<String, rm.DeclarationMirror> get declarations {
if (!reflectableSupportsDeclarations(_reflectable)) {
throw new NoSuchCapabilityError(
"Attempt to get declarations without capability");
}
// TODO(sigurdm) future: Possibly cache this.
Map<String, rm.DeclarationMirror> result =
new Map<String, rm.DeclarationMirror>();
_classMirror.declarations
.forEach((Symbol nameSymbol, dm.DeclarationMirror declarationMirror) {
String name = dm.MirrorSystem.getName(nameSymbol);
if (declarationMirror is dm.MethodMirror) {
if (_isMixin && declarationMirror.isStatic) return;
bool included = false;
if (declarationMirror.isConstructor) {
// Factory constructors are static, others not, so this decision
// is made independently of `isStatic`.
included = included ||
reflectableSupportsConstructorInvoke(
_reflectable, _classMirror, name);
} else {
// `declarationMirror` is a non-constructor.
if (declarationMirror.isStatic) {
// `declarationMirror` is a static method/getter/setter.
included = included ||
reflectableSupportsStaticInvoke(
_reflectable, name, declarationMirror.metadata);
} else {
// `declarationMirror` is an instance method/getter/setter.
included = included ||
reflectableSupportsInstanceInvoke(
_reflectable, name, _classMirror);
}
}
if (included) {
result[name] = wrapDeclarationMirror(declarationMirror, _reflectable);
}
} else if (declarationMirror is dm.VariableMirror) {
if (_isMixin && declarationMirror.isStatic) return;
// For variableMirrors we test both for support of the name and the
// derived setter name.
bool included = false;
if (declarationMirror.isStatic) {
included = included ||
reflectableSupportsStaticInvoke(
_reflectable, name, declarationMirror.metadata) ||
reflectableSupportsStaticInvoke(_reflectable,
_getterToSetter(name), declarationMirror.metadata);
} else {
included = included ||
reflectableSupportsInstanceInvoke(
_reflectable, name, _classMirror) ||
reflectableSupportsInstanceInvoke(
_reflectable, _getterToSetter(name), _classMirror);
}
if (included) {
result[name] = wrapDeclarationMirror(declarationMirror, _reflectable);
}
} else {
// TODO(sigurdm) implement: Consider capabilities for other
// declarations; the additional subtypes of DeclarationMirror are
// LibraryMirror (global), TypeMirror and the 4 subtypes thereof,
// and ParameterMirror (declared in a method, not class), so the
// only case that needs further clarification is TypeVariableMirror.
// For now we just let it through.
result[name] = wrapDeclarationMirror(declarationMirror, _reflectable);
}
});
return result;
}
@override
Map<String, rm.MethodMirror> get instanceMembers {
// TODO(sigurdm) implement: Only expose members that are allowed
// by the capabilities of the reflectable.
Map<Symbol, dm.MethodMirror> members = _classMirror.instanceMembers;
return new Map<String, rm.MethodMirror>.fromIterable(members.keys,
key: (k) => dm.MirrorSystem.getName(k),
value: (v) => new MethodMirrorImpl(members[v], _reflectable));
}
@override
Map<String, rm.MethodMirror> get staticMembers {
// TODO(sigurdm) implement: Only expose members that are allowed by the
// capabilities of the reflectable.
Map<Symbol, dm.MethodMirror> members = _classMirror.staticMembers;
return new Map<String, rm.MethodMirror>.fromIterable(members.keys,
key: (k) => dm.MirrorSystem.getName(k),
value: (v) => new MethodMirrorImpl(members[v], _reflectable));
}
@override
rm.ClassMirror get mixin {
return wrapClassMirrorIfSupported(_classMirror.mixin, _reflectable);
}
@override
Object newInstance(String constructorName, List positionalArguments,
[Map<Symbol, dynamic> namedArguments]) {
if (!reflectableSupportsConstructorInvoke(
_reflectable, _classMirror, constructorName)) {
throw new NoSuchInvokeCapabilityError(
_classMirror, constructorName, positionalArguments, namedArguments);
}
Symbol constructorNameSymbol = new Symbol(constructorName);
return _classMirror
.newInstance(constructorNameSymbol, positionalArguments, namedArguments)
.reflectee;
}
@override
Object invoke(String memberName, List positionalArguments,
[Map<Symbol, dynamic> namedArguments]) {
dm.DeclarationMirror declaration =
_classMirror.declarations[new Symbol(memberName)];
List<Object> metadata = declaration == null ? null : declaration.metadata;
if (reflectableSupportsStaticInvoke(_reflectable, memberName, metadata)) {
Symbol memberNameSymbol = new Symbol(memberName);
return _classMirror
.invoke(memberNameSymbol, positionalArguments, namedArguments)
.reflectee;
}
throw new NoSuchInvokeCapabilityError(
_receiver, memberName, positionalArguments, namedArguments);
}
@override
Object invokeGetter(String getterName) {
if (reflectableSupportsStaticInvoke(_reflectable, getterName,
_classMirror.declarations[new Symbol(getterName)].metadata)) {
Symbol getterNameSymbol = new Symbol(getterName);
return _classMirror.getField(getterNameSymbol).reflectee;
}
throw new NoSuchInvokeCapabilityError(_receiver, getterName, [], null);
}
@override
Object invokeSetter(String setterName, Object value) {
if (reflectableSupportsStaticInvoke(_reflectable, setterName,
_classMirror.declarations[new Symbol(setterName)].metadata)) {
String getterName = _setterToGetter(setterName);
Symbol getterNameSymbol = new Symbol(getterName);
return _classMirror.setField(getterNameSymbol, value).reflectee;
}
throw new NoSuchInvokeCapabilityError(_receiver, setterName, [value], null);
}
@override
bool operator ==(other) {
return other is ClassMirrorImpl
? _classMirror == other._classMirror
: false;
}
@override
int get hashCode => _classMirror.hashCode;
@override
bool isSubclassOf(rm.ClassMirror other) {
return _classMirror.isSubclassOf(unwrapClassMirror(other));
}
get _receiver => _classMirror.reflectedType;
@override
String toString() => "ClassMirrorImpl('${_classMirror}')";
@override
Function invoker(String memberName) {
Symbol memberNameSymbol = new Symbol(memberName);
Function helper(Object o) {
dm.InstanceMirror receiverMirror = dm.reflect(o);
dm.InstanceMirror memberMirror =
receiverMirror.getField(memberNameSymbol);
return memberMirror.reflectee;
}
return helper;
}
@override
rm.LibraryMirror get owner {
if (_reflectable._supportsLibraries) return super.owner;
throw new NoSuchCapabilityError(
"Trying to get owner of class '$qualifiedName' "
"without 'libraryCapability'");
}
bool get _isMixin => _classMirror.mixin != _classMirror;
}
class _FunctionTypeMirrorImpl extends ClassMirrorImpl
implements rm.FunctionTypeMirror {
dm.FunctionTypeMirror get _functionTypeMirror => _classMirror;
_FunctionTypeMirrorImpl(
dm.FunctionTypeMirror functionTypeMirror, ReflectableImpl reflectable)
: super(functionTypeMirror, reflectable);
@override
rm.MethodMirror get callMethod {
return new MethodMirrorImpl(_functionTypeMirror.callMethod, _reflectable);
}
@override
List<rm.ParameterMirror> get parameters {
return _functionTypeMirror.parameters
.map((dm.ParameterMirror parameterMirror) {
return new _ParameterMirrorImpl(parameterMirror, _reflectable);
});
}
@override
rm.TypeMirror get returnType {
// TODO(eernst) clarify: Is it really true that we cannot detect the
// type void other than looking at its name?
if (_functionTypeMirror.returnType.simpleName == "void") {
return new _VoidMirrorImpl();
}
return wrapTypeMirror(_functionTypeMirror.returnType, _reflectable);
}
}
abstract class _DeclarationMirrorImpl implements rm.DeclarationMirror {
final dm.DeclarationMirror _declarationMirror;
final ReflectableImpl _reflectable;
_DeclarationMirrorImpl(this._declarationMirror, this._reflectable);
@override
String get simpleName =>
dm.MirrorSystem.getName(_declarationMirror.simpleName);
@override
String get qualifiedName =>
dm.MirrorSystem.getName(_declarationMirror.qualifiedName);
@override
rm.DeclarationMirror get owner {
return wrapDeclarationMirror(_declarationMirror.owner, _reflectable);
}
@override
bool get isPrivate => _declarationMirror.isPrivate;
@override
bool get isTopLevel => _declarationMirror.isTopLevel;
@override
List<Object> get metadata {
if (_reflectable.capabilities.contains(metadataCapability)) {
return _declarationMirror.metadata.map((m) => m.reflectee).toList();
}
throw new NoSuchCapabilityError(
"Attempt to get metadata witout 'metadataCapability'");
}
@override
rm.SourceLocation get location {
return new _SourceLocationImpl(_declarationMirror.location);
}
@override
String toString() {
return "_DeclarationMirrorImpl('${_declarationMirror}')";
}
}
class MethodMirrorImpl extends _DeclarationMirrorImpl
implements rm.MethodMirror {
dm.MethodMirror get _methodMirror => _declarationMirror;
MethodMirrorImpl(dm.MethodMirror mm, ReflectableImpl reflectable)
: super(mm, reflectable);
@override
rm.TypeMirror get returnType {
// TODO(eernst) clarify: Is there really no other way to detect
// the `void` type than looking at its name?
if (_methodMirror.returnType.simpleName == const Symbol("void")) {
return new _VoidMirrorImpl();
}
return wrapTypeMirror(_methodMirror.returnType, _reflectable);
}
@override
String get source => _methodMirror.source;
@override
List<rm.ParameterMirror> get parameters {
return _methodMirror.parameters.map((p) {
return new _ParameterMirrorImpl(p, _reflectable);
}).toList();
}
@override
bool get isStatic => _methodMirror.isStatic;
@override
bool get isAbstract => _methodMirror.isAbstract;
@override
bool get isSynthetic => _methodMirror.isSynthetic;
@override
bool get isRegularMethod => _methodMirror.isRegularMethod;
@override
bool get isOperator => _methodMirror.isOperator;
@override
bool get isGetter => _methodMirror.isGetter;
@override
bool get isSetter => _methodMirror.isSetter;
@override
bool get isConstructor => _methodMirror.isConstructor;
@override
String get constructorName =>
dm.MirrorSystem.getName(_methodMirror.constructorName);
@override
bool get isConstConstructor => _methodMirror.isConstConstructor;
@override
bool get isGenerativeConstructor => _methodMirror.isGenerativeConstructor;
@override
bool get isRedirectingConstructor => _methodMirror.isRedirectingConstructor;
@override
bool get isFactoryConstructor => _methodMirror.isFactoryConstructor;
@override
bool operator ==(other) {
return other is MethodMirrorImpl
? _methodMirror == other._methodMirror
: false;
}
@override
int get hashCode => _methodMirror.hashCode;
@override
String toString() => "_MethodMirrorImpl('${_methodMirror}')";
}
class _ClosureMirrorImpl extends _InstanceMirrorImpl
implements rm.ClosureMirror {
dm.ClosureMirror get _closureMirror => _instanceMirror;
_ClosureMirrorImpl(
dm.ClosureMirror closureMirror, ReflectableImpl reflectable)
: super(closureMirror, reflectable);
@override
Object apply(List positionalArguments,
[Map<Symbol, dynamic> namedArguments]) {
return _closureMirror.apply(positionalArguments, namedArguments).reflectee;
}
@override
rm.MethodMirror get function {
return new MethodMirrorImpl(_closureMirror.function, _reflectable);
}
@override
String toString() => "_ClosureMirrorImpl('${_closureMirror}')";
}
class VariableMirrorImpl extends _DeclarationMirrorImpl
implements rm.VariableMirror {
dm.VariableMirror get _variableMirror => _declarationMirror;
VariableMirrorImpl(dm.VariableMirror vm, ReflectableImpl reflectable)
: super(vm, reflectable);
@override
rm.TypeMirror get type {
if (_supportsType(_reflectable.capabilities)) {
return wrapTypeMirror(_variableMirror.type, _reflectable);
}
throw new NoSuchCapabilityError("Attempt to get type without capability");
}
@override
bool get isStatic => _variableMirror.isStatic;
@override
bool get isFinal => _variableMirror.isFinal;
@override
bool get isConst => _variableMirror.isConst;
@override
bool operator ==(other) => other is VariableMirrorImpl
? _variableMirror == other._variableMirror
: false;
@override
int get hashCode => _variableMirror.hashCode;
@override
String toString() => "VariableMirrorImpl('${_variableMirror}')";
}
class _ParameterMirrorImpl extends VariableMirrorImpl
implements rm.ParameterMirror {
dm.ParameterMirror get _parameterMirror => _declarationMirror;
_ParameterMirrorImpl(dm.ParameterMirror pm, ReflectableImpl reflectable)
: super(pm, reflectable);
@override
bool get isOptional => _parameterMirror.isOptional;
@override
bool get isNamed => _parameterMirror.isNamed;
@override
bool get hasDefaultValue => _parameterMirror.hasDefaultValue;
@override
Object get defaultValue {
if (_parameterMirror.hasDefaultValue) {
return _parameterMirror.defaultValue.reflectee;
}
return null;
}
@override
bool operator ==(other) => other is _ParameterMirrorImpl
? _parameterMirror == other._parameterMirror
: false;
@override
int get hashCode => _parameterMirror.hashCode;
@override
String toString() => "_ParameterMirrorImpl('${_parameterMirror}')";
}
abstract class _TypeMirrorImpl extends _DeclarationMirrorImpl
implements rm.TypeMirror {
dm.TypeMirror get _typeMirror => _declarationMirror;
_TypeMirrorImpl(dm.TypeMirror typeMirror, ReflectableImpl reflectable)
: super(typeMirror, reflectable);
@override
bool get hasReflectedType => _typeMirror.hasReflectedType;
@override