OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this | 1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in | 2 // source code is governed by a BSD-style license that can be found in |
3 // the LICENSE file. | 3 // the LICENSE file. |
4 | 4 |
5 library reflectable.src.mirrors_unimpl; | 5 library reflectable.src.mirrors_unimpl; |
6 | 6 |
7 import 'dart:collection' show UnmodifiableMapView, UnmodifiableListView; | 7 import 'dart:collection' show UnmodifiableMapView, UnmodifiableListView; |
8 | 8 |
9 import '../capability.dart'; | 9 import '../capability.dart'; |
10 import '../mirrors.dart'; | 10 import '../mirrors.dart'; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 if (_typeToClassMirrorCache == null) { | 102 if (_typeToClassMirrorCache == null) { |
103 if (classMirrors.isEmpty) { | 103 if (classMirrors.isEmpty) { |
104 // This is the case when the capabilities do not include a | 104 // This is the case when the capabilities do not include a |
105 // `TypeCapability`; it is also the case when there are no | 105 // `TypeCapability`; it is also the case when there are no |
106 // supported classes but even then we can do the following. | 106 // supported classes but even then we can do the following. |
107 _typeToClassMirrorCache = <Type, ClassMirror>{}; | 107 _typeToClassMirrorCache = <Type, ClassMirror>{}; |
108 } else { | 108 } else { |
109 _typeToClassMirrorCache = new Map.fromIterables(types, classMirrors); | 109 _typeToClassMirrorCache = new Map.fromIterables(types, classMirrors); |
110 } | 110 } |
111 } | 111 } |
112 return _typeToClassMirrorCache[type]; | 112 ClassMirror result = _typeToClassMirrorCache[type]; |
113 if (result == null && "$type".startsWith("_")) { | |
sigurdm
2015/10/15 12:30:58
Abstract startsWith("_") to isPrivateName()
eernst
2015/10/15 13:46:11
This section deleted: We do not support reflecting
| |
114 for (Type typeKey in _typeToClassMirrorCache.keys) { | |
115 if (typeKey is FakeType && typeKey.correspondsTo(type)) { | |
116 return _typeToClassMirrorCache[typeKey]; | |
117 } | |
118 } | |
119 } | |
120 return result; | |
113 } | 121 } |
114 } | 122 } |
115 | 123 |
116 /// This mapping contains the mirror-data for each reflector. | 124 /// This mapping contains the mirror-data for each reflector. |
117 /// It will be initialized in the generated code. | 125 /// It will be initialized in the generated code. |
118 Map<Reflectable, ReflectorData> data = | 126 Map<Reflectable, ReflectorData> data = |
119 throw new StateError("Reflectable has not been initialized. " | 127 throw new StateError("Reflectable has not been initialized. " |
120 "Did you forget to add the main file to the " | 128 "Did you forget to add the main file to the " |
121 "reflectable transformer's entry_points in pubspec.yaml?"); | 129 "reflectable transformer's entry_points in pubspec.yaml?"); |
122 | 130 |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 } | 500 } |
493 | 501 |
494 String toString() => "ClassMirrorImpl($qualifiedName)"; | 502 String toString() => "ClassMirrorImpl($qualifiedName)"; |
495 | 503 |
496 // Because we take care to only ever create one instance for each | 504 // Because we take care to only ever create one instance for each |
497 // type/reflector-combination we can rely on the default `hashCode` and `==` | 505 // type/reflector-combination we can rely on the default `hashCode` and `==` |
498 // operations. | 506 // operations. |
499 } | 507 } |
500 | 508 |
501 class LibraryMirrorImpl extends _DataCaching implements LibraryMirror { | 509 class LibraryMirrorImpl extends _DataCaching implements LibraryMirror { |
502 LibraryMirrorImpl( | 510 LibraryMirrorImpl(this.simpleName, this.uri, this._reflector, |
503 this.simpleName, | 511 this._declarationIndices, this.getters, this.setters, this._metadata); |
504 this.uri, | |
505 this._reflector, | |
506 this._declarationIndices, | |
507 this.getters, | |
508 this.setters, | |
509 this._metadata); | |
510 | 512 |
511 final ReflectableImpl _reflector; | 513 final ReflectableImpl _reflector; |
512 | 514 |
513 /// A list of the indices in [ReflectorData.memberMirrors] of the | 515 /// A list of the indices in [ReflectorData.memberMirrors] of the |
514 /// declarations of the reflected class. This includes method mirrors for | 516 /// declarations of the reflected class. This includes method mirrors for |
515 /// top level functions of this library and it directly corresponds to | 517 /// top level functions of this library and it directly corresponds to |
516 /// `declarations`. Exception: When the given `_reflector.capabilities` do | 518 /// `declarations`. Exception: When the given `_reflector.capabilities` do |
517 /// not support the operation `declarations`, this will be | 519 /// not support the operation `declarations`, this will be |
518 /// `<int>[NO_CAPABILITY_INDEX]`. It is enough to check that the list is | 520 /// `<int>[NO_CAPABILITY_INDEX]`. It is enough to check that the list is |
519 /// non-empty and first element is NO_CAPABILITY_INDEX to detect this | 521 /// non-empty and first element is NO_CAPABILITY_INDEX to detect this |
(...skipping 24 matching lines...) Expand all Loading... | |
544 // need not have stellar performance, it is almost always a bug to do | 546 // need not have stellar performance, it is almost always a bug to do |
545 // that. | 547 // that. |
546 if (declarationIndex == NO_CAPABILITY_INDEX) { | 548 if (declarationIndex == NO_CAPABILITY_INDEX) { |
547 throw new NoSuchCapabilityError( | 549 throw new NoSuchCapabilityError( |
548 "Requesting declarations of '$qualifiedName' without capability"); | 550 "Requesting declarations of '$qualifiedName' without capability"); |
549 } | 551 } |
550 DeclarationMirror declarationMirror = | 552 DeclarationMirror declarationMirror = |
551 _data.memberMirrors[declarationIndex]; | 553 _data.memberMirrors[declarationIndex]; |
552 result[declarationMirror.simpleName] = declarationMirror; | 554 result[declarationMirror.simpleName] = declarationMirror; |
553 } | 555 } |
556 _data.classMirrors.forEach((ClassMirror classMirror) { | |
557 if (classMirror.owner == this) { | |
558 result[classMirror.simpleName] = classMirror; | |
559 } | |
560 }); | |
554 _declarations = | 561 _declarations = |
555 new UnmodifiableMapView<String, DeclarationMirror>(result); | 562 new UnmodifiableMapView<String, DeclarationMirror>(result); |
556 } | 563 } |
557 return _declarations; | 564 return _declarations; |
558 } | 565 } |
559 | 566 |
560 @override | 567 @override |
561 Object invoke(String memberName, List positionalArguments, | 568 Object invoke(String memberName, List positionalArguments, |
562 [Map<Symbol, dynamic> namedArguments]) { | 569 [Map<Symbol, dynamic> namedArguments]) { |
563 _StaticGetter getter = getters[memberName]; | 570 _StaticGetter getter = getters[memberName]; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
605 } | 612 } |
606 return _metadata; | 613 return _metadata; |
607 } | 614 } |
608 | 615 |
609 @override | 616 @override |
610 DeclarationMirror get owner => null; | 617 DeclarationMirror get owner => null; |
611 | 618 |
612 @override | 619 @override |
613 String get qualifiedName => simpleName; | 620 String get qualifiedName => simpleName; |
614 | 621 |
615 bool operator ==(other) => _unsupported(); | 622 bool operator ==(other) { |
616 int get hashCode => _unsupported(); | 623 return other is LibraryMirrorImpl && |
624 other.uri == uri && | |
625 other._reflector == _reflector && | |
626 other._declarationIndices == _declarationIndices; | |
627 } | |
628 | |
629 int get hashCode => | |
630 uri.hashCode ^ _reflector.hashCode ^ _declarationIndices.hashCode; | |
617 | 631 |
618 // TODO(sigurdm) implement: Need to implement this. Probably only when a given | 632 // TODO(sigurdm) implement: Need to implement this. Probably only when a given |
619 // capability is enabled. | 633 // capability is enabled. |
620 List<LibraryDependencyMirror> get libraryDependencies => _unsupported(); | 634 List<LibraryDependencyMirror> get libraryDependencies => _unsupported(); |
621 } | 635 } |
622 | 636 |
623 class MethodMirrorImpl extends _DataCaching implements MethodMirror { | 637 class MethodMirrorImpl extends _DataCaching implements MethodMirror { |
624 /// An encoding of the attributes and kind of this mirror. | 638 /// An encoding of the attributes and kind of this mirror. |
625 final int _descriptor; | 639 final int _descriptor; |
626 | 640 |
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1248 } | 1262 } |
1249 return new UnmodifiableMapView(result); | 1263 return new UnmodifiableMapView(result); |
1250 } | 1264 } |
1251 | 1265 |
1252 @override | 1266 @override |
1253 Iterable<ClassMirror> get annotatedClasses { | 1267 Iterable<ClassMirror> get annotatedClasses { |
1254 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); | 1268 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); |
1255 } | 1269 } |
1256 } | 1270 } |
1257 | 1271 |
1258 // For mixin-applications we need to construct objects that represents their | 1272 // For mixin-applications we need to construct objects that represents their |
sigurdm
2015/10/15 12:30:58
I think this comment needs to be updated.
eernst
2015/10/15 13:46:11
Done.
| |
1259 // type. | 1273 // type. |
1260 class FakeType implements Type { | 1274 class FakeType implements Type { |
1261 const FakeType(this.description); | 1275 const FakeType(this.description); |
1262 | 1276 |
1263 final String description; | 1277 final String description; |
1264 | 1278 |
1265 String toString() => "Type($description)"; | 1279 String toString() => "Type($description)"; |
1280 | |
1281 /// Make a best effort attempt to recognize that this fake type represents | |
1282 /// the given [type]. Only private non-mixin-application types are recognized, | |
1283 bool correspondsTo(Type type) { | |
sigurdm
2015/10/15 12:30:58
I think this support for reflecting on instances o
eernst
2015/10/15 13:46:11
Deleted, too.
| |
1284 if (description.contains(" with ")) return false; | |
1285 String typeName = type.toString(); | |
1286 if (!typeName.startsWith("_")) return false; | |
1287 return description.contains(".$type"); | |
1288 } | |
1266 } | 1289 } |
OLD | NEW |